我使用以下代码来获取上传文件。
@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
。
答案 0 :(得分:6)
您可以通过以下方式对文件执行空检查:
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
}