Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Look if the following code helps you. This is just an example. You may have to tweak it</p> <p>Create a class called <code>FileUploader</code> which returns <code>ServletFileUpload</code> object</p> <pre><code>public class FileUploader { private static ServletFileUpload uploader; private FileUploader() { } public static synchronized ServletFileUpload getservletFileUploader(String tempDir, int maxSizeInMB) { if(uploader == null) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1024 * 1024); factory.setRepository(new File(tempDir)); uploader = new ServletFileUpload(factory); uploader.setFileSizeMax(maxSizeInMB * 1024 * 1024); } return uploader; } } </code></pre> <p>Now you can process a request and read all the data </p> <pre><code> protected MultiPartFormData handleMultiPartRequest(HttpServletRequest request) throws FileSizeLimitExceededException { if(!isMultipartRequest(request)) return null; ServletFileUpload upload = FileUploader.getservletFileUploader(tempDir, 50); MultiPartFormData data = new MultiPartFormData(); try { List&lt;FileItem&gt; items = upload.parseRequest(request); for (FileItem item : items) { if(item.isFormField()) { data.getParameters().put(item.getFieldName(), item.getString()); } else { String filename = item.getName(); //Internet explorer and firefox will send the file name differently //Internet explorer will send the entire path to the file name including //the backslash characters etc ... we should strip it down //THIS IS HACKY if(filename.indexOf("\\") != -1) { int index = filename.lastIndexOf("\\"); filename = filename.substring(index + 1); } if(filename == null || filename.equals("")) { //do nothing } else { File uploadFile = new File(uploadDir + File.separator + randomFileName); item.write(uploadFile); data.addFile(item.getFieldname(), item.getString()); } } } } catch(FileSizeLimitExceededException e) { throw e; } catch(Exception e) { e.printStackTrace(); } return data; } </code></pre> <p>After parsing the request I am storing it in some object called MultipartFormData which can be used to get request parameters</p> <pre><code>public class MultiPartFormData { private Hashtable&lt;String, String&gt; parameters; private Hashtable&lt;String, String&gt; uploadedFiles; public MultiPartFormData() { this.parameters = new Hashtable&lt;String, String&gt;(); this.uploadedFiles = new Hashtable&lt;String, String&gt;(); } public Hashtable&lt;String, String&gt; getParameters() { return parameters; } public void setParameters(Hashtable&lt;String, String&gt; parameters) { this.parameters = parameters; } public void getParameter(String paramName) { if(this.parameters.contains(paramName)) return tyhis.parameters.get(paramName); return null; } public void addFile(String key, String filename) { uploadedFile.put(key, filename); } public void getFilename(String key) { uploadedFile.get(key); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload