管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-02-05 d41cd4196d159aa819de0e800d343b7f9edc3d53
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.lf.server.service.all;
 
import com.alibaba.fastjson.JSON;
import com.lf.server.controller.all.BaseController;
import com.lf.server.entity.all.HttpStatus;
import com.lf.server.entity.all.ResponseMsg;
import com.lf.server.entity.all.SettingData;
import com.lf.server.entity.all.StaticData;
import com.lf.server.entity.ctrl.KeyValueEntity;
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.entity.data.MetaFileEntity;
import com.lf.server.entity.sys.AttachEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.*;
import com.lf.server.service.sys.AttachService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 上传附件服务类
 * @author WWW
 */
@Service
public class UploadAttachService {
    @Autowired
    PathHelper pathHelper;
 
    @Autowired
    AttachService attachService;
 
    private static String tabs = "''";
 
    private static Map<String, String> attachTabs = new HashMap<>();
 
    private final static Log log = LogFactory.getLog(UploadAttachService.class);
 
    private static final String NO_FILE = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.NOT_FOUND, "文件找不到"));
 
    /**
     * 获取表名
     */
    public static String getTabs() {
        return tabs;
    }
 
    /**
     * 初始化附件表
     */
    public static void init(String cfg) {
        if (StringHelper.isEmpty(cfg)) {
            return;
        }
 
        String[] strs = cfg.split(StaticData.COMMA);
        if (strs.length == 0) {
            return;
        }
 
        List<String> keys = new ArrayList<>();
        for (String str : strs) {
            if (attachTabs.containsKey(str)) {
                continue;
            }
 
            attachTabs.put(str, str.contains("bd.") ? "materiname" : "photono");
            keys.add("'" + str + "'");
        }
        tabs = StringHelper.join(keys, ",");
    }
 
    /**
     * 上传文件
     */
    public ResponseMsg<String> upload(UserEntity ue, String tab, MultipartFile file, BaseController ctrl) {
        try {
            if (file == null && file.isEmpty()) {
                return ctrl.fail("文件上传为空", null);
            }
            if (file.getSize() > SettingData.MAX_FILE_SIZE) {
                return ctrl.fail(String.format("文件大于 %d MB", SettingData.MAX_FILE_SIZE / 1024 / 1024), null);
            }
 
            // 传输文件
            String oldName = file.getOriginalFilename();
            String filePath = pathHelper.getTempPath() + File.separator + oldName;
            File newFile = new File(filePath);
            file.transferTo(newFile);
            double sizes = FileHelper.sizeToMb(file.getSize());
 
            // 获取MD5
            String md5 = FileHelper.getFileMd5(filePath);
            AttachEntity entity = attachService.selectByGuid(md5);
            if (entity != null) {
                newFile.delete();
                return ctrl.success("文件已存在", md5);
            }
 
            AttachEntity ae = getAttachEntity(ue, tab, oldName, md5, sizes);
            String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath();
            newFile.renameTo(new File(targetPath));
 
            int rows = attachService.insert(ae);
 
            return rows > 0 ? ctrl.success(md5) : ctrl.fail("保存文件失败", null);
        } catch (Exception ex) {
            return ctrl.fail(ex.getMessage(), null);
        }
    }
 
    /**
     * 获取附件实体类
     */
    protected AttachEntity getAttachEntity(UserEntity ue, String tab, String oldName, String md5, double sizes) {
        AttachEntity entity = new AttachEntity();
        entity.setName(oldName);
        entity.setTab(tab);
        entity.setGuid(md5);
        String subPath = PathHelper.getUploadPath() + File.separator + md5;
        entity.setPath(subPath);
        entity.setSizes(sizes);
        if (ue != null) {
            entity.setCreateUser(ue.getId());
        }
 
        return entity;
    }
 
    /**
     * 下载文件
     */
    public void download(String guid, HttpServletResponse res) {
        try {
            if (StringHelper.isEmpty(guid)) {
                WebHelper.write2Page(res, NO_FILE);
                return;
            }
 
            AttachEntity entity = attachService.selectByGuid(guid);
            if (entity == null) {
                WebHelper.write2Page(res, NO_FILE);
                return;
            }
 
            String file = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath();
            WebHelper.download(file, entity.getName(), res);
        } catch (Exception ex) {
            try {
                String msg = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.ERROR, "文件下载出错"));
                WebHelper.write2Page(res, msg);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            log.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 上传Excel附件
     */
    public List<KeyValueEntity> uploadXlsAnnex(List<MetaEntity> ms, List<MetaFileEntity> list, String path) {
        List<String> files = getAttachFiles(list, path);
        if (files.size() == 0) {
            return null;
        }
 
        List<KeyValueEntity> rs = new ArrayList<>();
        for (MetaEntity me : ms) {
            int rows = uploadXlsAnnex(me, files);
            rs.add(new KeyValueEntity(me.getName(), String.valueOf(rows)));
        }
 
        return rs;
    }
 
    /**
     * 获取附件文件
     */
    private List<String> getAttachFiles(List<MetaFileEntity> list, String path) {
        List<String> files = new ArrayList<>();
 
        for (MetaFileEntity mf : list) {
            if (mf.getName().contains(StaticData.ZIP)) {
                List<String> subs = getZipFiles(mf, path);
                if (null == subs || subs.size() > 0) {
                    files.addAll(subs);
                }
                continue;
            }
 
            files.add(mf.getPath());
        }
 
        return files;
    }
 
    /**
     * 获取zip中的文件
     */
    private List<String> getZipFiles(MetaFileEntity mf, String path) {
        File file = new File(mf.getPath());
        if (!file.exists() || file.isDirectory()) {
            return null;
        }
 
        File zipFolder = new File(pathHelper.getConfig().getTempPath() + File.separator + path + "_zip");
        if (!zipFolder.exists() || !zipFolder.isDirectory()) {
            zipFolder.mkdirs();
        }
 
        String subPath = zipFolder + File.separator + mf.getName().toLowerCase().replace(".zip", "");
        ZipHelper.unzip(mf.getPath(), subPath);
 
        List<String> files = new ArrayList<>();
        getFilesByPath(files, subPath);
 
        return files;
    }
 
    /**
     * 根据路径获取文件
     */
    private void getFilesByPath(List<String> list, String path) {
        File file = new File(path);
        if (!file.isDirectory()) {
            list.add(file.getPath());
            return;
        }
 
        File[] files = file.listFiles();
        if (null == files) {
            return;
        }
 
        for (File f : files) {
            if (f.isDirectory()) {
                getFilesByPath(list, f.getPath());
            } else {
                list.add(f.getPath());
            }
        }
    }
 
    /**
     * 上传Excel附件
     */
    private int uploadXlsAnnex(MetaEntity me, List<String> files) {
 
 
        return 0;
    }
}