Jersey Image Upload Client

I am trying to upload an image using the Jersey web service, I am using a jersey client to upload the image. below is a jersey web service that accepts an input stream and uploads an image to a server. it works fine when i call it directly using jsp multipart form upload but fails when i upload images using jersey client

@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) throws ServiceException { // upload code } 

Below, the jersey client uploads the image, the client code is part of another web service that is called from the php rest client and this jersey client call to download the web service to download the image, if I directly call the jersey web service to download the image, it works fine but it doesn’t work when I load using the jersey client.

 ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); client.setChunkedEncodingSize(1024); WebResource wr = client .resource("http://localhost:8080/rest/upload"); String contentDisposition = "attachment; filename=\"" + fileDetail.getName() + "\""; FormDataMultiPart form = new FormDataMultiPart(); ContentDisposition contentDisposition2 = new ContentDisposition(contentDisposition); form.setContentDisposition(contentDisposition2); FormDataBodyPart fdp = new FormDataBodyPart("file", uploadedInputStream, MediaType.MULTIPART_FORM_DATA_TYPE); form.bodyPart(fdp); ClientResponse response = wr.type(MediaType.MULTIPART_FORM_DATA).post( ClientResponse.class, form) 

Please help me not to know what I do not see here. Thanks.

+5
source share
1 answer

this complete example of loading an image using a jersey client and web service client code

 public class Test { private static URI getBaseURI() { return UriBuilder.fromUri("http://localhost:8080/restfullwebservice/resources/generic").build(""); } public static void main(String[] args) throws FileNotFoundException { final ClientConfig config = new DefaultClientConfig(); final Client client = Client.create(config); final WebResource resource = client.resource(getBaseURI()).path("upload"); final File fileToUpload = new File("C:/Users/Public/Pictures/Desert.jpg"); final FormDataMultiPart multiPart = new FormDataMultiPart(); if (fileToUpload != null) { multiPart.bodyPart(new FileDataBodyPart("file", fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE)); } final ClientResponse clientResp = resource.type( MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, multiPart); System.out.println("Response: " + clientResp.getClientResponseStatus()); client.destroy(); } } 

your web service

 @POST @Path("upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public void uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) throws ServiceChannelException { OutputStream os = null; try { File fileToUpload = new File("C:/Users/Public/Pictures/Desert1.jpg"); os = new FileOutputStream(fileToUpload); byte[] b = new byte[2048]; int length; while ((length = uploadedInputStream.read(b)) != -1) { os.write(b, 0, length); } } catch (IOException ex) { Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex); } finally { try { os.close(); } catch (IOException ex) { Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex); } } } 

Full app https://github.com/abdotalaat/upladeimageusingjersy

+10
source

Source: https://habr.com/ru/post/1210728/


All Articles