users@jersey.java.net

[Jersey] Re: What is the best approach to upload a file using Jersey client?

From: Matthew Hall <mhall_at_mhcomputing.net>
Date: Mon, 22 Jul 2013 00:08:38 -0700

How did each look in Wireshark Follow TCP Stream? Did you try timing a few big uploads to compare?
-- 
Sent from my mobile device.
svileng_at_gmail.com wrote:
>I want to upload a file (a zip file to be specific) to a Jersey-backed
>REST server.
>
>Basically there are two approaches (I mean using Jersey Client,
>otherwise one can use pure servlet API or various HTTP clients) to do
>this:
>
>1)
>
>	WebResource webResource = resource();
>	final File fileToUpload = new File("D:/temp.zip");
>
>	final FormDataMultiPart multiPart = new FormDataMultiPart();
>	if (fileToUpload != null) {
>	    multiPart.bodyPart(new FileDataBodyPart("file",
>fileToUpload, MediaType.valueOf("application/zip")));
>	}
>
>	final ClientResponse clientResp =
>webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(
>	    ClientResponse.class, multiPart);
>	System.out.println("Response: " +
>clientResp.getClientResponseStatus());
>
>
>2) 
>
>    File fileName = new File("D:/temp.zip");
>	    InputStream fileInStream = new FileInputStream(fileName);
>	    String sContentDisposition = "attachment; filename=\"" +
>fileName.getName() + "\"";
>	    ClientResponse response =
>resource().type(MediaType.APPLICATION_OCTET_STREAM)
>		.header("Content-Disposition",
>sContentDisposition).post(ClientResponse.class, fileInStream);
>	    System.out.println("Response: " +
>response.getClientResponseStatus());
>
>For sake of completeness here is the server part:
>
>    @POST
>	@Path("/import")
>	@Consumes({MediaType.MULTIPART_FORM_DATA,
>MediaType.APPLICATION_OCTET_STREAM})
>	public void uploadFile(File theFile) throws
>PlatformManagerException, IOException {
>	    ...
>	}
>
>So I am wondering what is the difference between those two clients?  
>Which one to use and why?   
>Downside (for me) of using 1) approach is that it adds dependency on
>jersey-multipart.jar (which additionally adds dependency on
>mimepull.jar) so why would I want those two jars in my classpath if
>pure Jersey Client approach 2) works just as fine.  
>And maybe one general question is whether there is a better way to
>implement ZIP file upload, both client and server side...