我是Spring和Hibernate的新手。我想将图像保存到MySQL数据库中。以下是我尝试使用Spring MVC和Hibernate将图像存储到数据库时的错误消息。
org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported 警告:请求方法' POST'不支持
我正在使用以下代码集:
<form:form action="saveproduct" method="post" modelAttribute="product"
enctype="multipart/form-data">
<form:hidden path="code" />
<tr>
<td>Product Name:</td>
<td><form:input type="text" path="name" placeholder="ProductName"/></td>
</tr>
<tr>
<td>Product Price:</td>
<td><form:input type="text" path="price" placeholder="price"/></td>
</tr>
<tr>
<td>Image</td>
<td>
<img src="${pageContext.request.contextPath}/productImage?
code=${product.code}" width="100"/></td>
<td> </td>
</tr>
<tr>
<td>Upload Image:</td>
<td><input type="file" name="file" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</td>
</tr>
</form:form>
@RequestMapping(value = "/productImage", method = RequestMethod.GET)
public void productimage(@RequestParam("code")int code, Model model,
HttpServletRequest request, HttpServletResponse response) throws
IOException
{
Product product = null;
if(code != 0)
{
product = productdao.findProduct(code);
}
if(product != null && product.getProimage() != null)
{
response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
response.getOutputStream().write(product.getProimage());
}
response.getOutputStream().close();
}
@RequestMapping(value = "/newproduct", method = RequestMethod.GET)
public ModelAndView newProduct()
{
Product product = new Product();
ModelAndView model = new ModelAndView();
model.addObject("product", product);
model.setViewName("NewProduct");
return model;
}
@RequestMapping(value ="/saveproduct", method = RequestMethod.POST)
public String saveProduct(@ModelAttribute("product")Product
product,@RequestParam("file") MultipartFile file)
{
try{
System.out.println(file.getOriginalFilename());
Blob blob = null;
byte[] contents = file.getBytes();
blob = new SerialBlob(contents);
product.setProimage(contents);
productdao.saveimage(product,file);
return "admin";
}
catch(Exception e)
{
System.out.println("erorr is "+e);
return "admin";
}
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int code;
@Lob
@Column(name="pro_image")
public byte[] proimage;
@Column(name="pro_name")
public String name;
@Column(name="pro_price")
public double price;
// Getter and setter
@Bean
public CommonsMultipartResolver multipartResolver() throws IOException{
CommonsMultipartResolver commonsMultipartResolver = new
CommonsMultipartResolver();
commonsMultipartResolver.setMaxUploadSize(20971520);
return commonsMultipartResolver;
}