在说文件上传之前,先来说一下会用到的类.
MultipartFile(文件的上传)--CommonsMultipartResolver
SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的。 所以,如果要实现文件的上传,只要在 spring-mvc.xml 中注册相应的 MultipartResolver 即可。
MultipartResolver 的实现类有两个:
- CommonsMultipartResolver
- StandardServletMultipartResolver
两个的区别:
- 第一个需要使用 Apache 的 commons-fileupload 等 jar 包支持,但它能在比较旧的 servlet 版本中使用。
- 第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,但是只能在 Servlet 3 以上的版本使用。
第一个使用步骤:
/*CommonsMultipartResolver 上传用到的两个包*/
"commons-fileupload:commons-fileupload:1.3.1",
"commons-io:commons-io:2.4"
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
第二个:
<!--2 注册上传 StandardServletMultipartResolver 第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能, 但是只能在 Servlet 3 以上的版本使用。 -->
springmvc的@Validated注解使用
3. 在需要校验的pojo前边添加@Validated,在需要校验的pojo后边添加BindingResult bindingResult接收校验出错信息
注意:@Validated和BindingResult bindingResult是配对出现,并且形参顺序是固定的(一前一后)。
文件上传:
1.在xxx-servlet.xml文件中配置,spring mvc文件上传类bean配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
2.新建一个FileModel类,继承org.springframework.web.multipart.MultipartFile文件上传
import org.springframework.web.multipart.MultipartFile; public class FileModel { private MultipartFile file; public MultipartFile getFile() { return file; } public void setFile(MultipartFile file) { this.file = file; } }
3.新建模板文件
file.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <title>Spring MVC上传文件示例</title> </head> <body> <form:form method="POST" modelAttribute="fileUpload" enctype="multipart/form-data"> 请选择一个文件上传 : <input type="file" name="file" /> <input type="submit" value="提交上传" /> </form:form> </body> </html>
上传结果页
file_result.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <html> <head> <title>Spring MVC上传文件示例</title> </head> <body> 文件名称 : <b> ${fileName} </b> - 上传成功! </body> </html>
4.上传程序
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.util.FileCopyUtils; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; @Controller public class FileUploadController { @Autowired ServletContext context; @RequestMapping(value = "/fileUploadPage", method = RequestMethod.GET) public ModelAndView fileUploadPage() { FileModel file = new FileModel(); ModelAndView modelAndView = new ModelAndView("fileUpload", "command", file); return modelAndView; } @RequestMapping(value="/fileUploadPage", method = RequestMethod.POST) public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model) throws IOException { if (result.hasErrors()) { System.out.println("validation errors"); return "fileUploadPage"; } else { System.out.println("Fetching file"); MultipartFile multipartFile = file.getFile(); String uploadPath = context.getRealPath("") + File.separator + "temp" + File.separator; //Now do something with file... FileCopyUtils.copy(file.getFile().getBytes(), new File(uploadPath+file.getFile().getOriginalFilename())); String fileName = multipartFile.getOriginalFilename(); model.addAttribute("fileName", fileName); return "success"; } } }
其中public ModelAndView fileUploadPage()代码块中的fileUpload跟file.jsp中的fileUpload是配套的:modelAttribute="fileUpload"
其中public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model)代码块中的@Validated 是用来验证数据的,验证数据跟BindingResult配套使用。FileModel file 是file.jsp中提交上来的file(fileUpload)
SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的。 所以,如果要实现文件的上传,只要在 spring-mvc.xml 中注册相应的 MultipartResolver 即可。
MultipartResolver 的实现类有两个:
- CommonsMultipartResolver
- StandardServletMultipartResolver
两个的区别:
- 第一个需要使用 Apache 的 commons-fileupload 等 jar 包支持,但它能在比较旧的 servlet 版本中使用。
- 第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,但是只能在 Servlet 3 以上的版本使用。
第一个使用步骤:
/*CommonsMultipartResolver 上传用到的两个包*/
"commons-fileupload:commons-fileupload:1.3.1",
"commons-io:commons-io:2.4"
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
第二个:
<!--2 注册上传 StandardServletMultipartResolver 第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能, 但是只能在 Servlet 3 以上的版本使用。 -->
springmvc的@Validated注解使用
3. 在需要校验的pojo前边添加@Validated,在需要校验的pojo后边添加BindingResult bindingResult接收校验出错信息
注意:@Validated和BindingResult bindingResult是配对出现,并且形参顺序是固定的(一前一后)。
文件上传:
1.在xxx-servlet.xml文件中配置,spring mvc文件上传类bean配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
2.新建一个FileModel类,继承org.springframework.web.multipart.MultipartFile文件上传
import org.springframework.web.multipart.MultipartFile; public class FileModel { private MultipartFile file; public MultipartFile getFile() { return file; } public void setFile(MultipartFile file) { this.file = file; } }
3.新建模板文件
file.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <title>Spring MVC上传文件示例</title> </head> <body> <form:form method="POST" modelAttribute="fileUpload" enctype="multipart/form-data"> 请选择一个文件上传 : <input type="file" name="file" /> <input type="submit" value="提交上传" /> </form:form> </body> </html>
上传结果页
file_result.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <html> <head> <title>Spring MVC上传文件示例</title> </head> <body> 文件名称 : <b> ${fileName} </b> - 上传成功! </body> </html>
4.上传程序
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.util.FileCopyUtils; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; @Controller public class FileUploadController { @Autowired ServletContext context; @RequestMapping(value = "/fileUploadPage", method = RequestMethod.GET) public ModelAndView fileUploadPage() { FileModel file = new FileModel(); ModelAndView modelAndView = new ModelAndView("fileUpload", "command", file); return modelAndView; } @RequestMapping(value="/fileUploadPage", method = RequestMethod.POST) public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model) throws IOException { if (result.hasErrors()) { System.out.println("validation errors"); return "fileUploadPage"; } else { System.out.println("Fetching file"); MultipartFile multipartFile = file.getFile(); String uploadPath = context.getRealPath("") + File.separator + "temp" + File.separator; //Now do something with file... FileCopyUtils.copy(file.getFile().getBytes(), new File(uploadPath+file.getFile().getOriginalFilename())); String fileName = multipartFile.getOriginalFilename(); model.addAttribute("fileName", fileName); return "success"; } } }
其中public ModelAndView fileUploadPage()代码块中的fileUpload跟file.jsp中的fileUpload是配套的:modelAttribute="fileUpload"
其中public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model)代码块中的@Validated 是用来验证数据的,验证数据跟BindingResult配套使用。FileModel file 是file.jsp中提交上来的file(fileUpload)