Selecting multiple files and uploading them using Jersey

Ujjwal Subedi

I need help with multiple file uploads using Jersey. I used the following code to upload a single file using Jersey.

package my.first.rest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;



import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("uploadfile")
public class Upload {
String location;



    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public  String uploadfile(@FormDataParam("file") InputStream is, @FormDataParam("file") FormDataContentDisposition filedetail){



        saveToDisk(is,filedetail);
        return  "File Uploaded Succesfully_"+location;

    }


    private void saveToDisk(InputStream is1,
            FormDataContentDisposition filedetail) {
        // TODO Auto-generated method stub

         location = "E://upload/"+filedetail.getFileName();
        try{
            OutputStream out = new FileOutputStream(new File(location));
            int read = 0;
            byte[] bytes = new byte[1024];
            out = new FileOutputStream (new File(location));
            while((read = is1.read(bytes)) != -1){
                out.write(bytes,0,read);
            }
            out.flush();

            out.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }


}

The above code works well with uploading a single file but when I add the multiple="multiple" attribute to the input type = file tag, I'm able to select multiple items in a single upload, it uploads the first selected item and the name of the last selected item. I'm not expecting the code to work cause it wasn't meant to handle multiple file uploads but there has to be a way around it, right?? Since, it's taking one file and the name of another one.

I have looked multiple stackoverflow threads and googled a lot. I wouldn't have posted this if I had found the answer. I'm not looking for uploading multiple files using the types of code below:

<input type ="file" name="file">
<input type ="file" name="file">
<input type ="file" name="file2">

I don't want to upload multiple files individually. I want to be able to select multiple files at a time and all of them to be uploaded somewhere. My tag is like this:

<input type ="file" name="file" multiple="multiple">

Here's the entire HTML code.

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action ="http://localhost:8080/fuseframework/uploadfile/upload" method="post" enctype="multipart/form-data">
file: 
<br>
<input type ="file" name="file" multiple="multiple">

<input type="submit" value="send">
</form>
</body>
</html>

These are the jars I've used http://i.stack.imgur.com/1tVT8.png

李骏骁

It works OK for me to use the 'FormDataMultiPart'.

Here is the Java code, the FormDataContentDisposition object(formParams) contains actual file content.

List<FormDataBodyPart> parts = formParams.getFields("file");
for (FormDataBodyPart part : parts) {
    FormDataContentDisposition file = part.getFormDataContentDisposition();
}

In the JS side, I use the FormData object and push several files with the same name:

for (var i = 0; i < files.length; i++)
    fd.append('file', files[i]);

Wish it will help

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

uploading multiple files and zip them in a folder in the server

From Dev

Uploading multiple files using Javascript

From Dev

Uploading multiple files with iOS SDK and deleting them locally when done

From Dev

Uploading and processing multiple files using MVC

From Dev

Uploading multiple files via FTP using curl

From Dev

Selecting multiple records and passing them on

From Dev

Uploading multiple files in a ModelForm

From Dev

AngularJs Uploading multiple files .then

From Dev

Multiple Files Uploading in Laravel

From Dev

Selecting multiple files with in bash

From Dev

Selecting multiple files with in bash

From Dev

Uploading multiple image files using okhttp v3.2.0

From Dev

Weird behaviour when uploading multiple files using AngularJS

From Dev

Uploading multiple single files using separate Dropzones in one form

From Dev

Uploading multiple files in PHP using HTML Form or cURL

From Dev

Uploading multiple files in PHP using HTML Form or cURL

From Dev

Uploading Multiple Files in Android using existing multipart post project

From Dev

Uploading multiple image files using okhttp v3.2.0

From Dev

Uploading large file using Jersey on backend

From Dev

Uploading large file using Jersey on backend

From Dev

Giving unique names to files while uploading them

From Dev

Send files with phpmailer before uploading them?

From Dev

Giving unique names to files while uploading them

From Dev

Send files with phpmailer before uploading them?

From Dev

multiple images uploading and converting them to thumbnails in cakephp

From Dev

Issues with Uploading Multiple files with PHP

From Dev

Uploading multiple files in the same request

From Dev

Multiple files uploading in multidimensional array

From Dev

PHP code for uploading multiple files

Related Related

  1. 1

    uploading multiple files and zip them in a folder in the server

  2. 2

    Uploading multiple files using Javascript

  3. 3

    Uploading multiple files with iOS SDK and deleting them locally when done

  4. 4

    Uploading and processing multiple files using MVC

  5. 5

    Uploading multiple files via FTP using curl

  6. 6

    Selecting multiple records and passing them on

  7. 7

    Uploading multiple files in a ModelForm

  8. 8

    AngularJs Uploading multiple files .then

  9. 9

    Multiple Files Uploading in Laravel

  10. 10

    Selecting multiple files with in bash

  11. 11

    Selecting multiple files with in bash

  12. 12

    Uploading multiple image files using okhttp v3.2.0

  13. 13

    Weird behaviour when uploading multiple files using AngularJS

  14. 14

    Uploading multiple single files using separate Dropzones in one form

  15. 15

    Uploading multiple files in PHP using HTML Form or cURL

  16. 16

    Uploading multiple files in PHP using HTML Form or cURL

  17. 17

    Uploading Multiple Files in Android using existing multipart post project

  18. 18

    Uploading multiple image files using okhttp v3.2.0

  19. 19

    Uploading large file using Jersey on backend

  20. 20

    Uploading large file using Jersey on backend

  21. 21

    Giving unique names to files while uploading them

  22. 22

    Send files with phpmailer before uploading them?

  23. 23

    Giving unique names to files while uploading them

  24. 24

    Send files with phpmailer before uploading them?

  25. 25

    multiple images uploading and converting them to thumbnails in cakephp

  26. 26

    Issues with Uploading Multiple files with PHP

  27. 27

    Uploading multiple files in the same request

  28. 28

    Multiple files uploading in multidimensional array

  29. 29

    PHP code for uploading multiple files

HotTag

Archive