然后将这两个文件发送到服务以设置实体.但是如果两个或一个为null,则抛出NullPointerException.如何检查两个文件中的任何一个(projectImg / chartImg)是否为空?
到目前为止,这是我的代码:
public void uploadImages(MultipartFile projectImg,MultipartFile chartImg,Long projectId) throws ValidationException,IOException {
Project project = projectRepository.findOne(projectId);
Project save = projectRepository.save(project);
int maximumSizeMB = 15000000;
if (!projectImg.isEmpty()) {
if (projectImg.getSize() > maximumSizeMB) {
throw new ValidationException("Image size is too big. Maximum size is 15 MB");
}
byte[] projectFile = ImageCompression.compressImage(projectImg);
project.setProjectImg(projectFile);
save.getProjectImg();
}
if (!chartImg.isEmpty()) {
if (chartImg.getSize() > maximumSizeMB) {
throw new ValidationException("Image size is too big. Maximum size is 15 MB");
}
byte[] chartFile = ImageCompression.compressImage(chartImg);
project.setChartImg(chartFile);
save.getChartImg();
}
projectRepository.save(project);
}
谢谢!
最佳答案
只是为了让你接受一个答案而且这个问题不能保持“未答复”,我会发表我的评论作为答案:
原文链接:https://www.f2er.com/java/437444.html你可以打电话
if (projectImg != null) { ... }
之前或之后
if (projectImg.isEmpty()) { ... }