Hello all, this is my first post here to here goes...
On my index.class servlet I have the following code:
else if(ServletFileUpload.isMultipartContent(request)){ nwg = new NWG(session.getAttribute("authenticated")!=null?session.getAttribute("authenticated").toString():null); Map<String, String> formData = this.formDataToHashMap(request); FileUploader file = new FileUploader(request); file.saveToRelativePath("users\\evo4ever\\images\\"); String message = null; if(file.upload()) message = "File uploaded "; FormController doForm = new FormController(formData.get("FORM_SUBMIT"), formData); doForm.SwitchFormProcess(); request.setAttribute("message", message+doForm.getMessage()); request.setAttribute("nwg", nwg); request.getRequestDispatcher("index.jsp").forward(request, response); }
the this.formDataToHashMap(request) method is as follows:
private Map<String, String> formDataToHashMap(HttpServletRequest request) { Map<String, String> formData = new HashMap<String, String>(); if(ServletFileUpload.isMultipartContent(request)) { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List fileItems = upload.parseRequest(request); Iterator i = fileItems.iterator(); while(i.hasNext()) { FileItem fi = (FileItem)i.next(); if(fi.isFormField()) { formData.put(fi.getFieldName(), fi.getString()); } } } catch(Exception e) { return null; } } else { Enumeration params = request.getParameterNames(); while(params.hasMoreElements()) { String paramName = (String)params.nextElement(); if(!paramName.equals("FORM_SUBMIT")) { formData.put(paramName, request.getParameter(paramName)); } } } return formData; }
And my FileUploader.class code:
package evo.net; import java.io.*; import java.util.*; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.output.*; public class FileUploader { private String path = "D:\\Apache Software Foundation\\Tomcat 7.0\\webapps\\ROOT\\"; private String saveToDir = null; private int maxFileSize = 2147483500; private int maxMemSize = 4 * 1024; private File file; private HttpServletRequest request; private String message; public FileUploader(HttpServletRequest request) { this.request = request; } public void setMaxFileSize(int size) { this.maxFileSize = size; } public void setMaxMemSize(int size) { this.maxMemSize = size; } public void saveToRelativePath(String path) { this.saveToDir = this.path+path; } public void saveToAbsolutePath(String path) { this.path = path; } public String getMessage() { return this.message; } public boolean upload() { String saveTo = null; if(this.saveToDir!=null) saveTo = this.saveToDir; else saveTo = this.path; if(ServletFileUpload.isMultipartContent(this.request)) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(this.maxMemSize); factory.setRepository(new File(this.path)); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(this.maxFileSize); try { List fileItems = upload.parseRequest(this.request); Iterator i = fileItems.iterator(); while(i.hasNext()) { FileItem fi = (FileItem)i.next(); if(!fi.isFormField()) { String fieldName = fi.getFieldName(); String fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); if(fileName.lastIndexOf("\\") >= 0 ) { file = new File(saveTo + fileName.substring(fileName.lastIndexOf("\\"))) ; } else { file = new File(saveTo + fileName.substring(fileName.lastIndexOf("\\")+1)) ; } fi.write(file); } } } catch(Exception ex) { this.message = " File Upload Error: "+ex.getMessage(); return false; } } return true; } }
When I create an object of FileUploader in my index servlet the file wont upload which throws no exceptions, and nor do does it have any compile time errors.
However, If I create a test servlet called fupload, code as follows:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); if(ServletFileUpload.isMultipartContent(request)) { FileUploader file = new FileUploader(request); file.saveToRelativePath("users\\evo4ever\\images\\"); if(file.upload()) out.println("File successfully uploaded."); else out.println(file.getMessage()); } else out.println("No multipart content!"); }
The file will upload as expected with no errors or exceptions of any kind. The problem is why would it work in one servlet (fuploader) and not in another (index servlet)? Thats what I cant get my head around because clearly the FileUploader works but only when it feels like lol.
I'm suspecting its not working because ive already processed the request object. So I change the index.class servlet for experimental purposes to this:
else if(ServletFileUpload.isMultipartContent(request)){ nwg = new NWG(session.getAttribute("authenticated")!=null?session.getAttribute("authenticated").toString():null); //Map<String, String> formData = this.formDataToHashMap(request); String message = null; FileUploader file = new FileUploader(request); file.saveToRelativePath("users\\evo4ever\\images\\"); if(file.upload()) message = "File successfully uploaded."; else message = file.getMessage(); //FormController doForm = new FormController(formData.get("FORM_SUBMIT"), formData); //doForm.SwitchFormProcess(); request.setAttribute("message", message); request.setAttribute("nwg", nwg); request.getRequestDispatcher("index.jsp").forward(request, response); }
As you can see ive commented out the FormController object and now the file WILL upload! Any explanations as to why its doing this? Is there anyway of creating two separate request objects? one for processing the form data and one for processing the file/s? Cheers guys and this is ripping my hair out lol.