SpringMVC uploads files using FileUpload and springmvc files

Source: Internet
Author: User

SpringMVC uploads files using FileUpload and springmvc files

Import FileUpload and common-io Jar packages

Note: 1. Struts2 requires other methods: When struts2 is used for filtering, the reqeust type is changed from HttpServletRequest to MultiPartRequestWrapper.

1. Test. jsp:

<form action="<%=basePath%>self/testFileUpload.do" method="post" enctype="multipart/form-data">    <input type="text" name="num">    <input name="img1" type="file" accept="image/gif,image/jpg,image/png,image/jpeg"/>    <input name="img2" type="file" accept="image/gif,image/jpg,image/png,image/jpeg"/>    <input type="submit" name="test">    <input type="reset" name="test1">    </form>

2. controller:

@ RequestMapping (value = "/testFileUpload") public void testFileUpload (HttpServletRequest request, HttpServletResponse response) throws Exception {String num = ""; String img1 = ""; string img2 = ""; String fileName = ""; try {String t = request. getCharacterEncoding (); boolean isMultipart = ServletFileUpload. isMultipartContent (request); // determines whether it is a Form file type. if (isMultipart) {DiskFileItemFactory factory = new DiskFileIte MFactory (); ServletFileUpload upload = new ServletFileUpload (factory); upload. setHeaderEncoding (request. getCharacterEncoding (); // It is very important to solve the problem of garbled file names uploaded in linux. // upload. setFileSizeMax (3*1024*1024); // 3 MList items = upload. parseRequest (request); if (items. size () = 0) {System. out. println ("no data");} Iterator <Object> formItem = items. iterator (); while (formItem. hasNext () {FileItem item = (FileItem) formItem. next (); // If it is a form element if (item. isFormField () {if (item. getFieldName (). equals ("num") {num = item. getString (request. getCharacterEncoding ()). trim () ;}}// if it is not a form element, if (! Item. isFormField () {String name = item. getName (); String type = item. getContentType (); long sizeInBytes = item. getSize (); try {String fullName = item. getName (). trim (); if (fullName! = "") {FileName = System. currentTimeMillis () + fullName. substring (fullName. indexOf (". "); if (item. getFieldName (). equals ("img1") {fileName = "img1-" + System. currentTimeMillis () + fullName. substring (fullName. indexOf (". ");} if (item. getFieldName (). equals ("img2") {fileName = "img2-" + System. currentTimeMillis () + fullName. substring (fullName. indexOf (". ");} String tomcatRoot = FileOperator. getRootPath (); String path = tomcatRoot; if (FileOperator. fileUploadEx (path, fileName, item. getSize (), item. getInputStream () {if (item. getFieldName (). equals ("img1") {System. out. println ("testimg1");} if (item. getFieldName (). equals ("img2") {System. out. println ("testimg2") ;}}} catch (Exception e) {e. printStackTrace () ;}}} else {System. out. println ("non-multipart/form-data submission method") ;}} catch (Exception e) {e. printStackTrace ();}}



SpringMVC uses commons fileupload to upload files. The set obtained by parseRequest (request) is empty. Please wait for a while.

Now that you have used Spring MVC, there is no need to write the uploaded code by yourself.
Next, upload the MultipartFile
Public String upload (@ RequestParam (value = "image") MultipartFile image)
Then you can do this.
Image. transferTo (new File ("where to save "));

There are other methods for image. Just take a look.

Configure a bean in XML

<Bean id = "multipartResolver"
Class = "org. springframework. web. multipart. commons. CommonsMultipartResolver">
<Property name = "maxUploadSize" value = "104857600"/>
<Property name = "maxInMemorySize" value = "4096"/>
<Property name = "defaultEncoding" value = "UTF-8"/>
</Bean>
 
A spring mvc implements a file upload instance. If you have an example, you 'd better have a comment.

Is Spring used? If you don't just find an upload control, such as COS, it's easy to do. You have a lot of Baidu...

Below we use Html5 multi-File Upload in our system




Related Article

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.