1

I have a requirement to upload multiple files at a time and save in server. But the ServletFileUpload#parseRequest(request) method is missing the first file in request i.e., 0th index file and returning the list of files from index 1 and so on.

The request :

Request headers :

POST /restcon/fileUploadTest HTTP/1.1
Host: localhost:8089
Connection: keep-alive
Content-Length: 1371330
Pragma: no-cache
Cache-Control: no-cache
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryd9n7NKPBSR9245hd
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,te;q=0.6

Request Payload :

------WebKitFormBoundaryd9n7NKPBSR9245hd
Content-Disposition: form-data; name="fileUpload"; filename="Hydrangeas.jpg"
Content-Type: image/jpeg

------WebKitFormBoundaryd9n7NKPBSR9245hd
Content-Disposition: form-data; name="fileUpload1"; filename="Jellyfish.jpg"
Content-Type: image/jpeg

------WebKitFormBoundaryd9n7NKPBSR9245hd--

Java code :

// Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory();

// Configure a repository (to ensure a secure temp location is used)
File repository = new File(System.getProperty("java.io.tmpdir"));
factory.setRepository(repository);

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
try {
    List<FileItem> files = upload.parseRequest(req);
    System.out.println("files size : "+files.size());
    for(FileItem file : files) {
        String name = file.getName();
        System.out.println("file name : "+ name);
    }
} catch (FileUploadException e) {
}

The returned List contains the Jellyfish.jpg (second file) and doesnt contain the Hydrangeas.jpg (first file).

  • Anything else you do in that Servlet? Can you share from beginnging of doGet or whatever? – Jan Jan 05 '16 at 16:09
  • This might help http://stackoverflow.com/questions/13048939/file-upload-with-servletfileuploads-parserequest – Sajjad Jan 05 '16 at 16:26

0 Answers0