jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.file;
 
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.uploader.DownloadFile;
import com.bstek.dorado.uploader.UploadFile;
import com.bstek.dorado.uploader.annotation.FileProvider;
import com.bstek.dorado.uploader.annotation.FileResolver;
import com.ld.igds.util.ContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;
 
/**
 * 工单附件文件上传、下载、删除等操作
 *
 * @author Andy
 */
@Slf4j
@Component
public class ApplyOrderUploadProcessor {
 
    @Autowired
    private CoreFileService fileService;
 
    /**
     * applyOrderUploadProcessor#uploadApplyOrderFile
     *
     * @param file
     * @param parameter
     * @return fileId 新文件ID包含文件后缀
     */
    @FileResolver
    public String uploadApplyOrderFile(UploadFile file, Map<String, Object> parameter) {
 
        log.debug("---uploadApplyOrderFile-----{}", file.getFileName());
        String fileId = null;
        try {
 
            String basePath = fileService.getCommonFilePath(null);
 
            fileId = ContextUtil.getTimeId(1000)
                    + file.getFileName().substring(
                    file.getFileName().lastIndexOf("."));
 
            file.transferTo(new File(basePath + fileId));
 
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return fileId;
    }
 
    /**
     * applyOrderUploadProcessor#downloadApplyOrderFile 文件下载
     *
     * @param parameter
     * @return
     * @throws IOException
     */
    @FileProvider
    public DownloadFile downloadApplyOrderFile(Map<String, Object> parameter) throws Exception {
        InputStream inputStream = null;
        try {
            String info = ContextHolder.getRequest().getHeader("User-Agent");
            String fileName = (String) parameter.get("fileName");
            String fileId = (String) parameter.get("fileId");
            String timeStr = (String) parameter.get("createTime");
 
            Date createTime = DateUtils.parseDate(timeStr, "yyyy-MM-dd hh:mm:ss");
 
            if (null == createTime) createTime = new Date();
 
            fileName = URLDecoder.decode(fileName, "UTF-8");
 
 
            log.debug("---downloadApplyOrderFile-----{}", fileName);
 
 
            File file = new File(fileService.getCommonFilePath(createTime), fileId);
 
            if (!file.exists()) {
                throw new Exception("附件不存在");
            }
 
            inputStream = new FileInputStream(file);
            DownloadFile dlFile = new DownloadFile(fileId, inputStream);
 
            // 浏览器的默认编码不同,IE下BGK
            if (info.indexOf("MSIE") != -1) {
                dlFile.setFileName(new String(fileName.getBytes("GBK"), "iso-8859-1"));
            } else {// firefox、chrome、safari、opera
                dlFile.setFileName(new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
            }
            return dlFile;
 
        } catch (Exception e) {
            if (null != inputStream)
                inputStream.close();
            log.error("下载附件执行失败:{}", e.getMessage());
        }
        return null;
    }
 
    /**
     * applyOrderUploadProcessor#delFile
     *
     * @param parameter
     * @return
     * @throws IOException
     */
    @Expose
    public String delFile(Map<String, Object> parameter) throws Exception {
 
        String fileName = (String) parameter.get("fileName");
        String fileId = (String) parameter.get("fileId");
        Date createTime = (Date) parameter.get("createTime");
        if (null == createTime) createTime = new Date();
        fileName = URLDecoder.decode(fileName, "UTF-8");
        log.debug("---delFile-----{}", fileName);
 
        File file = new File(fileService.getCommonFilePath(createTime), fileId);
 
        if (!file.exists()) {
            return "删除失败,系统未找到当前附件";
        }
 
        file.delete();
        fileService.delFile(fileId);
        return null;
    }
 
}