Java – how do I check if a byte array is empty?
•
Java
I am using the following code to get the 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. During debugging, I didn't upload any files. It shows that its length is 9. Why isn't its 9 0
Solution
You can check the null of a file in the following ways:
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 }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码