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.