Solve the bug that the commons-fileupload component cannot process custom head information.

Source: Internet
Author: User

Solve the bug that the commons-fileupload component cannot process custom head information.

Related reading:

1. simple encapsulation of commons fileupload component 2. use the Jakarta commons fileupload component to upload multiple files. 3. the spring package for commons fileupload Jakarta commons fileupload can be used to process HTTP requests and responses. It is often used to process file uploads. However, it has recently been found that, when we add custom head nodes to custom file uploads, self-assembly mime information, and file uploads, The fileupload component cannot obtain the custom head node. After carefully analyzing the source code of the fileupload component, the core method is found in the findnextitem method of the fileuploadbase file. The problem is that after the fileupload component parses the custom head node, it forgot to pass it to fileitemstreamimpl, this bug can be fixed. /** Resolution file list <br/> * called for finding the NEX item, if any. <br/> * @ return true, if an next item was found, otherwise false. <br/> * @ throws ioexception an I/O error occurred. <br/> */<br/> private Boolean findnextitem () throws ioexception {<br/> If (EOF) {<br/> return false; <br/>}< br/> If (currentitem! = NULL) {<br/> currentitem. close (); <br/> currentitem = NULL; <br/>}< br/> for (;) {<br/> Boolean nextpart; <br/> If (skippreamble) {<br/> nextpart = multi. skippreamble (); <br/>}else {<br/> nextpart = multi. readboundary (); <br/>}< br/> If (! Nextpart) {<br/> If (currentfieldname = NULL) {<br/> // outer multipart terminated-> no more data <br/> EOF = true; <br/> return false; <br/>}< br/> // inner multipart terminated-> return to parsing the outer <br/> multi. setboundary (Boundary); <br/> currentfieldname = NULL; <br/> continue; <br/>}< br/> fileitemheaders headers = getparsedheaders (multi. readheaders (); <br/> If (currentfieldna m E = NULL) {<br/> // We're parsing the outer multipart <br/> string fieldname = getfieldname (headers); <br/> If (fieldname! = NULL) {<br/> string subcontenttype = headers. getheader (content_type); <br/> If (subcontenttype! = NULL <br/> & subcontenttype. tolowercase () <br/>. startswith (multipart_mixed) {<br/> currentfieldname = fieldname; <br/> // multiple files associated with this field name <br/> byte [] subboundary = getboundary (subcontenttype); <br/> multi. setboundary (subboundary); <br/> skippreamble = true; <br/> continue; <br/>}< br/> string filename = getfilename (headers ); <br/> currentitem = new fileitemstr Eamimpl (filename, <br/> fieldname, headers. getheader (content_type), <br/> filename = NULL, getcontentlength (headers); <br/> notifier. noteitem (); <br/> itemvalid = true; <br/> return true; <br/>}< br/>} else {<br/> string filename = getfilename (headers); <br/> If (filename! = NULL) {<br/> // here the code is to be revised <br/> // This is the original code, no header is input <br/> // currentitem = new fileitemstreamimpl (filename, currentfieldname, headers. getheader (content_type), false, getcontentlength (headers); <br/> // This is the new code. We need to pass in the header <br/> currentitem = new fileitemstreamimpl (filename, currentfieldname, headers. getheader (content_type), false, getcontentlength (headers), headers); <br/> notifier. noteitem (); <br/> item Valid = true; <br/> return true; <br/>}< br/> multi. discardbodydata (); <br/>}</P> <p>/** original code, there is a bug where the fileitemheaders information is lost <br/> * creates a new instance. <br/> * @ Param pname the items file name, or null. <br/> * @ Param pfieldname the items field name. <br/> * @ Param pcontenttype the items content type, or null. <br/> * @ Param pformfield whether the item is a form field. <br/> * @ Pa Ram pcontentlength the items content length, if known, or-1 <br/> * @ throws ioexception creating the file item failed. <br/> */<br/> fileitemstreamimpl (string pname, string pfieldname, <br/> string pcontenttype, Boolean pformfield, <br/> long pcontentlength) throws ioexception {<br/> name = pname; <br/> fieldname = pfieldname; <br/> contenttype = pcontenttype; <br/> formfield = pformfield; <br/> Final iteminputstream itemstream = multi. newinputstream (); <br/> inputstream istream = itemstream; <br/> If (filesizemax! =-1) {<br/> If (pcontentlength! =-1 <br/> & pcontentlength> filesizemax) {<br/> fileuploadexception E = <br/> New filesizelimitexceededexception (<br/> "the field" + fieldname <br/> + "exceeds its maximum permitted" <br/> + "size of" + filesizemax <br/> + "characters. ", <br/> pcontentlength, filesizemax); <br/> throw new fileuploadioexception (E); <br/>}< br/> istream = new limitedinputstream (istream, filesizemax) {<br /> Protected void raiseerror (long psizemax, long pcount) <br/> throws ioexception {<br/> itemstream. close (true ); <br/> fileuploadexception E = <br/> New filesizelimitexceededexception (<br/> "the field" + fieldname <br/> + "exceeds its maximum permitted" <br/> + "size of" + psizemax <br/> + "characters. ", <br/> pcount, psizemax); <br/> throw new fileuploadioexception (E); <br/>}< br/>}; <br />}< Br/> stream = istream; <br/>}</P> <p>/** create a fileitem and the revised code, fixed the bug where fileitemheaders information was lost. <br/> * @ Param pname <br/> * @ Param pfieldname <br/> * @ Param pcontenttype <br/> * @ Param pformfield <br /> * @ Param pcontentlength <br/> * @ Param headers <br/> * @ throws ioexception <br/> */<br/> fileitemstreamimpl (string pname, string pfieldname, <br/> string pcontenttype, Boolean pformfield, <br/> long P Contentlength, fileitemheaders headers) throws ioexception {<br/> name = pname; <br/> fieldname = pfieldname; <br/> contenttype = pcontenttype; <br/> formfield = pformfield; <br/> If (headers! = NULL) {<br/> This. headers = headers; <br/>}< br/> final iteminputstream itemstream = multi. newinputstream (); <br/> inputstream istream = itemstream; <br/> If (filesizemax! =-1) {<br/> If (pcontentlength! =-1 <br/> & pcontentlength> filesizemax) {<br/> fileuploadexception E = <br/> New filesizelimitexceededexception (<br/> "the field" + fieldname <br/> + "exceeds its maximum permitted" <br/> + "size of" + filesizemax <br/> + "characters. ", <br/> pcontentlength, filesizemax); <br/> throw new fileuploadioexception (E); <br/>}< br/> istream = new limitedinputstream (istream, filesizemax) {<br/> protected void raiseerror (long psizemax, long pcount) <br/> throws ioexception {<br/> itemstream. close (true ); <br/> fileuploadexception E = <br/> New filesizelimitexceededexception (<br/> "the field" + fieldname <br/> + "exceeds its maximum permitted" <br/> + "size of" + psizemax <br/> + "characters. ", <br/> pcount, psizemax); <br/> throw new fileuploadioexception (E); <br/>}< br/> }; <br/>}< br/> stream = istream; <br/>} 

 

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.