Commit 382986fd authored by zhangqingle's avatar zhangqingle

增加图片损坏验证

parent ae9dbe77
......@@ -666,8 +666,12 @@ public class BChapterServiceImpl implements BChapterService {
LogUtil.fileChangeLog("------------------下载-------------------------" + fileOld);
String downLoadName = downLoadFile(bFile, file, filePath);
LogUtil.fileChangeLog("------------------判断可读-------------------------" + fileOld);
if ("7".equals(bFile.getType()) || "8".equals(bFile.getType()) || "9".equals(bFile.getType()) || "10".equals(bFile.getType())){
FileReadableUtil.imgReadable(filePath + File.separator + downLoadName);
}else {
FileReadableUtil.fileReadable(filePath + File.separator + downLoadName, fileMaxSize, fileMaxPages);
}
}
return flag;
}
......
......@@ -64,5 +64,15 @@ public class BFileController extends PaginationController<BFile> {
MessageConstant.MESSAGE_ALERT_ERROR,
message);
}
@ApiOperation(value="403 判断图片是否可读", notes="判断图片是否可读")
@PostMapping(value = "/fileReadable")
public Result imgReadable(CurUser curUser,MultipartFile file) {
int result = bFileService.imgReadable(file);
return Result.builder(new PersistModel(1),
MessageConstant.MESSAGE_ALERT_SUCCESS,
MessageConstant.MESSAGE_ALERT_ERROR,
result);
}
}
......@@ -54,6 +54,8 @@ public interface BFileService {
String deleteFile();
int imgReadable(MultipartFile file);
public BFile queryFileUrlByChapterId(String chapterId);
......
......@@ -11,8 +11,12 @@ import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.rcisoft.core.exception.ServiceException;
import org.rcisoft.core.result.ResultServiceEnums;
import org.springframework.web.multipart.MultipartFile;
import ucar.nc2.util.IO;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
@Slf4j
......@@ -199,4 +203,41 @@ public class FileReadableUtil {
return file;
}
public static int imgReadable(MultipartFile f){
try{
// File f = new File(path);
// FileInputStream fi = new FileInputStream(f);
FileInputStream fi = (FileInputStream)f.getInputStream();
try{
BufferedImage sourceImg =ImageIO.read(fi);//判断图片是否损坏
int picWidth= sourceImg.getWidth(); //确保图片是正确的(正确的图片可以取得宽度)
}catch (Exception e) {
fi.close();//关闭IO流才能操作图片
throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
}finally{
fi.close();//最后一定要关闭IO流
}
}catch (IOException e ) {
throw new ServiceException("读取io流失败");
}
return 1;
}
public static void imgReadable(String path){
try{
File f = new File(path);
FileInputStream fi = new FileInputStream(f);
try{
BufferedImage sourceImg =ImageIO.read(fi);//判断图片是否损坏
int picWidth= sourceImg.getWidth(); //确保图片是正确的(正确的图片可以取得宽度)
}catch (Exception e) {
fi.close();//关闭IO流才能操作图片
throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
}finally{
fi.close();//最后一定要关闭IO流
}
}catch (IOException e ) {
throw new ServiceException("读取io流失败");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment