2

We are using java 6. I need to check "inputStream" is null or not.

@POST
@Path("/test")
@ApiOperation ( value = "Test", response = String.class)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String test(
  @ApiParam(value = "file upload") @FormDataParam("file") InputStream is
) {
  return is.read();
}

The inputStream returns "117" while reading even when we didn't upload file.

  • I have read inputStream used inputStream.available() it always returns "0"
  • I have searched through google some links suggested to use "PushbackInputStream" (PushbackInputStream)

  • PushbackInputStream is support for jdk-7 but we are using jdk-6

  • how to solve this issue??

How to check if the inputStream is null???

Community
  • 1
  • 1
SST
  • 2,054
  • 5
  • 35
  • 65

2 Answers2

0

The inputStream.available() returns 0 for my requests too. I don't know if my solution works every time, but it works for me.

You can get additional information about the uploaded file with one more parameter in the method and you can use it to check the name of the file. If it's empty, the InputStream should be empty:

@POST
@Path("/test")
@ApiOperation ( value = "Test", response = String.class)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String test(
    @FormDataParam("file") InputStream is,
    @FormDataParam("file") FormDataContentDisposition fileDetails
) {
    String fileName = fileDetails.getFileName();
    if (fileName != null && !fileName.equals("")) {
        // read the input stream
    }

    return is.read();
}
Galya
  • 6,294
  • 6
  • 27
  • 45
0

You should not specify a different name for each param, like:

@FormDataParam("file") InputStream is,
@FormDataParam("details") FormDataContentDisposition fileDetails

Maybe one is clobbering the other.

CodeSlave
  • 425
  • 6
  • 21