vendredi 2 mai 2014

Jersey File Upload Example

In this example we are going to see how you can upload a File to a server using a JAX-RS REST Service using Jersey. Uploading a File using Jersey is fairly easy, as it uses all the HTTP infrastructure for file upload operations.
In this example we are going to use an HTML Form that has one input field of typefile. When the HTTP POST request is constructed, it will contain a media type ofmultipart/form-data. The media-type multipart/form-data follows the rules of all multipart MIME data streams. multipart/form-data contains a number of parts, corresponding to the input parameters of the form. Each part contains a content-disposition header where the disposition type is form-data. The disposition, also contains a “name” parameter, the value of which is the input field name in the HTML form and can be used to obtain this header in our service
reference http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/ (here only the code)

The form 
01<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
02<html>
03<head>
04<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
05<title>Form Page</title>
06</head>
07<body>
08<h1>Upload a File</h1>
09 
10    <form action="rest/files/upload" method="post" enctype="multipart/form-data">
11 
12       <p>
13        Select a file : <input type="file" name="file" size="50" />
14       </p>
15 
16       <input type="submit" value="Upload It" />
17    </form>
18 
19</body>
20</html>
The service
@Path("/files")
17public class JerseyFileUpload {
18 
19    private static final String SERVER_UPLOAD_LOCATION_FOLDER ="C://Users/nikos/Desktop/Upload_Files/";
20 
21    /**
22     * Upload a File
23     */
24 
25    @POST
26    @Path("/upload")
27    @Consumes(MediaType.MULTIPART_FORM_DATA)
28    public Response uploadFile(
29            @FormDataParam("file") InputStream fileInputStream,
30            @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
31 
32        String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();
33 
34        // save the file to the server
35        saveFile(fileInputStream, filePath);
36 
37        String output = "File saved to server location : " + filePath;
38 
39        return Response.status(200).entity(output).build();
40 
41    }
42 
43    // save uploaded file to a defined location on the server
44    private void saveFile(InputStream uploadedInputStream,
45            String serverLocation) {
46 
47        try {
48            OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
49            int read = 0;
50            byte[] bytes = new byte[1024];
51 
52            outpuStream = new FileOutputStream(new File(serverLocation));
53            while ((read = uploadedInputStream.read(bytes)) != -1) {
54                outpuStream.write(bytes, 0, read);
55            }
56            outpuStream.flush();
57            outpuStream.close();
58        catch (IOException e) {
59 
60            e.printStackTrace();
61        }
62 
63    }
64 
65}
The service Using FormDataMultiPart

@Path("/files")
20public class JerseyFileUpload {
21 
22    private static final String SERVER_UPLOAD_LOCATION_FOLDER ="C://Users/nikos/Desktop/Upload_Files/";
23 
24    /**
25     * Upload a File
26     */
27 
28    @POST
29    @Path("/upload")
30    @Consumes(MediaType.MULTIPART_FORM_DATA)
31    public Response uploadFile(FormDataMultiPart form) {
32 
33         FormDataBodyPart filePart = form.getField("file");
34 
35         ContentDisposition headerOfFilePart =  filePart.getContentDisposition();
36 
37         InputStream fileInputStream = filePart.getValueAs(InputStream.class);
38 
39         String filePath = SERVER_UPLOAD_LOCATION_FOLDER + headerOfFilePart.getFileName();
40 
41        // save the file to the server
42        saveFile(fileInputStream, filePath);
43 
44        String output = "File saved to server location using FormDataMultiPart : " + filePath;
45 
46        return Response.status(200).entity(output).build();
47 
48    }
49 
50    // save uploaded file to a defined location on the server
51    private void saveFile(InputStream uploadedInputStream, String serverLocation) {
52 
53        try {
54            OutputStream outpuStream = new FileOutputStream(new File(
55                    serverLocation));
56            int read = 0;
57            byte[] bytes = new byte[1024];
58 
59            outpuStream = new FileOutputStream(new File(serverLocation));
60            while ((read = uploadedInputStream.read(bytes)) != -1) {
61                outpuStream.write(bytes, 0, read);
62            }
63 
64            outpuStream.flush();
65            outpuStream.close();
66 
67            uploadedInputStream.close();
68        catch (IOException e) {
69 
70            e.printStackTrace();
71        }
72 
73    }
74 
75}

Aucun commentaire:

Enregistrer un commentaire