"Spring Learning note-mvc-13" Spring MVC file Upload

Source: Internet
Author: User

SSSLINPPP 1. Summary Spring MVC provides the most direct support for file uploads, which is implemented through plug-and-play multipartresolve. Spring uses Jakarta Commons FileUpload technology to implement a Multipartresolver implementation class: Commonsmultipartresolver.
The following is a detailed explanation of how spring MVC implements file uploads. 2. Add a jar package to upload a Spring mvc file, you need to add the following two jar packages:
    1. Commons-fileupload-1.2.2.jar;
    2. Commons-io-2.0.1.jar
3. Configure Commonsmultipartresolver
  
 
  1. <!-- 文件上传 -->
  2. <bean id="multipartResolver"
  3. class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
  4. p:defaultEncoding="UTF-8" p:maxUploadSize="5000000" p:uploadTempDir="upload/temp" />
Description
    • P:defaultencoding= "UTF-8": here Set the default file encoding to UTF-8, must be consistent with the default encoding of the user JSP;
    • P:maxuploadsize= "5000000": Specify the file upload size, in bytes;
    • P:uploadtempdir= "upload/temp": File upload temporary directory, upload completed, the temporary files will be deleted;

4. Control Layer Code Front Desk Request: Http://localhost:8080/SpringMVCTest/test/uploadPage.action, return to the Uploadpage.jsp interface, as follows:

  
 
  1. @RequestMapping(value = "/upload")
  2. public String updateThumb(@RequestParam("name") String name,
  3. @RequestParam("file") MultipartFile file,
  4. HttpServletRequest request, ModelMap model) throws Exception {
  5. if (!file.isEmpty()) {
  6. // 保存文件-方式1 --测试过,可以用,必须先创建相应目录
  7. // file.transferTo(new File("d:/"+file.getOriginalFilename()));
  8. // 保存文件-方式2
  9. String path = request.getSession().getServletContext()
  10. .getRealPath("upload");
  11. String fileName = file.getOriginalFilename();
  12. File targetFile = new File(path, fileName);
  13. //目录不存在,则创建目录
  14. if(!targetFile.exists()){
  15. targetFile.mkdirs();
  16. }
  17. //保存
  18. try {
  19. file.transferTo(targetFile);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);
  24. return "success";
  25. } else {
  26. return "fail";
  27. }
  28. }

5. File Upload JSP
  
 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <title>请上传用户头像</title>
  5. <body>
  6. 请选择上传的头像文件
  7. <form method="post" action="<c:url value="/test/upload.action"/>"
  8. enctype="multipart/form-data">
  9. <input type="text" name="name" />
  10. <input type="file" name="file" />
  11. <input type="submit" />
  12. </form>
  13. </body>
Successfully returned interface:


6. Blogs HTTP://WWW.CNBLOGS.COM/SSSLINPPP Http://blog.sina.com.cn/spstudy



From for notes (Wiz)

List of attachments

    "Spring Learning note-mvc-13" Spring MVC file Upload

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.