Servletfileupload parsing to NULL in SPRINGMVC

Source: Internet
Author: User

cause Analysis

First, let's look at the configuration of file uploads in spring MVC.

<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name= "defaultencoding" value= "UTF-8"/><property name= "maxuploadsize" value= "2000000000"/> </bean>


Let's take a look at the controller use

public void Upload2 (HttpServletRequest request) {//transition to Multiparthttprequesttry {multiparthttpservletrequest Multipartrequest = (multiparthttpservletrequest) request; list<multipartfile> fileList = multipartrequest.getfiles ("file"); for (Multipartfile mf:filelist) {if (! Mf.isempty ()) {}}} catch (Exception e) {e.printstacktrace ();}}

Way Two

Public String upload (httpservletrequest request, @RequestParam (value = "file") multipartfile[] files) {try {for ( Multipartfile mf:files) {if (!mf.isempty ()) {}}} catch (Exception e) {e.printstacktrace ();} return "Upload";}


Here Springmvc all for us to encapsulate into their own file object, the conversion process is in our configuration of Commonsmultipartresolver this converter inside the next look at its source


His converter inside is called Common-fileupload parsing, and then use the Parsefileitems () method to encapsulate their own file objects.

List<fileitem> Fileitems = ((servletfileupload) fileUpload). Parserequest (Request);


We should have found the above code, has used FileUpload resolved request, you in controller inside received request is already resolved, you use upload to parse to get is definitely empty, this is the problem ( You can experiment in the servlet to see if you can get the data after the second parse, of course not.


Solution Solutions

1) Remove the spring MVC file upload configuration

<!--<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver "><property name=" defaultencoding "value=" UTF-8 "/><property name=" maxuploadsize "value=" 2000000000 "/ ></bean>

Within the controller to complete the request of the resolution (of course, the above spring MVC provides two methods are not available, all the uploaded places need to do their own processing)

public void Upload3 (HttpServletRequest request) {Diskfileitemfactory factory = new Diskfileitemfactory (); Servletfileupload upload = new Servletfileupload (factory); try {list<fileitem> List = upload.parserequest (Request ); for (Fileitem item:list) {if (Item.isformfield ()) {}else{//item.write (New File (""));}}} catch (Fileuploadexception e) {e.printstacktrace ();}}

2) if it is necessary to use the Progresslistener listener we can rewrite the Commonsmultipartresolver parserequest method

package com.lwp.spring.ext;import java.util.list;import javax.servlet.http.httpservletrequest; Import org.apache.commons.fileupload.fileitem;import org.apache.commons.fileupload.fileupload;import  org.apache.commons.fileupload.FileUploadBase;import  org.apache.commons.fileupload.fileuploadexception;import  org.apache.commons.fileupload.servlet.servletfileupload;import  org.springframework.web.multipart.maxuploadsizeexceededexception;import  org.springframework.web.multipart.multipartexception;import  org.springframework.web.multipart.commons.commonsmultipartresolver;import  com.lwp.listener.fileuploadlistener;public class commonsmultipartresolverext extends  commonsmultipartresolver {@Overrideprotected  multipartparsingresult parserequest ( Httpservletrequest request) throws multipartexception {fileuploadlistener listener =  new fileuploadlistener (); String encoding&nBsp;= determineencoding (Request); Fileupload fileupload = preparefileupload (encoding); Fileupload.setprogresslistener (listener); try {list<fileitem> fileitems =  ((servletfileupload)  fileUpload). ParseRequest ( request); Return parsefileitems (fileitems, encoding);} catch  (Fileuploadbase.sizelimitexceededexception ex)  {throw new  Maxuploadsizeexceededexception (Fileupload.getsizemax (),  ex);} catch  (Fileuploadexception ex)  {throw new multipartexception ("Could not  Parse multipart servlet request ",  ex);}}}

Listener method

Import Org.apache.commons.fileupload.progresslistener;public class Fileuploadlistener implements Progresslistener {@ overridepublic void Update (Long arg0, long arg1, int arg2) {//arg0 has uploaded how many bytes//arg1 altogether how many bytes//arg2 is uploading the first few files System.out.printl N (arg0 + "\ T" + arg1 + "\ T" + arg2);}}

Config file changed to our own (the flaw in this way is that all file uploads need to be used to listener)

<bean id= "Multipartresolver" class= "com.lwp.spring.ext.CommonsMultipartResolverExt" ><property name= " Defaultencoding "value=" UTF-8 "/><property name=" maxuploadsize "value=" 2000000000 "/></bean>


Note: In summary, if only normal file upload spring MVC can complete, if you need to use the progress bar of the listener front can use the false progress bar or the above two ways.


Servletfileupload parsing to NULL in SPRINGMVC

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.