Commit 2fc94169 authored by liqin's avatar liqin 💬

bug fixed

parent 42dafa18
package cn.wisenergy.chnmuseum.party.web.controller; package cn.wisenergy.chnmuseum.party.web.controller;
import cn.wisenergy.chnmuseum.party.common.dfs.FastDFSUtils; import cn.wisenergy.chnmuseum.party.common.dfs.FastDFSUtils;
import cn.wisenergy.chnmuseum.party.common.util.ImageUtil;
import cn.wisenergy.chnmuseum.party.common.vo.PicUploadResult; import cn.wisenergy.chnmuseum.party.common.vo.PicUploadResult;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -28,39 +27,38 @@ public class PicUploadController { ...@@ -28,39 +27,38 @@ public class PicUploadController {
private String IMAGE_BASE_URL; private String IMAGE_BASE_URL;
// 允许上传的格式 // 允许上传的格式
private static final String[] IMAGE_TYPE = new String[] { ".bmp", ".jpg", ".jpeg", ".png" }; private static final String[] IMAGE_TYPE = new String[]{".bmp", ".jpg", ".jpeg", ".png"};
@RequestMapping(value = "/upload", method = RequestMethod.POST) // @RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<PicUploadResult> upload(@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response) throws Exception { // public ResponseEntity<PicUploadResult> upload(@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response) throws Exception {
// 校验文件扩展名 // // 校验文件扩展名
boolean isLegal = false; // boolean isLegal = false;
// for (String type : IMAGE_TYPE) { // for (String type : IMAGE_TYPE) {
// if (StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(), type)) { // if (StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(), type)) {
// isLegal = true; // isLegal = true;
// break; // break;
// } // }
// } // }
byte[] bytes = uploadFile.getBytes(); // byte[] bytes = uploadFile.getBytes();
if (ImageUtil.isPicture(uploadFile.getOriginalFilename()) && ImageUtil.isImage(bytes) && ImageUtil.isImage1(bytes)) { // if (ImageUtil.isPicture(uploadFile.getOriginalFilename()) && ImageUtil.isImage(bytes) && ImageUtil.isImage1(bytes)) {
isLegal = true; // isLegal = true;
} else { // } else {
isLegal = false; // isLegal = false;
throw new RuntimeException("图片文件不合法"); // throw new RuntimeException("图片文件不合法");
} // }
//
// 封装Result对象,并且将文件的byte数组放置到result对象中 // // 封装Result对象,并且将文件的byte数组放置到result对象中
PicUploadResult fileUploadResult = new PicUploadResult(); // PicUploadResult fileUploadResult = new PicUploadResult();
// 状态 // // 状态
fileUploadResult.setError(isLegal ? 0 : 1); // fileUploadResult.setError(isLegal ? 0 : 1);
// 文件新路径 // // 文件新路径
String filePath = FastDFSUtils.uploadPic(bytes, uploadFile.getOriginalFilename(), uploadFile.getSize()); // String filePath = FastDFSUtils.uploadPic(bytes, uploadFile.getOriginalFilename(), uploadFile.getSize());
//
if (log.isDebugEnabled()) { // if (log.isDebugEnabled()) {
log.debug("Pic file upload .[{}] to [{}] .", uploadFile.getOriginalFilename(), filePath); // log.debug("Pic file upload .[{}] to [{}] .", uploadFile.getOriginalFilename(), filePath);
} // }
String picUrl = IMAGE_BASE_URL + filePath; // String picUrl = IMAGE_BASE_URL + filePath;
// fileUploadResult.setUrl(picUrl); // fileUploadResult.setUrl(picUrl);
// // 校验图片是否合法
// isLegal = false; // isLegal = false;
// try { // try {
// BufferedImage image = ImageIO.read(new URL(picUrl)); // BufferedImage image = ImageIO.read(new URL(picUrl));
...@@ -73,27 +71,24 @@ public class PicUploadController { ...@@ -73,27 +71,24 @@ public class PicUploadController {
// log.error("check image is legal error! " + e.getMessage()); // log.error("check image is legal error! " + e.getMessage());
// } // }
// isLegal = true; // isLegal = true;
// // 状态
// fileUploadResult.setError(isLegal ? 0 : 1); // fileUploadResult.setError(isLegal ? 0 : 1);
if (!isLegal) { // if (!isLegal) {
// 不合法,将磁盘上的文件删除 // // 不合法,将磁盘上的文件删除
FastDFSUtils.deletePic(picUrl); // FastDFSUtils.deletePic(picUrl);
} // }
return ResponseEntity.status(HttpStatus.CREATED).body(fileUploadResult); // return ResponseEntity.status(HttpStatus.CREATED).body(fileUploadResult);
} // }
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ResponseEntity<PicUploadResult> uploadFile(
@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response)
throws Exception {
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<PicUploadResult> uploadFile(@RequestParam(value = "cover") MultipartFile uploadFile, HttpServletResponse response) throws Exception {
// 封装Result对象,并且将文件的byte数组放置到result对象中 // 封装Result对象,并且将文件的byte数组放置到result对象中
PicUploadResult fileUploadResult = new PicUploadResult(); PicUploadResult fileUploadResult = new PicUploadResult();
if(uploadFile.getSize()/(1024*1024)>1024){ if (uploadFile.getSize() / (1024 * 1024) > 1024) {
//50兆 //50兆
log.error("附件大小超过50兆!请重新上传 " ); log.error("附件大小超过50兆!请重新上传 ");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}; }
;
// 文件新路径 // 文件新路径
String filePath = FastDFSUtils.uploadPic(uploadFile.getBytes(), uploadFile.getOriginalFilename(), String filePath = FastDFSUtils.uploadPic(uploadFile.getBytes(), uploadFile.getOriginalFilename(),
uploadFile.getSize()); uploadFile.getSize());
......
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