Product类
package app06;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.springframework.web.multipart.MultipartFile;
public class Product {
private static final long servialVersionUID = 74458L;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
private String name;
private String description;
private Float price;
private List<MultipartFile> images;
public List<MultipartFile> getImages() {
return images;
}
public void setImages(List<MultipartFile> images) {
this.images = images;
}
}
ProductController类
package app06;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class ProductController {
@RequestMapping("/product_input")
public String inputProduct(Model model){
model.addAttribute("product",new Product());
return "FileUpload";
}
@RequestMapping("/product_save")
public String saveProduct(HttpServletRequest servletRequest,@ModelAttribute Product product,BindingResult bindingResult,Model model) throws IOException{
//return null;
List<MultipartFile> files = product.getImages();
List<String> fileNames = new ArrayList<String>();
//File mkdir = new File(servletRequest.getServletContext().getRealPath("/image"));
//mkdir.mkdir();
if(null != files && files.size() > 0){
for(MultipartFile mpf : files){
String fileName = mpf.getOriginalFilename();
fileNames.add(fileName);
File parFile = new File(servletRequest.getServletContext().getRealPath("/").toString()).getParentFile().getAbsoluteFile();
parFile = new File(parFile,"/app06_file");
if(!parFile.exists()){
parFile.mkdirs();
}
File imageFile = new File(parFile,fileName);
System.out.println(imageFile);
if(!imageFile.exists()){
imageFile.createNewFile();
}
try{
mpf.transferTo(imageFile);
}catch(Exception e){
e.printStackTrace();
}
}
}
return null;
}
}
FileUpload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'FileUpload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<form:form commandName="product" action="product_save" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Add a product</legend>
Product Name : <form:input path="name"/><br/>
Description: <form:input path="description"/><br/>
Price:<form:input path="price"/><br/>
Product image : <input type="file" name="images[0]"><br/>
<input type="reset"><input type="submit" value="Add A Product">
</fieldset>
</form:form>
<body>
</body>
</html>
这个代码运行是能成功上传文件的 但是我不明白
为什么不需要用form:input就能被后台的@ModelAttribute Product这个接收到 并且name的image要加个[0] 就算是不加这个[0]也是能成功的
因为我是大一新生一枚 是按照书上一步一步来的 不太了解问问题的方式 求大神指导