我正在尝试使用以下代码在线上传图片。但是,由于某种原因,图像文件夹中似乎没有创建图像。不确定,我在这里做错了什么。
@RequestMapping(value="/add", method=RequestMethod.POST)
public String processAddNewProductForm(@ModelAttribute("newProduct") Product productToBeAdded, BindingResult result, HttpServletRequest request) {
String[] supressedFields = result.getSuppressedFields();
if(supressedFields.length > 0) {
throw new RuntimeException("Attempting to bind disallowed fields: " + StringUtils.arrayToCommaDelimitedString(supressedFields));
}
MultipartFile productImage = productToBeAdded.getProductImage();
String rootDirectory = request.getSession().getServletContext().getRealPath("/");
if (productImage!=null && !productImage.isEmpty()) {
try {
productImage.transferTo(new File(rootDirectory+"resources\\images\\"+ productToBeAdded.getProductId() + ".png"));
} catch (Exception e) {
throw new RuntimeException("Product Image saving failed", e);
}
}
productService.addProduct(productToBeAdded);
return "redirect:/products/";
}
DispatcherServlet的
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10240000" />
</bean>
Product.java
public class Product {
private String productId;
private String name;
private BigDecimal unitPrice;
private String description;
private String manufacturer;
private String category;
private long unitsInStock;
private long unitsInOrder;
private boolean discontinued;
private String condition;
private MultipartFile productImage;
public Product() {
super();
}
public Product(String productId, String name, BigDecimal unitPrice) {
this.productId = productId;
this.name = name;
this.unitPrice = unitPrice;
}
// add setters and getters for all the fields here
public MultipartFile getProductImage() {
return productImage;
}
public void setProductImage(MultipartFile productImage) {
this.productImage = productImage;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(BigDecimal unitPrice) {
this.unitPrice = unitPrice;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public long getUnitsInStock() {
return unitsInStock;
}
public void setUnitsInStock(long unitsInStock) {
this.unitsInStock = unitsInStock;
}
public long getUnitsInOrder() {
return unitsInOrder;
}
public void setUnitsInOrder(long unitsInOrder) {
this.unitsInOrder = unitsInOrder;
}
public boolean isDiscontinued() {
return discontinued;
}
public void setDiscontinued(boolean discontinued) {
this.discontinued = discontinued;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
product.jsp
<div class="col-md-5"> <img src="<c:url value="/resources/images/${product.productId}.png"> </c:url>" alt="image" style="width: 100%" />
答案 0 :(得分:1)
可能还有很多问题。
首先,您将解析器命名为multipartResolver
。除非您专门配置MultipartFilter
,否则它会在名称filterMultipartResolver
。
接下来,您在dispatcherservlet
中配置了解析器。即使过滤器是一个Web元素,它也独立于任何servlet并使用根应用程序上下文。
即使您没有显示,我也认为您在MultipartFilter
文件中配置了web.xml
。
您似乎只是在MultipartFile
添加了Product
。根据javadoc ,用户负责根据需要将文件内容复制到会话级或持久性存储。临时存储将在请求处理结束时清除。你应该使用类似的东西:
private byte[] image;
public void setProductImage(MultipartFile productImage) {
image = productImage.getBytes();
}
如果要将图像存储在产品中。否则,您应将其传输到文件(File dest = ...; productImage.tranferTo(dest)
;)并将名称存储在产品中。