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 type
file
. When the HTTP POST request is constructed, it will contain a media type of
multipart/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
04 | < meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" > |
05 | < title >Form Page</ title > |
10 | < form action = "rest/files/upload" method = "post" enctype = "multipart/form-data" > |
13 | Select a file : < input type = "file" name = "file" size = "50" /> |
16 | < input type = "submit" value = "Upload It" /> |
The service
17 | public class JerseyFileUpload { |
27 | @Consumes (MediaType.MULTIPART_FORM_DATA) |
28 | public Response uploadFile( |
29 | @FormDataParam ( "file" ) InputStream fileInputStream, |
30 | @FormDataParam ( "file" ) FormDataContentDisposition contentDispositionHeader) { |
32 | String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName(); |
35 | saveFile(fileInputStream, filePath); |
37 | String output = "File saved to server location : " + filePath; |
39 | return Response.status( 200 ).entity(output).build(); |
44 | private void saveFile(InputStream uploadedInputStream, |
45 | String serverLocation) { |
48 | OutputStream outpuStream = new FileOutputStream( new File(serverLocation)); |
50 | byte [] bytes = new byte [ 1024 ]; |
52 | outpuStream = new FileOutputStream( new File(serverLocation)); |
53 | while ((read = uploadedInputStream.read(bytes)) != - 1 ) { |
54 | outpuStream.write(bytes, 0 , read); |
58 | } catch (IOException e) { |
The service
Using FormDataMultiPart
20 | public class JerseyFileUpload { |
30 | @Consumes (MediaType.MULTIPART_FORM_DATA) |
31 | public Response uploadFile(FormDataMultiPart form) { |
33 | FormDataBodyPart filePart = form.getField( "file" ); |
35 | ContentDisposition headerOfFilePart = filePart.getContentDisposition(); |
37 | InputStream fileInputStream = filePart.getValueAs(InputStream. class ); |
39 | String filePath = SERVER_UPLOAD_LOCATION_FOLDER + headerOfFilePart.getFileName(); |
42 | saveFile(fileInputStream, filePath); |
44 | String output = "File saved to server location using FormDataMultiPart : " + filePath; |
46 | return Response.status( 200 ).entity(output).build(); |
51 | private void saveFile(InputStream uploadedInputStream, String serverLocation) { |
54 | OutputStream outpuStream = new FileOutputStream( new File( |
57 | byte [] bytes = new byte [ 1024 ]; |
59 | outpuStream = new FileOutputStream( new File(serverLocation)); |
60 | while ((read = uploadedInputStream.read(bytes)) != - 1 ) { |
61 | outpuStream.write(bytes, 0 , read); |
67 | uploadedInputStream.close(); |
68 | } catch (IOException e) { |
Aucun commentaire:
Enregistrer un commentaire