My JSP page like this:
$(function() {
$("#file_upload").uploadify({
'height': 27,
'width': 80,
'buttonText':'浏览',
'swf':'<%=basePath%>admin/tupian/js/uploadify.swf',
'uploader': '<%=basePath%>Imguploadoper.img',
'auto' : false,
'fileTypeExts' : '*.jpg'
});
});
Here is my java code:
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
try {
//this line returns null
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
......
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.flush();
out.close();
upload.parseRequest(request)
returns null
. I really don't know the reason.
This is a common mistake when uploading in Struts2. You shouldn't parse the request in the action. I believe that you've written the java code in the action. So, Struts2 handles multipart request via the
MultipartRequestWrapper
, which is using the configuration constantthat corresponds to the multipart request adapter
JakartaMultiPartRequest
, used to parse the request and put files to the location defined by this constantstruts.multipart.saveDir
, if this constant isn't set thenjavax.servlet.context.tempdir
used by default.You can get
MultipartRequestWrapper
usingServletActionContext
, see How do we upload files.Then
fileUpload
interceptor, which is a part of thedefaultStack
, using the maltipart request get all accepted files, accepted file names and accepted content types and put them to the action context.Then
params
interceptor, which is a part of thedefaultStack
, using that action context params, put them to the action properties.When multipart request wrapped, which is done by the
Dispatcher
, and parsed when wrapper is instantiated, you can check files in thesaveDir
, if uploading finished without errors.To perform file uploading make sure you submit the multipart request, i.e the form
enctype
attribute is"multipart/form-data"
and interceptors are applied to the action explicitly referencing them or implicitly usingdefaultStack
of interceptors. In the action create properties with getters/setters for file names, content types, and files. If your uploads are succeeded then check your files in the action properties.To learn more you can exercise these examples: