0

I know that there are some similar questions to this one on the site, and I've read most of them. I've also Googled for a solution, but am still having issues.

So, in my project I need to catch multipart post requests that are used to send mixed data (an image, two strings (company, type), date data (month, year) and a few integers) from a client. Upon receiving this request I need to change contrast and brightness of the image, and modify the color to gray tones.

For debugging I use Postman Chrome Extension, and for now I am only focusing on sending the image. I've tested sending the request in different ways, in half of them I get a "404 Not Found" error, in the other half wrong data is returned (mostly array of 0). Below is the code used in my resource method:

@Component
@Path("/code")
@Produces("application/json")
public class EncodeResource extends BasicResource {
    @POST
    @Path("/encode")
    @Consumes("multipart/form-data")
    public String encodeImage(
            @FormDataParam("image") InputStream imageInputStream) {
        BufferedImage imageBuffer = null;
        try {
            imageBuffer = ImageIO.read(imageInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (imageBuffer != null)
            return okResponse(<some parameters>);
        else
            return errorResponse(<some parameters>);
    }
}

ImageInputStream contains some byte array, but ImageIO.read() always returns null. I tried different images with different extensions (which imageio can read by default, checked it), but got the same result. As I think, it happens cause of there are some "unnecessary" information in my inputStream, so imageio.read() can't convert it into any type of image. I've suddenly found it, when changed InputStream to File:

@FormDataParam("image") File imageInputStream

...and then checked saved by tomcat .tmp file. It looked like that:

------WebKitFormBoundary4nC1XD4cevbbRh7A

Content-Disposition: form-data; name="image"; filename="1.jpg">

Content-Type: image/jpeg

(a lot of bytes)

------WebKitFormBoundary4nC1XD4cevbbRh7A--

And of course imageio.read() returned null.

Summing up all of that, I have 2 questions actually:

  1. Why did I get 404 error and how can i fix it? For example, I tried this and like here. And I had to delete the second @FormDataParam from here to receive at least something. Can it be cause of the Postman "specific" form-data requests?
  2. Why did I get the wrong data? Maybe I can parse received file and get "correct" byte array? Or something else?..
Community
  • 1
  • 1
Morn
  • 1
  • 1

0 Answers0