1 package com.acobc.common.utils.file;
2
3 import java.io.File;
4 import java.io.IOException;
5 import org.apache.shiro.crypto.hash.Md5Hash;
6 import org.apache.tomcat.util.http.fileupload.FileUploadBase.FileSizeLimitExceededException;
7 import org.springframework.web.multipart.MultipartFile;
8 import com.wstapp.appframework.model.exception.FileNameLengthLimitExceededException;
9
10 /**
11 * 文件上传工具类
12 *
13 * 2018-12-06 14:51:42
14 */
15 public class FileUploadUtils
16 {
17
18 /**
19 * 默认大小 50M
20 */
21 public static final long DEFAULT_MAX_SIZE = 52428800;
22
23 /**
24 * 默认上传的地址
25 */
26 private static String defaultBaseDir = "";
27
28 /**
29 * 默认的文件名最大长度
30 */
31 public static final int DEFAULT_FILE_NAME_LENGTH = 200;
32
33 /**
34 * 默认文件类型jpg
35 */
36 public static final String IMAGE_JPG_EXTENSION = ".jpg";
37
38 private static int counter = 0;
39
40 public static void setDefaultBaseDir(String defaultBaseDir)
41 {
42 FileUploadUtils.defaultBaseDir = defaultBaseDir;
43 }
44
45 public static String getDefaultBaseDir()
46 {
47 return defaultBaseDir;
48 }
49
50 /**
51 * 以默认配置进行文件上传
52 *
53 * @param file 上传的文件
54 * @return 文件名称
55 * @throws Exception
56 */
57 public static final String upload(MultipartFile file) throws IOException
58 {
59 try
60 {
61 return upload(getDefaultBaseDir(), file, FileUploadUtils.IMAGE_JPG_EXTENSION);
62 }
63 catch (Exception e)
64 {
65 throw new IOException(e);
66 }
67 }
68
69 /**
70 * 根据文件路径上传
71 *
72 * @param baseDir 相对应用的基目录
73 * @param file 上传的文件
74 * @return 文件名称
75 * @throws IOException
76 */
77 public static final String upload(String baseDir, MultipartFile file) throws IOException
78 {
79 try
80 {
81 return upload(baseDir, file, FileUploadUtils.IMAGE_JPG_EXTENSION);
82 }
83 catch (Exception e)
84 {
85 throw new IOException(e);
86 }
87 }
88
89 /**
90 * 文件上传
91 *
92 * @param baseDir 相对应用的基目录
93 * @param file 上传的文件
94 * @param extension 上传文件类型
95 * @return 返回上传成功的文件名
96 * @throws FileSizeLimitExceededException 如果超出最大大小
97 * @throws FileNameLengthLimitExceededException 文件名太长
98 * @throws IOException 比如读写文件出错时
99 */
100 public static final String upload(String baseDir, MultipartFile file, String extension)
101 throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException
102 {
103
104 int fileNamelength = file.getOriginalFilename().length();
105 if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
106 {
107 throw new FileNameLengthLimitExceededException(file.getOriginalFilename(), fileNamelength,
108 FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
109 }
110
111 assertAllowed(file);
112
113 String fileName = encodingFilename(file.getOriginalFilename(), extension);
114
115 File desc = getAbsoluteFile(baseDir, baseDir + fileName);
116 file.transferTo(desc);
117 return fileName;
118 }
119
120 private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException
121 {
122 File desc = new File(File.separator + filename);
123
124 if (!desc.getParentFile().exists())
125 {
126 desc.getParentFile().mkdirs();
127 }
128 if (!desc.exists())
129 {
130 desc.createNewFile();
131 }
132 return desc;
133 }
134
135 /**
136 * 编码文件名
137 */
138 private static final String encodingFilename(String filename, String extension)
139 {
140 filename = filename.replace("_", " ");
141 filename = new Md5Hash(filename + System.nanoTime() + counter++).toHex().toString() + extension;
142 return filename;
143 }
144
145 /**
146 * 文件大小校验
147 *
148 * @param file 上传的文件
149 * @return
150 * @throws FileSizeLimitExceededException 如果超出最大大小
151 */
152 public static final void assertAllowed(MultipartFile file) throws FileSizeLimitExceededException
153 {
154 long size = file.getSize();
155 if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
156 {
157 throw new FileSizeLimitExceededException("not allowed upload upload", size, DEFAULT_MAX_SIZE);
158 }
159 }
160 }