广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
package cn.huge.module.file.utils;
 
import cn.huge.base.common.constant.FileCatEnum;
import cn.huge.base.common.exception.ServiceException;
import cn.huge.base.common.utils.DateUtils;
import cn.huge.module.constant.BaseConsts;
import cn.huge.module.file.config.FtpFileConfig;
import cn.huge.module.file.domain.po.FileInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
 
/**
 * @title: 上传附件包装处理类
 * @description: 上传附件包装处理类
 * @company: hugeinfo
 * @author: liyj
 * @time: 2021-11-05 16:51:48
 * @version: 1.0.0
 */
public class FtpMultipartFileWrapper {
 
    static final long kilobyte = 1024;
    static final long megabyte = 1024 * 1024L;
    static final long gigabyte = 1024 * 1024 * 1024L;
 
    static final BigDecimal kilobyteDividend = new BigDecimal(kilobyte);
    static final BigDecimal megabyteDividend = new BigDecimal(megabyte);
    static final BigDecimal gigabyteDividend = new BigDecimal(gigabyte);
 
    static final String SHOW_URL_WEB = "/api/web/fileInfo/show";
    static final String DOWN_URL_WEB = "/api/web/fileInfo/down";
    static final String SHOW_URL_WECHAT = "/api/wechat/fileInfo/show";
    static final String DOWN_URL_WECHAT = "/api/wechat/fileInfo/down";
 
    static final String STORE_WAY = "ftp";
 
    private MultipartFile file;
 
    public FtpMultipartFileWrapper(MultipartFile file) {
        this.file = file;
    }
 
    /**
     * web端上传附件
     * @param fileId
     * @return
     */
    public FileInfo toWebFileInfo(String fileId) {
        FileInfo fileInfo = new FileInfo();
        fileInfo.setId(fileId);
        fileInfo.setSuffix(this.getFileSuffix());
        fileInfo.setTrueName(this.getFileName() + BaseConsts.DOT + fileInfo.getSuffix());
        fileInfo.setFileName(this.getFileName() + BaseConsts.UNDER + fileInfo.getId() + BaseConsts.DOT + fileInfo.getSuffix());
        fileInfo.setCat(FileCatEnum.getFileCat(fileInfo.getSuffix()));
        fileInfo.setSize(this.getFileSize());
        fileInfo.setUnit(this.getFileUnit());
        fileInfo.setMd5Code(this.getFileVerifyCode());
        fileInfo.setStoreWay(STORE_WAY);
        fileInfo.setPath(this.getFileDir());
        fileInfo.setFullPath(fileInfo.getPath() + BaseConsts.SLANT + fileInfo.getFileName());
        fileInfo.setShowUrl(SHOW_URL_WEB + BaseConsts.SLANT + fileInfo.getId());
        fileInfo.setDownUrl(DOWN_URL_WEB + BaseConsts.SLANT + fileInfo.getId());
        // todo
        fileInfo.setZipUrl(fileInfo.getShowUrl());
        Date nowDate = DateUtils.getNowDate();
        fileInfo.setCreateTime(nowDate);
        fileInfo.setUpdateTime(nowDate);
        fileInfo.setDeleteStatus(BaseConsts.DELETE_STATUS_0);
        return fileInfo;
    }
 
    /**
     * wechat上传附件
     * @param fileId
     * @return
     */
    public FileInfo toWechatFileInfo(String fileId) {
        FileInfo fileInfo = new FileInfo();
        fileInfo.setId(fileId);
        fileInfo.setSuffix(this.getFileSuffix());
        fileInfo.setTrueName(this.getFileName() + BaseConsts.DOT + fileInfo.getSuffix());
//        fileInfo.setFileName(this.getFileName() + BaseConsts.UNDER + fileInfo.getId() + BaseConsts.DOT + fileInfo.getSuffix());
        fileInfo.setFileName(this.getFileVerifyCode() + BaseConsts.DOT + fileInfo.getSuffix());
        fileInfo.setCat(FileCatEnum.getFileCat(fileInfo.getSuffix()));
        fileInfo.setSize(this.getFileSize());
        fileInfo.setUnit(this.getFileUnit());
        fileInfo.setMd5Code(this.getFileVerifyCode());
        fileInfo.setStoreWay(STORE_WAY);
        fileInfo.setPath(this.getFileDir());
        fileInfo.setFullPath(fileInfo.getPath() + BaseConsts.SLANT + fileInfo.getFileName());
        fileInfo.setShowUrl(SHOW_URL_WECHAT + BaseConsts.SLANT + fileInfo.getId());
        fileInfo.setDownUrl(DOWN_URL_WECHAT + BaseConsts.SLANT + fileInfo.getId());
        // todo
        fileInfo.setZipUrl(fileInfo.getShowUrl());
        Date nowDate = DateUtils.getNowDate();
        fileInfo.setCreateTime(nowDate);
        fileInfo.setUpdateTime(nowDate);
        fileInfo.setDeleteStatus(BaseConsts.DELETE_STATUS_0);
        return fileInfo;
    }
 
    /**
     * 获取文件名称
     */
    public String getFileName() {
        return StringUtils.substringBeforeLast(this.file.getOriginalFilename(), BaseConsts.DOT);
    }
 
    /**
     * 获取文件后缀
     */
    public String getFileSuffix() {
        return StringUtils.substringAfterLast(this.file.getOriginalFilename(), BaseConsts.DOT);
    }
 
    /**
     * 获取文件存储目录,以斜杠开头(/home/ftp/huge-dyh/2022/3)
     */
    public String getFileDir() {
        //按照年月构建目录
        Calendar ca = Calendar.getInstance();
        StringBuilder builder = new StringBuilder();
        builder.append(FtpFileConfig.getRootdir());
        builder.append(BaseConsts.SLANT);
        builder.append(ca.get(Calendar.YEAR));
        builder.append(BaseConsts.SLANT);
        builder.append(ca.get(Calendar.MONTH) + 1);
        return builder.toString();
    }
 
    /**
     * 获取文件大小, 根据不同的单位进行运算
     */
    public double getFileSize() {
        long size = this.file.getSize();
        BigDecimal divisor = new BigDecimal(size);
        String unit = this.getFileUnit();
 
        if (BaseConsts.B.equals(unit)) {
            return size;
        } else if (BaseConsts.KB.equals(unit)) {
            return divisor.divide(kilobyteDividend, 2, RoundingMode.HALF_UP).doubleValue();
        } else if (BaseConsts.MB.equals(unit)) {
            return divisor.divide(megabyteDividend, 2, RoundingMode.HALF_UP).doubleValue();
        } else if (BaseConsts.GB.equals(unit)) {
            return divisor.divide(gigabyteDividend, 2, RoundingMode.HALF_UP).doubleValue();
        }
        return 0.0;
    }
 
    /**
     * 获取文件单位, B|KB|MB|GB
     */
    public String getFileUnit() {
        long size = this.file.getSize();
        if (size > 0 && size <= kilobyte) {
            return BaseConsts.B;
        } else if (size > kilobyte && size <= megabyte) {
            return BaseConsts.KB;
        } else if (size > megabyte && size <= gigabyte) {
            return BaseConsts.MB;
        } else if (size > gigabyte) {
            return BaseConsts.GB;
        }
        return BaseConsts.B;
    }
 
    /**
     * 获取文件校验码, MD5
     */
    public String getFileVerifyCode() {
        try {
            return DigestUtils.md5Hex(this.file.getBytes());
        } catch (IOException e) {
            throw new ServiceException("MultipartFileWrapper.getFileVerifyCode", e);
        }
    }
}