Springmvc File Upload

Source: Internet
Author: User

SPRINGMVC default parser inside is not added to the file upload parsing, use Springmvc file upload parser to handle file upload need to use SPRINGMVC provide multipartresolver declaration, And because Commonsmultipartresolver implements the Multipartresolver interface, we can configure this in the SPRINGMVC configuration file:

XML code
  1. <Bean id="Multipartresolver"
  2. class="Org.springframework.web.multipart.commons.CommonsMultipartResolver">
  3. <property name= "defaultencoding" value="utf-8" />
  4. <property name= "maxuploadsize" value="10485760000" />
  5. <property name= "maxinmemorysize" value="40960" />
  6. </Bean>

First introduce the file upload required package, Commons-logging-*.jar Commons-io-*.jar Commons-fileupload-*.jar

Create a new JSP page.

HTML code
  1. <%@ page language="java" contenttype="text/html; Charset=utf-8 "
  2. pageencoding="UTF-8"%>
  3. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  7. <title> File Upload </title>
  8. </head>
  9. <body>
  10. <%--<form action="user/fileupload" method="POST" enctype=" Multipart/form-data ">--%>
  11. <form action="user/fileupload" method="POST" enctype="Multipart/form-data " >
  12. <input type="file" name="fileUpload" />
  13. <input type="Submit" value= "upload" />
  14. </form>
  15. </body>
  16. </html>

Springmvc upload the file form There are many, here I introduce two kinds.

First, look at the controller.

Java code
  1. Package Gd.hz.springmvc.controller;
  2. Import Java.io.File;
  3. Import java.io.IOException;
  4. Import Org.springframework.stereotype.Controller;
  5. Import org.springframework.web.bind.annotation.RequestMapping;
  6. Import Org.springframework.web.bind.annotation.RequestMethod;
  7. Import Org.springframework.web.bind.annotation.RequestParam;
  8. Import Org.springframework.web.multipart.commons.CommonsMultipartFile;
  9. Import Org.springframework.web.servlet.ModelAndView;
  10. @Controller ("Usercontroller")
  11. @RequestMapping ("user")
  12. Public class Usercontroller {
  13. //process file upload one
  14. @RequestMapping (value = "FileUpload", method = Requestmethod.post)
  15. Public Modelandview FileUpload (
  16. @RequestParam ("fileUpload") commonsmultipartfile file) {
  17. //Get file type
  18. System.out.println (File.getcontenttype ());
  19. //Get file size
  20. System.out.println (File.getsize ());
  21. //Get file name
  22. System.out.println (File.getoriginalfilename ());
  23. //Determine if the file exists
  24. if (!file.isempty ()) {
  25. String path = "d:/" + file.getoriginalfilename ();
  26. File LocalFile = new file (path);
  27. try {
  28. File.transferto (LocalFile);
  29. } catch (IllegalStateException e) {
  30. E.printstacktrace ();
  31. } catch (IOException e) {
  32. E.printstacktrace ();
  33. }
  34. }
  35. return new Modelandview ("datasuccess");
  36. }
  37. }

Class Commonsmultipartfile provides us with a number of methods for handling files. For example, file size, upload file name, file type, specific usage you can view the spring documentation. Transferto is the output of the file to the specified place.

File upload the second method, this method is more commonly used:

Java code
  1. Package Gd.hz.springmvc.controller;
  2. Import Java.io.File;
  3. Import java.io.IOException;
  4. Import Java.util.Iterator;
  5. Import Javax.servlet.http.HttpServletRequest;
  6. Import Org.springframework.stereotype.Controller;
  7. Import org.springframework.web.bind.annotation.RequestMapping;
  8. Import Org.springframework.web.bind.annotation.RequestMethod;
  9. Import Org.springframework.web.multipart.MultipartFile;
  10. Import Org.springframework.web.multipart.MultipartHttpServletRequest;
  11. Import Org.springframework.web.multipart.commons.CommonsMultipartResolver;
  12. @Controller ("Usercontroller")
  13. @RequestMapping ("user")
  14. Public class Usercontroller {
  15. //process file upload two
  16. @RequestMapping (value = "FileUpload2", method = Requestmethod.post)
  17. Public String fileUpload2 (httpservletrequest request)
  18. throws IllegalStateException, IOException {
  19. //Set up and down Fang Wen
  20. Commonsmultipartresolver multipartresolver = new Commonsmultipartresolver (
  21. Request.getsession (). Getservletcontext ());
  22. //Check if the form has enctype= "Multipart/form-data"
  23. if (Multipartresolver.ismultipart (Request)) {
  24. Multiparthttpservletrequest multirequest = (multiparthttpservletrequest) request;
  25. Iterator<string> iter = Multirequest.getfilenames ();
  26. While (Iter.hasnext ()) {
  27. //inherited by Commonsmultipartfile, with the method above.
  28. Multipartfile file = Multirequest.getfile (Iter.next ());
  29. if (file! = null) {
  30. String fileName = "Demoupload" + file.getoriginalfilename ();
  31. String path = "d:/" + fileName;
  32. File LocalFile = new file (path);
  33. File.transferto (LocalFile);
  34. }
  35. }
  36. }
  37. return "datasuccess";
  38. }
  39. }

Multiparthttpservletrequest provides a more flexible way to get multiple files and file names that can be traversed to get each file.

Springmvc 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.