java – 如何检查字节数组是否为空?

前端之家收集整理的这篇文章主要介绍了java – 如何检查字节数组是否为空?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用以下代码获取上传文件.
@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);

现在我想检查这个byte []是否为空,
在调试时,我没有上传任何文件,它显示其长度为9,为什么它的9不为0.

解决方法

您可以通过以下方式实现对文件的空检查:
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
}
原文链接:https://www.f2er.com/java/129948.html

猜你在找的Java相关文章