In struts, upload files in post mode. enctype = "multipart/form-data", request. getparameter ("file ")

Source: Internet
Author: User

In struts, the post method uploads files. The enctype = "multipart/form-Data" and request. getparameter ("file") are null.

In the past two days, we can see that the POST method in struts uploads files, enctype = "multipart/form-Data", request. getparameter ("file"), get a null value, or directly get Java. lang. illegalargumentexception.

I have encountered this problem before, so I searched for some materials online. I think the following comments from this netizen have some reason, so I copied them. (I think it makes sense to use red for others' analysis)

Use struts to upload files. If some parameters are not fully defined in actionform. getparameter. When the form is submitted and the validate fails to return the input page, this part needs to be obtained from the request. the parameter data obtained by getprameter is lost and cannot be obtained even if multipart Parsing is performed on the request.

After analysis, it is found that after the struts actionservlet receives the multipart request, it encapsulates the request in requestprocessor: multirequestwrapper. After the action is executed, restore the encapsulated request. The following code is used to intercept requestprocessor:

Encapsulation: the source code of Struts is as follows:

Protected httpservletrequest processmultipart (httpservletrequest request ){

If (! "Post". inclusignorecase (request. getmethod ())){
Return (request );
}

String contenttype = request. getcontenttype ();
If (contenttype! = NULL )&&
Contenttype. startswith ("multipart/form-Data ")){
Return (New multipartrequestwrapper (request ));
} Else {
Return (request );
}

}

Next, we need to restore the httpservletrequest encapsulated as multipartrequestwrapper.
Solution:
① Write a class that inherits org. Apache. Struts. Action. requestprocessor and override the doforward method to parse it.

Package org. luyang;
Import java. Io. ioexception;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import org. Apache. Struts. Upload. multipartrequestwrapper;

Public class requestprocessor extends org. Apache. Struts. Action. requestprocessor {

Protected void doforward (string Uri, httpservletrequest request, httpservletresponse response) throws ioexception, servletexception {
Httpservletrequest myrequest = request;
If (request instanceof multipartrequestwrapper ){
Myrequest = (multipartrequestwrapper) request). getrequest ();
}
Super. doforward (Uri, myrequest, response );
}
}

② Add the following sentence between your own struts-config.xml <Struts-config> </Struts-config>

<Controller processorclass = "org. luyang. requestprocessor"/>
<Message-resources parameter = "applicationresources"/>

The above are the solutions I know now. I don't know if I can solve your problems for the time being.

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.