If you do not know multipartresolver or servletfileupload, click here
If both multipartresolver and servletfileupload are used, false will be returned in ITER. hasnext (). Then the entire loop will jump out.
if (isMultipart) { DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 1024 * 20, null); ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("UTF-8"); upload.setSizeMax(1024 * 1024 * 20); List<FileItem> fileItems = upload.parseRequest(req); Iterator<FileItem> iter = fileItems.iterator();
// No fileitem while (ITER. hasnext () {fileitem item = (fileitem) ITER. next (); If (item. isformfield () {string name = item. getfieldname (); string value = item. getstring ("UTF-8"); Request. put (name, value);} else {byte [] filebytes = item. get (); If (stringutils. isblank (item. getname () {continue;} request. put (item. getfieldname (), new string (filebyte, "UTF-8 "));}}}}
The cause of the problem is that the Spring framework first calls multipartresolver to process HTTP multi-part requests. Here, the HTTP multipart request has been consumed. After being handed over to servletfileupload, servletfileupload will not be able to obtain the corresponding multi-part request.
If you want to solve this problem quickly before accounting. The solution is to add the multipartresolver before the account to process some requests separately. These requests require special judgment. For example, the following code:
Package example; import javax. servlet. HTTP. httpservletrequest; import Org. springframework. web. multipart. commons. expiration;/*** @ author account pawn */public class mymultipartresolver extends {private string excludeurls; private string [] excludeurlarray; Public String getexcludeurls () {return excludeurls ;} public void setexcludeurls (string excludeurls) {This. exclude URLs = excludeurls; this. excludeurlarray = excludeurls. Split (",") ;}/ *** here is the method for processing multipart HTTP. If the returned value is true, the multipart HTTP body will be consumed by mymultipartresolver. if "false" is returned here, the processing function written by myself will be handed over to the subsequent handler, for example, the function where the servletfileupload is located * @ see Org. springframework. web. multipart. commons. commonsmultipartresolver # ismultipart (javax. servlet. HTTP. httpservletrequest) * // @ override public Boolean ismultipart (httpservletrequest request) {for (string URL: excludeurlarray ){
// Here, you can determine if (request. getrequesturi (). Contains (URL) {return false ;}} return Super. ismultipart (request );}}
Write in Web. XML as follows:
<Bean id = "multipartresolver" class = "example. mymultipartresolver"> <property name = "excludeurls" value = "example"/> <! -- HTTP requests with example in the URL will not be parsed by multipartresolver --> </bean>