Hi all,
I am using FileUpload from apache.commons for the first time. I am using
multipart/form-data with a servlet.
I have tried everything exactly like the examples but for some reason, fail
at fileItems = upload.parseRequest(req) and get the FileUploadException.
The classpath includes these jars:
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
The servlet includes the following imports:
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
My html and servlet code is attached.
At the end of a failed run, my logs has the following messages:
***Request is multipart
****failed to load items from req
<!-- <html> -->
(javascript function)
function doUpload(){
........
document.uploadForm.target = "rightPanel";
window.setTimeout('document.uploadForm.submit()',500);
......
}
<form name="uploadForm" action="/it0030/uploadDoc"
enctype="multipart/form-data" method="post">
<table border='0' cellspacing="0" cellpadding="2" width="100%">
<tr>
<td align="left" width="40%">
<p>
Please select a file with one of these file extension types: .doc,
.pdf, or .xls <br>
<input type="file" name="datafile" size=80>
</p>
<input type="radio" name="doctype" value="N" CHECKED>Pricing
News
<input type="radio" name="doctype" value="G">Pricing
Guidelines
<p> <%uploadMsg%></p>
<input type="submit" name = "Submit" value="Upload Files"
onclick="doUpload();">
</td>
</tr>
</table>
</form>
//servlet code
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
if (! Util.ckSession(req))
{
res.sendRedirect("common/session_timeout.htm");
}
if(isMultiPart(req)) {
log.debug("***Request is multipart");
}
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
factory.setRepository(new File("/tmp"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1000000);
List fileItems = null;
try {
fileItems = upload.parseRequest(req);
} catch (FileUploadException e) {
log.debug("****failed to load items from req");
e.printStackTrace();
}
Iterator itr = fileItems.iterator();
while(itr.hasNext()) {
FileItem fi = (FileItem)itr.next();
if(fi.isFormField()) {
String fieldName = fi.getFieldName();
}
else {
File fullFile = new
File(getServletContext().getRealPath("/"), fi.getName());
inFile = fi.getName();
try {
fi.write(fullFile);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintWriter out = res.getWriter();
out.println("saved file = " + fullFile.getName());
}
}
}
Thanks in advance for your help and suggestions.
Thanks
B
--
View this message in context:
http://www.nabble.com/FileUpload-fails-at-parseRequest-tp16624711p16624711.html
Sent from the Commons - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]