我正在使用以下代码来获取上传的文件.
@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);
解决方法
您可以通过以下方式实现对文件的空检查:
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 }