jyy
2021-04-12 f1a565cbcbb1ab672fdbc57953ce6d8298a159fe
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.matrix.system.common.actions;
 
import com.alibaba.fastjson.JSONObject;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.system.common.constance.AppConstance;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 多文件上传控制器
 * 
 * @author jiangyouyao
 * @email 512061637@qq.com
 * @date 2019年2月25日
 */
@Controller
@RequestMapping(value = "admin/multipleUploadFile")
public class MultipleFileUploadAction {
    Logger log = Logger.getLogger(MultipleFileUploadAction.class);
 
    @Value("${file_storage_path}")
    private String fileStoragePath;
    @Value("${static_resource_url}")
    private String nginxUrl;
 
    /**
     * 最大值
     */
    private Long maxSize = 1024*1024*100L;
 
    /**
     * 多文件上传方法
     * 
     * @author jiangyouyao
     * @email 512061637@qq.com
     * @date 2019年2月25日
     * @param response
     * @param request
     * @return
     * @throws IOException
     * @throws FileUploadException
     */
    @RequestMapping(value = "/doUpload")
    public @ResponseBody JSONObject doFileUpload(HttpServletResponse response, MultipartHttpServletRequest request, Integer data)
            throws IOException, FileUploadException {
        // 文件保存目录路径
        String savePath = fileStoragePath;
        // 文件保存目录URL
        String saveUrl = nginxUrl;
        // String msgPag = "common/fileUploadResult";
        JSONObject object = new JSONObject();
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
 
        // 保存和访问路径检查
        if (StringUtils.isBlank(saveUrl) || StringUtils.isBlank(savePath)) {
            object.put("status", "err");
            object.put("msg", "文件上传失败错误代码:001");
            return object;
        }
        // 检查目录
        File uploadDir = new File(savePath);
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
        // 检查目录写权限
//        if (!uploadDir.canWrite()) {
//            object.put("status", "err");
//            object.put("msg", "上传目录没有写权限");
//            return object;
//        }
 
        Map<String, MultipartFile> fileMaps = request.getFileMap();
        for (String key : fileMaps.keySet()) {
            MultipartFile file = fileMaps.get(key);
 
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String ymd = sdf.format(new Date());
            savePath += ymd + "/";
            saveUrl += ymd + "/";
            File dirFile = new File(savePath);
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }
            log.info("上传文件名:" + file.getOriginalFilename());
            log.info("上传文件大小:" + file.getBytes().length);
            log.info("上传文件大小限制:" + maxSize);
            log.info("上传文件大小是否超过限制:" + (file.getBytes().length > maxSize));
            if (file.getBytes().length > maxSize) {
                object.put("status", "err");
                object.put("msg", "上传文件大小超过限制");
                return object;
            }
            String fileName = file.getOriginalFilename();
            String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
 
            fileName = fileName.replace("." + fileExt, "");
            fileName = getSensitive(fileName);
            String newFileName = UUIDUtil.getRandomID() + UUIDUtil.getRandomID() + "." + fileExt;
            File uploadedFile = new File(savePath, newFileName);
            try {
                FileCopyUtils.copy(file.getBytes(), uploadedFile);
            } catch (Exception e) {
                object.put("status", "err");
                object.put("msg", "上传文件失败 "+e.getMessage());
                return object;
            }
            log.info("saveUrl:" + saveUrl);
            String visitPath = saveUrl + newFileName;
            log.info("上传一个文件:" + newFileName);
            log.info("访问路径:" + visitPath);
            // 获取回调函数
            /*
             * String callBack = request.getParameter("callBack"); String inputId =
             * request.getParameter("inputId"); request.setAttribute("status", "200");
             * request.setAttribute("callBack", callBack); request.setAttribute("inputId",
             * inputId); request.setAttribute("url", visitPath);
             */
            object.put("path", visitPath);
            object.put("fileName", fileName);
            object.put("status", 200);
            if (data != null) {
                object.put("index", data);
            }
        }
        return object;
    }
 
    /**
     * 检查文件名,过滤特殊字符
     * 
     * @author jiangyouyao
     * @email 512061637@qq.com
     * @date 2019年2月25日
     * @param globWords
     * @return
     */
    public String getSensitive(String globWords) {
 
        String sensitive = "";
        Pattern pattern = Pattern.compile(AppConstance.SPECIAL_CHARACTERS);
        Matcher matcher = pattern.matcher(globWords);
        while (matcher.find()) {
            sensitive += matcher.group();
        }
        /*
         * if(sensitive=="" || sensitive.length()<3 ){
         * sensitive=StringUtils.getRandomString(8); }
         */
        return sensitive;
    }
}