I am trying to create a upload form using servlets, and so far i have the following;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRealPath("");
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
System.out.println(uploadedFile.getAbsolutePath());
item.write(uploadedFile);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
It seems to upload the files fine, however it always throws a java.io.FileNotFoundException:
Have i done something wrong?
ERROR:
/Applications/Tomcat/apache-tomcat-6.0.35/wtpwebapps/work/uploads/texsxxdddst.xml
/Applications/Tomcat/apache-tomcat-6.0.35/wtpwebapps/work/uploads
java.io.FileNotFoundException: /Applications/Tomcat/apache-tomcat-6.0.35/wtpwebapps/work/uploads (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:145)
at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:426)
at servlets.uploadServlet.doPost(uploadServlet.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:680)
My guess is that the directory where you want to write files is write-protected, or that you don’t have the rights to write there. You shouldn’t write these files in this directory anyway, because it will be deleted if you undeploy or redeploy the webapp. Consider that Tomcat’s directory is under Tomcat’s responsibility.
Choose a directory, outside of Tomcat, where you want to store your uploads, and configure your webapp with a parameter containing this path. Use this parameter in your code to get the path and write the uploaded files.