FileIOUtil.java
6 KB
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
package cn.com.glsx.admin.modules.utils;
import cn.com.glsx.admin.common.enums.ResultEnum;
import cn.com.glsx.admin.common.exception.AdminException;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteWorkbook;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 项目名:ne-shield-trialdrive-admin
* 包名: cn.com.glsx.admin.modules.utils
* 作者: Yanglinlong
* 日期: 2021/5/18 14:23
*/
public class FileIOUtil {
public enum MimeType {
STREAM("application/octet-stream", "jpg"),
PDF("application/pdf", "pdf"),
ZIP("application/zip", "zip"),
RAR("application/zip", "rar"),
EXCEL2003("application/vnd.ms-excel", "xls"),
EXCEL2007("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"),
EXE("application/octet-stream", "exe"),
TXT("text/plain", "txt"),
JAVA("text/plain", "java"),
PYTHON("text/plain", "py"),
JAVASCRIPT("text/plain", "js"),
CSS("text/plain", "css"),
JPG("application/x-jpg", "jpg"),
JPEG("image/jpeg", "jpg"),
GIF("image/gif", "gif"),
PNG("application/x-png", "png");
private String contentType;
public final static String CHARSET = "UTF-8";
private String suffix;
public String getContentType() {
return contentType + ";charset=" + CHARSET;
}
private MimeType(String contentType, String suffix) {
this.contentType = contentType;
this.suffix = suffix;
}
public static String getMimeSuffix(String contentType) {
for (MimeType mimeType : MimeType.values()) {
if (mimeType.getContentType().equals(contentType)) {
return mimeType.getSuffix();
}
}
return null;
}
public String getSuffix() {
return suffix;
}
}
public static void download(InputStream input, MimeType mime, String fileName, HttpServletResponse response) throws IOException {
if (input == null) {
return;
}
response.setContentType(mime.getContentType());
response.setHeader("Set-Cookie", "fileDownload=true; path=/");
String encodeFileName = fileName + "." + mime.getSuffix();
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(encodeFileName, "UTF-8"));
long length = input.available();
if (length != -1) {
response.setContentLength((int) length);
}
ServletOutputStream output = response.getOutputStream();
IOUtils.copy(input, output);
output.flush();
}
/**
* 从服务器上下载失败的excel文件
* @param fileName
* @return
* @throws FileNotFoundException
*/
public static InputStream downloadFailFile(String filePath, String fileName) throws FileNotFoundException {
File file = new File(filePath, fileName);
if (!file.exists()) {
throw new AdminException(ResultEnum.IMPORT_FAIL_NOT_FOUND);
}
InputStream inputStream = new FileInputStream(file);
return inputStream;
}
/**
* 获取文件的类型
* @param multipartFile
* @return
*/
public static String getMultipartFileType(MultipartFile multipartFile) {
if (multipartFile == null) {
return null;
}
String contentType = multipartFile.getContentType();
return MimeType.getMimeSuffix(contentType);
}
/**
* 导出文件到直指定路径
* @param path
* @param tList
* @param <T>
* @return
*/
public static <T>String exportFailRecord(String path, List<T> tList) {
if (CollectionUtils.isEmpty(tList)) {
return null;
}
String seqNo = System.currentTimeMillis() + "";
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
//清除30天之前的缓存文件
cleanExpiredFiles(file, 60);
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(path + "/" + seqNo + ".xlsx");
WriteWorkbook writeWorkbook = new WriteWorkbook();
writeWorkbook.setOutputStream(outputStream);
writeWorkbook.setExcelType(ExcelTypeEnum.XLSX);
ExcelWriter excelWriter = new ExcelWriter(writeWorkbook);
WriteSheet writeSheet = new WriteSheet();
writeSheet.setSheetName("sheet");
excelWriter.write(tList, writeSheet);
excelWriter.finish();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(outputStream);
}
return seqNo;
}
/**
* 清除过期文件
* @param dir
* @param expiredDays
*/
public static void cleanExpiredFiles(File dir, int expiredDays) {
if(!dir.exists()){
return ;
}
//清除 expiredDays 天之前的垃圾文件
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
long lastModified = pathname.lastModified();
long interval = DateUtil.interval(lastModified, System.currentTimeMillis(), TimeUnit.DAYS);
if (interval >= expiredDays) {
return true;
}
return false;
}
});
if(!ArrayUtils.isEmpty(files)){
for (File file : files) {
file.delete();
}
}
}
}