7

I am using following code to get uploaded file.

 @POST
    @Path("update")
    @Consumes(MediaType.WILDCARD)
    public boolean updateWorkBookMaster(MultipartFormDataInput input) {
        try {
            //get form data
            Map<String, List<InputPart>> uploadForm = input.getFormDataMap();

            //get uploaded file
            List<InputPart> inputParts = uploadForm.get("workBookFile");
            MultivaluedMap<String, String> header = inputParts.get(0).getHeaders();
            InputStream inputStream = inputParts.get(0).getBody(InputStream.class, null);
            byte[] bytes = IOUtils.toByteArray(inputStream);

now i want to check whether this byte[] is empty or not, while debugging when i have not uploaded any file it shows its length as 9, why its 9 not 0.

3
  • 2
    maybe because it still contains header info Commented May 20, 2015 at 5:30
  • 1
    An empty array is simply one with a length of 0. Commented May 20, 2015 at 5:33
  • @JunedAhsan you are right its header info, for this can i check as if(bytes != null && bytes.length >10) ? Commented May 20, 2015 at 5:51

1 Answer 1

6

You can implement null check for files this way:

import org.glassfish.jersey.media.multipart.ContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;

@POST
@Path("update")
@Consumes(MediaType.WILDCARD)
public boolean updateWorkBookMaster(FormDataMultiPart multiPartData) {
    try {
        final FormDataBodyPart workBookFilePart = multiPartData.getField("workBookFile");
        final ContentDisposition workBookFileDetails = workBookFilePart.getContentDisposition();
        final InputStream workBookFileDocument = workBookFilePart.getValueAs(InputStream.class);

        if (workBookFileDetails.getFileName() != null || 
            workBookFileDetails.getFileName().trim().length() > 0 ) {
            // file is present
        } else {
            // file is not uploadded
        }
    } ... // other code
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.