2

I really can't understand the reason for this error. I ran the sample application. It works correctly. Same code but cannot load correctly. I think the error is due to the version difference. Anyone have any suggestions for a solution?

The web service I created

    @POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response getImageText( @FormDataParam("file")InputStream inputStream) { 
        try {
            byte[] bytes =IOUtils.toByteArray(inputStream);             
            inputStream.close();                
            System.out.println(bytes.length);           
             ArrayList<ImageBarcod> temp=null;
            return Response.ok(temp).build();
        } catch (Exception e) {
            return Response
                    .status(Response.Status.NOT_FOUND)
                    .build();               
        }   
}

bytes.length output:

enter image description here

file size to upload

enter image description here

sample application

enter image description here

upload file:

upload file

save file:

save file

Libraries:

Maven Libraries

Ogün
  • 97
  • 2
  • 10
  • _"I really can't understand the reason for this error"_ - What error? Please be specific. – Paul Samsotha Mar 02 '21 at 02:27
  • I am sending the file from the client to the server, the expected file size is 190906. But the incoming file size is 191169 – Ogün Mar 02 '21 at 03:41
  • Have you looked at the contents of the InputStream to compare its contents? – Paul Samsotha Mar 02 '21 at 08:57
  • I have updated the question now. I guess I need to pull the body part – Ogün Mar 02 '21 at 10:52
  • What you are seeing is the _entire_ raw multipart request. What you want is just the extracted 'file' part. Normally, this is wha the `@FormDataParam` does; it extracts the part that you specify in the annotation. What this tells me is that the annotation is not being recognized, and so the InputStream is simply the entire request entity. My guess is that the `@FormDataParam` annotation you are using is not the correct one (maybe different package - version 2.x). It is hard to tell without more information. I suggest you post all your dependencies and app configuration if you want more help – Paul Samsotha Mar 02 '21 at 10:55
  • I edited the question. You can see the photo of the Libraries. There must be a missing .jar file. @RequestBody does not appear. – Ogün Mar 02 '21 at 18:55
  • Please stop using screenshots when using text is possible. It is much easier to view your pom dependencies than to look at a picture of jars. Same goes with all your other screenshots. – Paul Samsotha Mar 02 '21 at 20:46
  • Also @RequestBody is for Spring MVC. You are not using Spring MVC. Also why do have both Jersey 1.x and 2.x dependencies? You know that the two are incompatible, right? I don't even know which one you are using for your project. Where is your web.xml? – Paul Samsotha Mar 02 '21 at 20:46
  • Multipart version: When I deleted 1.8 and got 2.25, it was fixed. I removed all 1.x versions. I'm sorry I took your time. Thank you for giving a time. – Ogün Mar 02 '21 at 21:50

1 Answers1

1

The problem is that you are using Jersey 2.x, but your Multipart dependency is for Jersey 1.x. The two Jersey versions are incompatible. So the @FormDataParam annotation you using is just being ignored. That's why what you are getting in the InputStream is the entire multipart entity instead of just the file part.

What you need to do is get rid of all your Jersey 1.x dependencies then add the Jersey 2.x jersey-media-multipart dependency.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>${jersey2.version}</version>
</dependency>

Then you need to register the MultiPartFeature with your application. You can see a few different ways to do that in this post. After you do that, you will be able to use the 2.x version of the @FormDataParam (which is different from the 1.x version).

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720