linzhijie
2021-03-11 93af1c6ffb9ae0e894689ad3a37b548e57d54cff
src/main/java/com/ots/project/common/CommonController.java
@@ -1,4 +1,5 @@
package com.ots.project.common;
import com.ots.common.utils.file.FileUploadUtils;
import com.ots.common.utils.file.FileUtils;
import com.ots.framework.config.EssConfig;
@@ -12,24 +13,38 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 通用请求处理
 *
 * @author ots
 */
@Controller
public class CommonController {
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);
    @Autowired
    private ServerConfig serverConfig;
    /**
     * 通用下载请求
     *
     * @param fileName 文件名称
     * @param delete   是否删除
     */
    @GetMapping("common/download")
    public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
        try {
              //先注释掉看看,因为没有发先本系统的业务的任何意义
//            if (!FileUtils.isValidFilename(fileName)) {
//                throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
//            }
            String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
            String filePath = EssConfig.getDownloadPath() + fileName;
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
@@ -42,14 +57,17 @@
            log.error("下载文件失败", e);
        }
    }
    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = EssConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
@@ -60,31 +78,36 @@
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 上传图片服务器
     * /common/images 这里请求的连接是images,你的文件夹也要叫images
     */
    @PostMapping("/common/images")
    @ResponseBody
    public AjaxResult upImage(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = EssConfig.getUploadImagePath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            //String url = serverConfig.getUrl() + fileName;
            String url = fileName.replaceFirst("/profile/images", EssConfig.getImagePath());
            //1、先压缩出一个新的
            // filePath = "/Users/shawnli/Downloads/images"
            // fileName = "/profile/images/2019/12/19/aba5e2f850edf254a9e5b2a35c50089a.png"
//            // url = "http://localhost:8090/profile/images/2019/12/19/aba5e2f850edf254a9e5b2a35c50089a.png"
//            String filePathOriginal = fileName.replaceFirst("/profile/images", filePath);
//            String fileNew = filePathOriginal + "."+filePathOriginal.substring(filePathOriginal.lastIndexOf(".") + 1);
//            PicUtils.commpressPicForScale(filePathOriginal, fileNew, 20, 0.5);
//
//            //2、删除旧的
//            File fileOper = new File(filePathOriginal);
//            fileOper.delete();
//
//            //3、命名新的为旧的名字
//            File fileNewOper = new File(fileNew);
//            fileNewOper.renameTo(new File(filePathOriginal));
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);