wzy
2020-12-26 2cf928a22b6cf8ea7a29f29127ffc333c73b0bca
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
package com.matrix.system.common.tools;
 
import com.matrix.core.tools.*;
import com.matrix.system.common.constance.AppConstance;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 文件上传
 * 
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date 2018年6月15日
 */
public class UploadUtil {
 
    private static String STATUSS = "status";
    private static String MSG = "msg";
 
    /**
     * 
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年6月15日
     * @param response
     * @param request
     * @param extList
     * @param folderType
     * @param userId
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
 
    public static Map<String, String> doUpload(MultipartHttpServletRequest request, List<FileType> extList,
            String folderType, Long userId) throws NoSuchAlgorithmException, IOException {
        Map<String, String> resourceMap = new HashMap<>();
 
        // 图片保存目录路径
        String baseSavePath = PropertiesUtil.getString(AppConstance.FILES_TORAGE_PATH);
        // 图片保存目录URL
        String baseSaveUrl = PropertiesUtil.getString(AppConstance.NGINX_URL);
        LogUtil.debug("图片保存目录路径={}",baseSavePath);
        LogUtil.debug("图片保存目录URL={}",baseSaveUrl);
 
 
        // 检查目录
        File uploadDir = new File(baseSavePath);
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
 
        // 保存和访问路径检查
        if (StringUtils.isBlank(baseSaveUrl) || StringUtils.isBlank(baseSavePath)) {
            resourceMap.put(STATUSS, "err");
            resourceMap.put(MSG, "服务器图片保存路径配置错误");
            return resourceMap;
        }
 
        // 检查目录写权限
        if (!uploadDir.canWrite()) {
            resourceMap.put(STATUSS, "err");
            resourceMap.put(MSG, "上传目录没有写权限");
            return resourceMap;
        }
 
        Map<String, MultipartFile> fileMaps = request.getFileMap();
        for (String key : fileMaps.keySet()) {
            MultipartFile file = fileMaps.get(key);
            if (!FileTypeUtil.checkFileType(file.getInputStream(), extList)) {
                resourceMap.put(STATUSS, "err");
                resourceMap.put(MSG, "文件类型不正确");
                return resourceMap;
            }
            // 拼接64位文件名
            String fileName = file.getOriginalFilename();
            String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            String newFileName = UUIDUtil.getRandomID() + "." + fileExt;
            Map<String, String> fileUrlMap = fileUrl(baseSavePath, baseSaveUrl, folderType, userId);
            String savePath = fileUrlMap.get("savePath");
            String saveUrl = fileUrlMap.get("saveUrl");
            File uploadedFile = new File(savePath, newFileName);
            try {
                FileCopyUtils.copy(file.getBytes(), uploadedFile);
            } catch (IOException e) {
                resourceMap.put(STATUSS, "err");
                resourceMap.put(MSG, "文件上传失败" + e.getMessage());
                return resourceMap;
            }
            // 图片访问地址
            String visitPath = saveUrl + newFileName;
            String fileSize = file.getBytes().length + "";
            // 保存图片大小
            resourceMap.put("fileSize", fileSize);
            // 图片访问地址
            resourceMap.put("visitPath", visitPath);
            resourceMap.put("savePath", savePath + newFileName);
 
        }
        resourceMap.put(STATUSS, "ok");
        resourceMap.put(MSG, "图片上传成功");
        return resourceMap;
    }
 
    /**
     * 加載保存文件路徑
     * 
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年6月15日
     * @param savePath
     * @param saveUrl
     * @param folderType
     * @param userId
     * @return
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    public static Map<String, String> fileUrl(String savePath, String saveUrl, String folderType, Long userId)
            throws UnsupportedEncodingException, NoSuchAlgorithmException {
        Map<String, String> fileUrlMap = new HashMap<>();
 
        // 创建图片文件夹
        savePath += folderType + File.separatorChar;
        saveUrl += folderType + File.separatorChar;
        File saveDirFile = new File(savePath);
        if (!saveDirFile.exists()) {
            saveDirFile.mkdirs();
        }
 
        // 以账号ID命名创建文件夹
        String encryptionId = EncrypUtil.getSha1(userId+"");
        savePath += encryptionId + File.separatorChar;
        saveUrl += encryptionId + File.separatorChar;
        File userIdFile = new File(savePath);
        if (!userIdFile.exists()) {
            userIdFile.mkdirs();
        }
 
        // 创建日期文件夹
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String ymd = sdf.format(new Date());
        savePath += ymd + File.separatorChar;
        saveUrl += ymd + File.separatorChar;
        File dirFile = new File(savePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
 
        fileUrlMap.put("savePath", savePath);
        fileUrlMap.put("saveUrl", saveUrl);
        return fileUrlMap;
 
    }
 
}