0

While uploading PDF/text file using JERSEY client, extra information is being appended to the original pdf file.(I an using Jersey Glassfish API for Client)

Below is the extra content which is getting appended to the original file when saved in server:

  --Boundary_1_10166575_1464163574882
   Content-Type: text/plain
   Content-Disposition: form-data; name="foo" bar

   --Boundary_1_10166575_1464163574882
    Content-Type: application/pdf
    Content-Disposition: form-data; filename="ABC.pdf"; modification-date="Tue, 03 May 2016 06:34:59 GMT"; size=109494; name="inputPdfFile"

========================================================================= Refer the below Client Source Code:

        final Client client = ClientBuilder.newBuilder()
                .register(MultiPartFeature.class).build();

        final FileDataBodyPart filePart = new FileDataBodyPart(
                "inputPdfFile", new File(
                        "C:/Test/ABC.pdf"));
        FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
        final FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
                .field("foo", "bar").bodyPart(filePart);

        final WebTarget target = client
                .target("http://localhost:7001/RestFulService/resources/pdfUploadTest");
        final Response response = target.request().post(
                Entity.entity(multipart, multipart.getMediaType()));

======================================================================== Refer the below Server Source Code:

@POST
@Path("/pdfUploadTest")
@Consumes(MediaType.MULTIPART_FORM_DATA)    
public Response uploadFile(
    @FormDataParam("inputPdfFile") InputStream encodedPdfData,
    @FormDataParam("inputPdfFile") FormDataContentDisposition fileDetail) {

           OutputStream out = new FileOutputStream(new   
                                 File("C:/Test/success.pdf"));
         byte[] byteArray = new byte[1024];
        if (null != encodedPdfData) {
            while ((x = encodedPdfData.read(byteArray)) != -1) {
                out.write(byteArray,0,x);
            }
        }
        out.flush();
        out.close();
    }
  • What extra content are you talking about? That is the standard format of a multipart request. Aside from that you are not even using the correct part field name for your annotation. You should be using `@FormDataParam("inputPdfFile")` – Paul Samsotha May 25 '16 at 08:42
  • Thanks for pointing out the typo error. However the problem persists even after correcting it. The standard format as pointed out by you is being added to the top of actual pdf file saved on server using out.write(). Because of this the pdf is getting corrupted and we are unable to print it due to this additional information. – ANUP KUMAR MONDAL May 25 '16 at 08:53
  • What problem? You have not described any error. You are only showing the output, which is valid multipart format. If the boundary and headers are the "extra content" you are talking about, then you are mistaken. That is not extra content. That's how a multipart request _should_ look. – Paul Samsotha May 25 '16 at 08:55
  • It sounds like you are getting the entire request body into the input stream, when you are expecting just the single file part. The only way I can see this happening is if you have not registered the MultiPartFeature on the server side also – Paul Samsotha May 25 '16 at 08:58
  • I basically want to save the pdf file on server as it was originally sent from client. But when I save this pdf file in local file system it contains extra content as well apart from original pdf content. This extra content should be part of request but is also being added to the pdf file as being read using byte stream – ANUP KUMAR MONDAL May 25 '16 at 09:01
  • How to register the MultiPartFeature on the server side? Please specify for the above server code snippet. – ANUP KUMAR MONDAL May 25 '16 at 09:26
  • You need to do this at the application configuration level, not the resource class level. See [this post](http://stackoverflow.com/a/30656345/2587435) – Paul Samsotha May 25 '16 at 09:34
  • Added the below code snippet in web.xml: jersey.config.server.provider.classnames org.glassfish.jersey.media.multipart.MultiPartFeature Getting error as Truncated. see log file for complete stacktrace Caused By: java.lang.ClassNotFoundException: org.glassfish.jersey.serv er.internal.inject.AbstractHttpContextValueFactory – ANUP KUMAR MONDAL May 25 '16 at 11:24
  • I have enabled above mentioned MutiPartFeature in web.xml and included all the required jars. However I am getting below mentioned exception while running this code. Interestingly the missing method as mentioned in the exception is already there in guava-13.jar. Error 500--Internal Server Error java.lang.NoSuchMethodError: com.google.common.collect.Sets.newIdentityHashSet()Ljava/util/Set; at org.glassfish.jersey.model.internal.CommonConfig. (CommonConfig.java:212) at org.glassfish.jersey.server.ResourceConfig$State. (ResourceConfig.java:107) – ANUP KUMAR MONDAL May 26 '16 at 10:38

0 Answers0