Hello people. I am using:
# Orion app server (don't ask why)
# WebWork 2.1.7 framework
# Jakarta commons file upload 1.2
While writing a file upload action I am having problems with ServletFileUpload.parseRequest(...) as it always returns an empty list.
Reading related Jakarta FAQ it says "
this most commonly happens when the request has already been parsed, or processed in some other way."
I then assumed it was my WebWork interceptor so I took that out but I'm still getting the same problem.
Am I missing something in my JSP, another <input> or something?
Is the request being parsed somehwere else beforehand (maybe a WebWork issue)?
Any help would be much appreciated.
Here is the relevant code...
My JSP<html>
<head>
<title>File Upload</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="doUpload.action" name="uploadForm">
<input name="upload" type="file" label="File"/>
<input type="submit" value="upload" />
</form>
</body>
</html>
The execute() method of my action public String execute() {
log.info("executing");
// get the request
HttpServletRequest request = ServletActionContext.getRequest();
// check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
log.debug("request "+ ( (isMultipart ? "IS" : "is NOT") ) +" a multi-part");
if (isMultipart) {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(100);
factory.setRepository(new File("C:/tmp/cms/uploads"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
try {
//List /* FileItem */ items = upload.parseRequest(request);
_uploads = upload.parseRequest(request);
} catch (FileUploadException e) {
log.error("Error parsing request" , e);
}
// if the above method doesn't work, use my method :)
if (_uploads.isEmpty()) {
_uploads = parseRequest(request);
}
}
return (_uploads == null) || _uploads.isEmpty() ? INPUT : SUCCESS;
}
My implementation of parseRequest(...) works but returns File objects and I need FileItem objects. I don't really want to use this method but will include it here just incase it triggers some inspiration for someone.
private List<File> parseRequest(HttpServletRequest requestObj) {
List<File> files = new ArrayList<File>();
// if the request is a multi-part
if (requestObj instanceof MultiPartRequestWrapper) {
// cast to multi-part request
MultiPartRequestWrapper request = (MultiPartRequestWrapper) requestObj;
// get file parameter names
Enumeration paramNames = request.getFileParameterNames();
if (paramNames != null) {
// for each parameter name, get corresponding File
while (paramNames.hasMoreElements()) {
File[] f = request.getFiles("" + paramNames.nextElement() );
// add the File to our list
if (!ArrayUtils.isEmpty(f)) {
files.add(f[0]);
}
}
}
}
return files.isEmpty() ? null : files;
}