java 如何检查字节数组是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30340963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:57:02 来源:igfitidea点击:
How to check if byte array is empty or not?
提问by Sagar Koshti
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
.
现在我想检查这是否byte[]
为空,在调试时当我没有上传任何文件时,它显示其长度为9
,为什么9
不是0
。
采纳答案by Pramod Karandikar
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
}