提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.sys.core.util; |
懒 |
2 |
|
|
3 |
import cn.hutool.core.io.FileUtil; |
|
4 |
import cn.stylefeng.guns.base.consts.ConstantsContext; |
|
5 |
import cn.stylefeng.guns.sys.core.listener.ConfigListener; |
|
6 |
import cn.stylefeng.guns.sys.modular.system.model.UeditorFileResult; |
|
7 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
8 |
import cn.stylefeng.roses.kernel.model.exception.ServiceException; |
|
9 |
import com.baomidou.mybatisplus.core.toolkit.IdWorker; |
|
10 |
import lombok.extern.slf4j.Slf4j; |
|
11 |
import org.springframework.web.multipart.MultipartFile; |
|
12 |
|
|
13 |
import javax.servlet.http.HttpServletResponse; |
|
14 |
import java.io.File; |
|
15 |
import java.io.IOException; |
|
16 |
import java.io.OutputStream; |
|
17 |
|
|
18 |
import static cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum.*; |
|
19 |
|
|
20 |
/** |
|
21 |
* 百度富文本的工具类 |
|
22 |
* |
|
23 |
* @author fengshuonan |
|
24 |
* @Date 2019/8/26 15:27 |
|
25 |
*/ |
|
26 |
@Slf4j |
|
27 |
public class UeditorUtil { |
|
28 |
|
|
29 |
/** |
|
30 |
* ue上传文件逻辑 |
|
31 |
* |
|
32 |
* @author fengshuonan |
|
33 |
* @Date 2019-08-27 12:54 |
|
34 |
*/ |
|
35 |
public static UeditorFileResult uploadFile(MultipartFile upfile, FileType fileType) { |
|
36 |
if (upfile.isEmpty()) { |
|
37 |
throw new ServiceException(UE_FILE_NULL_ERROR); |
|
38 |
} |
|
39 |
|
|
40 |
// 获取文件名,后缀名 |
|
41 |
String oldFileName = upfile.getOriginalFilename(); |
|
42 |
String suffixName = ToolUtil.getFileSuffix(oldFileName); |
|
43 |
|
|
44 |
// 重新命名图片 |
|
45 |
String newFileName = IdWorker.getIdStr() + "." + suffixName; |
|
46 |
|
|
47 |
UeditorFileResult ueditorFileResult = new UeditorFileResult(); |
|
48 |
String path = null; |
|
49 |
|
|
50 |
// 如果是上传图片 |
|
51 |
if (fileType.equals(FileType.IMG)) { |
|
52 |
|
|
53 |
ueditorFileResult.setUrl(UeditorUtil.getImageRelativeUrl(newFileName)); |
|
54 |
ueditorFileResult.setTitle(newFileName); |
|
55 |
ueditorFileResult.setOriginal(newFileName); |
|
56 |
|
|
57 |
//文件用原始文件 |
|
58 |
path = ConstantsContext.getFileUploadPath() + newFileName; |
|
59 |
|
|
60 |
|
|
61 |
} else if (fileType.equals(FileType.FILE)) { |
|
62 |
|
|
63 |
// 如果是上传文件 |
|
64 |
ueditorFileResult.setUrl(UeditorUtil.getFileRelativeUrl(newFileName) + "/" + oldFileName); |
|
65 |
ueditorFileResult.setTitle(oldFileName); |
|
66 |
ueditorFileResult.setOriginal(oldFileName); |
|
67 |
|
|
68 |
//文件用原始文件 |
|
69 |
path = ConstantsContext.getFileUploadPath() + newFileName; |
|
70 |
|
|
71 |
} else { |
|
72 |
|
|
73 |
// 如果是上传视频 |
|
74 |
ueditorFileResult.setUrl(UeditorUtil.getVideoRelativeUrl(newFileName)); |
|
75 |
ueditorFileResult.setTitle(newFileName); |
|
76 |
ueditorFileResult.setOriginal(newFileName); |
|
77 |
|
|
78 |
//文件用原始文件 |
|
79 |
path = ConstantsContext.getFileUploadPath() + newFileName; |
|
80 |
|
|
81 |
} |
|
82 |
|
|
83 |
try { |
|
84 |
|
|
85 |
File dest = new File(path); |
|
86 |
if (!dest.getParentFile().exists()) { |
|
87 |
dest.getParentFile().mkdirs(); |
|
88 |
} |
|
89 |
|
|
90 |
upfile.transferTo(dest); |
|
91 |
return ueditorFileResult; |
|
92 |
} catch (IOException e) { |
|
93 |
log.error("保存ue的上传文件出错!", e); |
|
94 |
throw new ServiceException(UE_FILE_SAVE_ERROR); |
|
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* 读取文件 |
|
100 |
* |
|
101 |
* @author fengshuonan |
|
102 |
* @Date 2019-08-27 13:02 |
|
103 |
*/ |
|
104 |
public static void readFile(String fileName, HttpServletResponse response, FileType fileType, String orginalName) { |
|
105 |
|
|
106 |
if (ToolUtil.isEmpty(fileName)) { |
|
107 |
throw new ServiceException(UE_FILE_NULL_ERROR); |
|
108 |
} |
|
109 |
|
|
110 |
//获取文件路径 |
|
111 |
String path = ConstantsContext.getFileUploadPath() + fileName; |
|
112 |
File file = new File(path); |
|
113 |
|
|
114 |
//文件不存在或者不可读 |
|
115 |
if (!file.exists() || !file.canRead()) { |
|
116 |
throw new ServiceException(UE_FILE_NULL_ERROR); |
|
117 |
} |
|
118 |
|
|
119 |
//读取文件 |
|
120 |
byte[] bytes = null; |
|
121 |
|
|
122 |
//设置响应的类型 |
|
123 |
if (fileType.equals(FileType.IMG)) { |
|
124 |
|
|
125 |
response.setContentType("image/png"); |
|
126 |
bytes = FileUtil.readBytes(file); |
|
127 |
|
|
128 |
} else if (fileType.equals(FileType.FILE)) { |
|
129 |
|
|
130 |
response.setContentType("multipart/form-data;charset=utf-8"); |
|
131 |
|
|
132 |
//判断文件是否已经被重命名 |
|
133 |
String newFilePath = ConstantsContext.getFileUploadPath() + orginalName; |
|
134 |
File newFile = new File(newFilePath); |
|
135 |
if (!newFile.exists()) { |
|
136 |
newFile = UeditorUtil.reName(file, orginalName); |
|
137 |
} |
|
138 |
bytes = FileUtil.readBytes(newFile); |
|
139 |
|
|
140 |
} else { |
|
141 |
|
|
142 |
response.setContentType("video/x-sgi-movie"); |
|
143 |
bytes = FileUtil.readBytes(file); |
|
144 |
|
|
145 |
} |
|
146 |
|
|
147 |
try { |
|
148 |
OutputStream stream = response.getOutputStream(); |
|
149 |
stream.write(bytes); |
|
150 |
} catch (IOException e) { |
|
151 |
log.error("读取文件错误!", e); |
|
152 |
throw new ServiceException(UE_FILE_READ_ERROR); |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
/** |
|
157 |
* 获取图片相对路径 |
|
158 |
* |
|
159 |
* @author fengshuonan |
|
160 |
* @Date 2019/8/26 15:21 |
|
161 |
*/ |
|
162 |
private static String getImageRelativeUrl(String imageName) { |
|
163 |
String contextPath = ConfigListener.getConf().get("contextPath"); |
|
164 |
return contextPath + "/ueditor/images/" + imageName; |
|
165 |
} |
|
166 |
|
|
167 |
/** |
|
168 |
* 获取文件相对路径 |
|
169 |
* |
|
170 |
* @author fengshuonan |
|
171 |
* @Date 2019/8/26 15:22 |
|
172 |
*/ |
|
173 |
private static String getFileRelativeUrl(String imageName) { |
|
174 |
String contextPath = ConfigListener.getConf().get("contextPath"); |
|
175 |
return contextPath + "/ueditor/file/" + imageName; |
|
176 |
} |
|
177 |
|
|
178 |
/** |
|
179 |
* 获取视频相对路径 |
|
180 |
* |
|
181 |
* @author fengshuonan |
|
182 |
* @Date 2019/8/26 15:22 |
|
183 |
*/ |
|
184 |
private static String getVideoRelativeUrl(String imageName) { |
|
185 |
String contextPath = ConfigListener.getConf().get("contextPath"); |
|
186 |
return contextPath + "/ueditor/video/" + imageName; |
|
187 |
} |
|
188 |
|
|
189 |
/** |
|
190 |
* 文件重命名 |
|
191 |
* |
|
192 |
* @author fengshuonan |
|
193 |
* @Date 2019/8/26 15:23 |
|
194 |
*/ |
|
195 |
private static File reName(File file, String newName) { |
|
196 |
if (file.exists()) { |
|
197 |
|
|
198 |
//创建新名字的抽象文件 |
|
199 |
File newfile = new File(file.getParent() + File.separator + newName); |
|
200 |
|
|
201 |
if (file.renameTo(newfile)) { |
|
202 |
log.info("重命名成功!"); |
|
203 |
return newfile; |
|
204 |
} else { |
|
205 |
log.info("重命名失败!新文件名已存在!"); |
|
206 |
return file; |
|
207 |
} |
|
208 |
} else { |
|
209 |
log.info("重命名文件不存在!"); |
|
210 |
return file; |
|
211 |
} |
|
212 |
} |
|
213 |
|
|
214 |
/** |
|
215 |
* 文件类型 |
|
216 |
* |
|
217 |
* @author fengshuonan |
|
218 |
* @Date 2019-08-27 12:52 |
|
219 |
*/ |
|
220 |
public enum FileType { |
|
221 |
|
|
222 |
/** |
|
223 |
* 图片类型 |
|
224 |
*/ |
|
225 |
IMG, |
|
226 |
|
|
227 |
/** |
|
228 |
* 文件类型 |
|
229 |
*/ |
|
230 |
FILE, |
|
231 |
|
|
232 |
/** |
|
233 |
* 视频类型 |
|
234 |
*/ |
|
235 |
VIDEO |
|
236 |
} |
|
237 |
|
|
238 |
} |