|
1
2
3
4
|
package com.huaheng.common.utils.file;
import com.huaheng.common.exception.file.FileNameLengthLimitExceededException;
import com.huaheng.framework.config.HuaHengConfig;
|
|
5
6
|
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
|
|
7
|
import org.apache.commons.fileupload.FileUploadBase;
|
|
8
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
9
10
|
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.web.multipart.MultipartFile;
|
|
11
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
12
|
|
|
13
|
import java.io.*;
|
|
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
|
/**
* 文件上传工具类
*
* @author huaheng
*/
public class FileUploadUtils
{
/**
* 默认大小 50M
*/
public static final long DEFAULT_MAX_SIZE = 52428800;
/**
* 默认上传的地址
*/
private static String defaultBaseDir = HuaHengConfig.getProfile();
/**
* 默认的文件名最大长度
*/
public static final int DEFAULT_FILE_NAME_LENGTH = 200;
/**
* 默认文件类型jpg
*/
public static final String IMAGE_JPG_EXTENSION = ".jpg";
|
|
43
44
45
46
47
48
49
50
51
52
|
/**
* 图片类型png
*/
public static final String IMAGE_PNG_EXTENSION = ".png";
/**
* 图片类型ico
*/
public static final String IMAGE_ICO_EXTENSION = ".ico";
|
|
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
|
private static int counter = 0;
public static void setDefaultBaseDir(String defaultBaseDir)
{
FileUploadUtils.defaultBaseDir = defaultBaseDir;
}
public static String getDefaultBaseDir()
{
return defaultBaseDir;
}
/**
* 以默认配置进行文件上传
*
* @param file 上传的文件
* @return 文件名称
* @throws Exception
*/
public static final String upload(MultipartFile file) throws IOException
{
try
{
return upload(getDefaultBaseDir(), file, FileUploadUtils.IMAGE_JPG_EXTENSION);
}
catch (Exception e)
{
throw new IOException(e);
}
}
/**
* 根据文件路径上传
*
* @param baseDir 相对应用的基目录
* @param file 上传的文件
* @return 文件名称
* @throws IOException
*/
public static final String upload(String baseDir, MultipartFile file) throws IOException
{
try
{
return upload(baseDir, file, FileUploadUtils.IMAGE_JPG_EXTENSION);
}
catch (Exception e)
{
throw new IOException(e);
}
}
/**
* 文件上传
*
* @param baseDir 相对应用的基目录
* @param file 上传的文件
* @param needDatePathAndRandomName 是否需要日期目录和随机文件名前缀
* @param extension 上传文件类型
* @return 返回上传成功的文件名
* @throws FileSizeLimitExceededException 如果超出最大大小
* @throws FileNameLengthLimitExceededException 文件名太长
* @throws IOException 比如读写文件出错时
*/
public static final String upload(String baseDir, MultipartFile file, String extension)
|
|
117
|
throws FileUploadBase.FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException
|
|
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
|
{
int fileNamelength = file.getOriginalFilename().length();
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
{
throw new FileNameLengthLimitExceededException(file.getOriginalFilename(), fileNamelength,
FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
}
assertAllowed(file);
String fileName = encodingFilename(file.getOriginalFilename(), extension);
File desc = getAbsoluteFile(baseDir, baseDir + fileName);
file.transferTo(desc);
return fileName;
}
private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException
{
File desc = new File(File.separator + filename);
if (!desc.getParentFile().exists())
{
desc.getParentFile().mkdirs();
}
if (!desc.exists())
{
|
|
146
|
// desc.createNewFile();
|
|
147
148
149
150
|
}
return desc;
}
|
|
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
public static final String upload(String baseDir, MultipartFile file, int log)
throws FileUploadBase.FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException
{
int fileNamelength = file.getOriginalFilename().length();
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
{
throw new FileNameLengthLimitExceededException(file.getOriginalFilename(), fileNamelength,
FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
}
assertAllowed(file);
String fileName = file.getOriginalFilename();
File desc = getAbsoluteFile(baseDir, baseDir + fileName);
file.transferTo(desc);
return fileName;
}
|
|
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/**
* 编码文件名
*/
private static final String encodingFilename(String filename, String extension)
{
filename = filename.replace("_", " ");
filename = new Md5Hash(filename + System.nanoTime() + counter++).toHex().toString() + extension;
return filename;
}
/**
* 文件大小校验
*
* @param file 上传的文件
* @return
* @throws FileSizeLimitExceededException 如果超出最大大小
*/
|
|
189
|
public static final void assertAllowed(MultipartFile file) throws FileUploadBase.FileSizeLimitExceededException
|
|
190
191
192
193
|
{
long size = file.getSize();
if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
{
|
|
194
|
throw new FileUploadBase.FileSizeLimitExceededException("not allowed upload upload", size, DEFAULT_MAX_SIZE);
|
|
195
196
197
|
}
}
|
|
198
199
200
201
202
203
204
205
206
207
208
209
|
/**
* 公司logo,icon图片上传路径
* @return
* @throws FileNotFoundException
*/
public static String getCorporationUploadPath() throws FileNotFoundException {
//上传到resource/static/corporation目录下,jar包布署时无法实现
// String path = ResourceUtils.getURL("classpath:").getPath()+"static/";
//上传到huangheng.profile配置的目录下
String path = getDefaultBaseDir();
return path;
}
|
|
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
|
public static final MultipartFile fileToMultipartFile(File file) {
FileItem fileItem = createFileItem(file);
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
return multipartFile;
}
private static FileItem createFileItem(File file) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
FileItem item = factory.createItem("textField", "text/plain", true, file.getName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
try {
FileInputStream
fis = new FileInputStream(file);
OutputStream
os = item.getOutputStream();
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return item;
}
|
|
236
|
}
|