PicUploadController.java 3.82 KB
Newer Older
liqin's avatar
liqin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
package cn.wisenergy.chnmuseum.party.web.controller;

import cn.wisenergy.chnmuseum.party.common.dfs.FastDFSUtils;
import cn.wisenergy.chnmuseum.party.common.vo.PicUploadResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图片上传
 */
@RestController
@RequestMapping("/pic")
public class PicUploadController {

    private static final Logger log = LoggerFactory.getLogger(PicUploadController.class);

    // 允许上传的格式
liqin's avatar
liqin committed
25
    private static final String[] IMAGE_TYPE = new String[]{".bmp", ".jpg", ".jpeg", ".png"};
liqin's avatar
liqin committed
26

liqin's avatar
liqin committed
27 28 29 30
//    @RequestMapping(value = "/upload", method = RequestMethod.POST)
//    public ResponseEntity<PicUploadResult> upload(@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response) throws Exception {
//        // 校验文件扩展名
//        boolean isLegal = false;
liqin's avatar
liqin committed
31 32 33 34 35 36
//        for (String type : IMAGE_TYPE) {
//            if (StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(), type)) {
//                isLegal = true;
//                break;
//            }
//        }
liqin's avatar
liqin committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
//        byte[] bytes = uploadFile.getBytes();
//        if (ImageUtil.isPicture(uploadFile.getOriginalFilename()) && ImageUtil.isImage(bytes) && ImageUtil.isImage1(bytes)) {
//            isLegal = true;
//        } else {
//            isLegal = false;
//            throw new RuntimeException("图片文件不合法");
//        }
//
//        // 封装Result对象,并且将文件的byte数组放置到result对象中
//        PicUploadResult fileUploadResult = new PicUploadResult();
//        // 状态
//        fileUploadResult.setError(isLegal ? 0 : 1);
//        // 文件新路径
//        String filePath = FastDFSUtils.uploadPic(bytes, uploadFile.getOriginalFilename(), uploadFile.getSize());
//
//        if (log.isDebugEnabled()) {
//            log.debug("Pic file upload .[{}] to [{}] .", uploadFile.getOriginalFilename(), filePath);
//        }
//        String picUrl = IMAGE_BASE_URL + filePath;
liqin's avatar
liqin committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
//        fileUploadResult.setUrl(picUrl);
//        isLegal = false;
//        try {
//            BufferedImage image = ImageIO.read(new URL(picUrl));
//            if (image != null) {
//                // fileUploadResult.setWidth(image.getWidth() + "");
//                // fileUploadResult.setHeight(image.getHeight() + "");
//                isLegal = true;
//            }
//        } catch (IOException e) {
//            log.error("check image is legal error! " + e.getMessage());
//        }
//        isLegal = true;
//        fileUploadResult.setError(isLegal ? 0 : 1);
liqin's avatar
liqin committed
70 71 72 73 74 75
//        if (!isLegal) {
//            // 不合法,将磁盘上的文件删除
//            FastDFSUtils.deletePic(picUrl);
//        }
//        return ResponseEntity.status(HttpStatus.CREATED).body(fileUploadResult);
//    }
liqin's avatar
liqin committed
76

liqin's avatar
liqin committed
77
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
liqin's avatar
liqin committed
78
    public ResponseEntity<PicUploadResult> uploadFile(@RequestParam(value = "cover") MultipartFile uploadFile) throws Exception {
liqin's avatar
liqin committed
79
        PicUploadResult fileUploadResult = new PicUploadResult();
liqin's avatar
liqin committed
80
        String filePath = FastDFSUtils.uploadFile(uploadFile.getInputStream(), uploadFile.getSize(), uploadFile.getOriginalFilename());
liqin's avatar
liqin committed
81 82 83
        if (log.isDebugEnabled()) {
            log.debug("Pic file upload .[{}] to [{}] .", uploadFile.getOriginalFilename(), filePath);
        }
liqin's avatar
liqin committed
84
        fileUploadResult.setUrl(filePath);
liqin's avatar
liqin committed
85 86 87 88
        return ResponseEntity.status(HttpStatus.CREATED).body(fileUploadResult);
    }

}