linzhijie
2021-03-11 93af1c6ffb9ae0e894689ad3a37b548e57d54cff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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;
import com.ots.framework.config.ServerConfig;
import com.ots.framework.web.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (delete) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            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();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        } catch (Exception e) {
            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);
            ajax.put("url", url);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}