|
@ -1,389 +0,0 @@
|
1
|
|
package io.renren.modules.sys.controller;
|
2
|
|
|
3
|
|
import java.io.File;
|
4
|
|
import java.io.IOException;
|
5
|
|
import java.text.DecimalFormat;
|
6
|
|
import java.text.SimpleDateFormat;
|
7
|
|
import java.time.LocalDateTime;
|
8
|
|
import java.time.format.DateTimeFormatter;
|
9
|
|
import java.util.ArrayList;
|
10
|
|
import java.util.HashMap;
|
11
|
|
import java.util.List;
|
12
|
|
import java.util.Map;
|
13
|
|
import java.util.UUID;
|
14
|
|
|
15
|
|
import javax.servlet.http.HttpServletRequest;
|
16
|
|
|
17
|
|
import org.springframework.beans.factory.annotation.Value;
|
18
|
|
import org.springframework.context.annotation.Configuration;
|
19
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
20
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
21
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
22
|
|
import org.springframework.web.bind.annotation.RestController;
|
23
|
|
import org.springframework.web.multipart.MultipartFile;
|
24
|
|
|
25
|
|
import io.renren.common.utils.R;
|
26
|
|
import net.coobird.thumbnailator.Thumbnails;
|
27
|
|
|
28
|
|
/**
|
29
|
|
* Captcha by huwhois time 2019/11/22
|
30
|
|
*/
|
31
|
|
@RestController
|
32
|
|
@RequestMapping("/sys/filemanager")
|
33
|
|
@Configuration
|
34
|
|
public class FileManagerController {
|
35
|
|
|
36
|
|
@Value("${fyhs-meeting.file.datapath}")
|
37
|
|
private String dataPath; // 读取配置文件中的指定目录
|
38
|
|
|
39
|
|
@Value("${fyhs-meeting.file.uploadFolder}")
|
40
|
|
private String uploadFolder; // 读取配置文件中的指定目录
|
41
|
|
|
42
|
|
@PostMapping("/uploadimg")
|
43
|
|
public R uploadImage(@RequestParam(value = "upload_file") MultipartFile file, HttpServletRequest request)
|
44
|
|
throws IOException {
|
45
|
|
boolean isThumb = false;
|
46
|
|
String thumb = request.getParameter("isthumb");
|
47
|
|
if (thumb != null && thumb != "" && thumb.equals("1")) {
|
48
|
|
isThumb = true;
|
49
|
|
}
|
50
|
|
// 根据相对路径转化为真实路径
|
51
|
|
// String rootpath =
|
52
|
|
// request.getSession().getServletContext().getRealPath(File.separator);//
|
53
|
|
// 获得web应用的绝对路径
|
54
|
|
// File createFile = new File(rootpath + "../data/upload/");
|
55
|
|
String activePath = request.getParameter("activepath");
|
56
|
|
// 上传的图片只允许是 png、jpg 或 gif 中的格式
|
57
|
|
if (file.getOriginalFilename().contains(".png") || file.getOriginalFilename().contains(".jpg")
|
58
|
|
|| file.getOriginalFilename().contains(".gif")) {
|
59
|
|
Map<String, Object> resultSave = saveFile(file, activePath, false);
|
60
|
|
|
61
|
|
if ((Integer) resultSave.get("status") != 0) {
|
62
|
|
return R.error((String) resultSave.get("msg"));
|
63
|
|
}
|
64
|
|
|
65
|
|
String filename = (String) resultSave.get("filename");
|
66
|
|
|
67
|
|
Map<String, Object> result = new HashMap<String, Object>();
|
68
|
|
|
69
|
|
result.put("picname", filename);
|
70
|
|
if (isThumb) {
|
71
|
|
int dot = filename.lastIndexOf(".");
|
72
|
|
if ((dot >-1) && (dot < (filename.length()))) {
|
73
|
|
String suffix = filename.substring(dot + 1);
|
74
|
|
String thumbFilaname = filename.substring(0, dot)+ "_200." + suffix;
|
75
|
|
Thumbnails.of(dataPath + filename).width(200).toFile(dataPath + filename);
|
76
|
|
result.put("thumb", thumbFilaname);
|
77
|
|
}
|
78
|
|
}
|
79
|
|
|
80
|
|
return R.ok(result);
|
81
|
|
} else {
|
82
|
|
return R.error("上传文件失败");
|
83
|
|
}
|
84
|
|
}
|
85
|
|
|
86
|
|
@PostMapping("/uploadpaper")
|
87
|
|
public R uploadPaper(@RequestParam(value = "upload_file") MultipartFile file, HttpServletRequest request) throws IOException {
|
88
|
|
if (!file.isEmpty()) {
|
89
|
|
String fileOldName = file.getOriginalFilename();
|
90
|
|
// 上传的文件只允许是 pdf、doc 或 docx 中的格式
|
91
|
|
if (fileOldName.contains(".doc") || fileOldName.contains(".docx")) {
|
92
|
|
try {
|
93
|
|
// 使用配置文件中的指定上传目录与当前年月新建文件夹
|
94
|
|
LocalDateTime ldt = LocalDateTime.now();
|
95
|
|
String today = ldt.format(DateTimeFormatter.BASIC_ISO_DATE);
|
96
|
|
String useUploadPath = uploadFolder + "/documents"+ "/" + today + "/"; // 文件上传使用的目录
|
97
|
|
String picDir = dataPath + useUploadPath; // 文件绝对路径
|
98
|
|
|
99
|
|
File createFile = new File(picDir);
|
100
|
|
if (!createFile.exists()) {// 判断文件是否存在如果不存在则自动创建文件夹
|
101
|
|
createFile.mkdir();
|
102
|
|
}
|
103
|
|
String uuid = UUID.randomUUID().toString().replace("-", "");// 随机生成一个唯一性的id 文件不重名
|
104
|
|
String suffix = fileOldName.substring(fileOldName.lastIndexOf(".") + 1);
|
105
|
|
String newFilename = uuid + "." + suffix;
|
106
|
|
File f = new File(picDir + newFilename);
|
107
|
|
if (f.exists()) {
|
108
|
|
f.delete();// 上传的文件已经存在,就删除保存最新的文件
|
109
|
|
}
|
110
|
|
file.transferTo(f); // 将上传的文件写入到系统中
|
111
|
|
return R.ok().put("filename", useUploadPath + newFilename);
|
112
|
|
} catch (Exception e) {
|
113
|
|
e.printStackTrace();
|
114
|
|
}
|
115
|
|
} else {
|
116
|
|
return R.error("上传文件失败, 只允许上传doc,docx文件");
|
117
|
|
}
|
118
|
|
}
|
119
|
|
return R.error("上传文件不能为空");
|
120
|
|
}
|
121
|
|
|
122
|
|
@PostMapping("/uploadfile")
|
123
|
|
public R uploadFile(@RequestParam(value = "upload_file") MultipartFile file, HttpServletRequest request) throws IOException {
|
124
|
|
|
125
|
|
String useUploadPath = uploadFolder + "/documents"; // 文件上传使用的目录
|
126
|
|
|
127
|
|
String fileOldName = file.getOriginalFilename();
|
128
|
|
// 上传的文件只允许是 pdf、doc 或 docx 中的格式
|
129
|
|
if (fileOldName.contains(".pdf") || fileOldName.contains(".doc") || fileOldName.contains(".docx")) {
|
130
|
|
Map<String, Object> resultSave = saveFile(file, useUploadPath, true);
|
131
|
|
if ((Integer) resultSave.get("status") != 0) {
|
132
|
|
return R.error((String) resultSave.get("msg"));
|
133
|
|
}
|
134
|
|
return R.ok(resultSave);
|
135
|
|
} else {
|
136
|
|
return R.error("上传文件失败, 只允许上传pdf,doc,docx文件");
|
137
|
|
}
|
138
|
|
}
|
139
|
|
|
140
|
|
/*
|
141
|
|
* 读取指定路径下的文件名和目录名
|
142
|
|
*/
|
143
|
|
@RequestMapping("/list")
|
144
|
|
public R list(HttpServletRequest request) {
|
145
|
|
Map<String, Object> result = new HashMap<String, Object>();
|
146
|
|
String activepath = request.getParameter("activepath");
|
147
|
|
// System.out.println(activepath);
|
148
|
|
String dir = "";
|
149
|
|
if (activepath != null && activepath != "") {
|
150
|
|
dir = activepath;
|
151
|
|
}
|
152
|
|
|
153
|
|
File file = new File(dataPath + dir);
|
154
|
|
// System.out.println(dataPath + dir);
|
155
|
|
File[] fileList = file.listFiles();
|
156
|
|
|
157
|
|
ArrayList<Map<String, Object>> resultList = new ArrayList<>();
|
158
|
|
Map<String, Object> rfile = new HashMap<String, Object>();
|
159
|
|
|
160
|
|
for (int i = 0; i < fileList.length; i++) {
|
161
|
|
File ifile = fileList[i];
|
162
|
|
|
163
|
|
String fileName = ifile.getName();
|
164
|
|
|
165
|
|
long time = fileList[i].lastModified();
|
166
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 设置格式
|
167
|
|
String modifyTime = format.format(time); // 获得带格式的字符串
|
168
|
|
|
169
|
|
String suffix = "";
|
170
|
|
String size = "";
|
171
|
|
if (ifile.isFile()) {
|
172
|
|
suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
|
173
|
|
long s = ifile.length();
|
174
|
|
size = sizeBKM(s);
|
175
|
|
}
|
176
|
|
rfile.put("fileName", fileName);
|
177
|
|
rfile.put("suffix", suffix);
|
178
|
|
rfile.put("size", size);
|
179
|
|
rfile.put("modifyTime", modifyTime);
|
180
|
|
|
181
|
|
resultList.add(rfile);
|
182
|
|
|
183
|
|
}
|
184
|
|
|
185
|
|
result.put("resultList", resultList);
|
186
|
|
result.put("activepath", dir);
|
187
|
|
// System.out.println(dir);
|
188
|
|
return R.ok(result);
|
189
|
|
}
|
190
|
|
|
191
|
|
public String sizeBKM(long filesize) {
|
192
|
|
String size = "";
|
193
|
|
if (filesize > 1024) {
|
194
|
|
DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
|
195
|
|
Double lsize = filesize / 1024d;
|
196
|
|
if (lsize > 1024) {
|
197
|
|
lsize = lsize / 1024;
|
198
|
|
|
199
|
|
size = decimalFormat.format(lsize) + "M";
|
200
|
|
} else {
|
201
|
|
size = decimalFormat.format(lsize) + "K";
|
202
|
|
}
|
203
|
|
} else {
|
204
|
|
size = filesize + "B";
|
205
|
|
}
|
206
|
|
return size;
|
207
|
|
}
|
208
|
|
|
209
|
|
/**
|
210
|
|
* 删除文件
|
211
|
|
*
|
212
|
|
* @param pathname
|
213
|
|
* @return
|
214
|
|
* @throws IOException
|
215
|
|
*/
|
216
|
|
public static boolean deleteFile(String pathname) {
|
217
|
|
boolean result = false;
|
218
|
|
File file = new File(pathname);
|
219
|
|
if (file.exists()) {
|
220
|
|
file.delete();
|
221
|
|
result = true;
|
222
|
|
}
|
223
|
|
return result;
|
224
|
|
}
|
225
|
|
|
226
|
|
/**
|
227
|
|
* 删除目录
|
228
|
|
*
|
229
|
|
* @param pathname
|
230
|
|
* @return
|
231
|
|
* @throws IOException
|
232
|
|
*/
|
233
|
|
public static boolean deleteDir(String pathname) {
|
234
|
|
boolean result = false;
|
235
|
|
File dir = new File(pathname);
|
236
|
|
if (dir.exists()) {
|
237
|
|
File[] files = dir.listFiles();
|
238
|
|
if (null == files) {
|
239
|
|
dir.delete();
|
240
|
|
result = true;
|
241
|
|
}
|
242
|
|
}
|
243
|
|
return result;
|
244
|
|
}
|
245
|
|
|
246
|
|
@PostMapping("/delete")
|
247
|
|
public R deleteFile(String activePath, String fileName) {
|
248
|
|
boolean result = false;
|
249
|
|
String dir = "";
|
250
|
|
if (activePath != null || activePath != "") {
|
251
|
|
dir = activePath;
|
252
|
|
}
|
253
|
|
|
254
|
|
String pathname = dataPath + dir + "/" + fileName;
|
255
|
|
// System.out.println(pathname);
|
256
|
|
File file = new File(pathname);
|
257
|
|
if (!file.exists()) {
|
258
|
|
return R.error(1, "文件or文件夹不存在");
|
259
|
|
}
|
260
|
|
|
261
|
|
if (file.isDirectory()) {
|
262
|
|
File[] files = file.listFiles();
|
263
|
|
if (null != files) {
|
264
|
|
for (File f : files) {
|
265
|
|
if (f.exists()) {
|
266
|
|
f.delete();
|
267
|
|
}
|
268
|
|
}
|
269
|
|
}
|
270
|
|
result = file.delete();
|
271
|
|
}
|
272
|
|
|
273
|
|
if (file.isFile()) {
|
274
|
|
result = file.delete();
|
275
|
|
}
|
276
|
|
|
277
|
|
if (!result) {
|
278
|
|
return R.error("删除失败");
|
279
|
|
}
|
280
|
|
return R.ok();
|
281
|
|
}
|
282
|
|
|
283
|
|
@RequestMapping("/filesupload")
|
284
|
|
public R filesUpload(@RequestParam("myfiles") MultipartFile[] files, HttpServletRequest request) {
|
285
|
|
List<String> list = new ArrayList<String>();
|
286
|
|
if (files != null && files.length > 0) {
|
287
|
|
for (int i = 0; i < files.length; i++) {
|
288
|
|
MultipartFile file = files[i];
|
289
|
|
// 保存文件
|
290
|
|
list = saveFile(file, list);
|
291
|
|
}
|
292
|
|
}
|
293
|
|
return R.ok().put("list", list);// 跳转的页面
|
294
|
|
}
|
295
|
|
|
296
|
|
private List<String> saveFile(MultipartFile file, List<String> list) {
|
297
|
|
// 判断文件是否为空
|
298
|
|
if (!file.isEmpty()) {
|
299
|
|
try {
|
300
|
|
// 使用配置文件中的指定上传目录与当前年月新建文件夹
|
301
|
|
LocalDateTime ldt = LocalDateTime.now();
|
302
|
|
String today = ldt.format(DateTimeFormatter.BASIC_ISO_DATE);
|
303
|
|
String useUploadPath = uploadFolder + "/" + today + "/";
|
304
|
|
String picDir = dataPath + useUploadPath; // 文件绝对路径
|
305
|
|
|
306
|
|
File createFile = new File(picDir);
|
307
|
|
if (!createFile.exists()) {// 判断文件是否存在如果不存在则自动创建文件夹
|
308
|
|
createFile.mkdir();
|
309
|
|
}
|
310
|
|
String fileOldName = file.getOriginalFilename();
|
311
|
|
// 上传的图片只允许是 png、jpg 或 gif 中的格式
|
312
|
|
if (file.getOriginalFilename().contains(".png") || file.getOriginalFilename().contains(".jpg")
|
313
|
|
|| file.getOriginalFilename().contains(".gif")) {
|
314
|
|
String uuid = UUID.randomUUID().toString().replace("-", "");// 随机生成一个唯一性的id 文件不重名
|
315
|
|
String suffix = fileOldName.substring(fileOldName.lastIndexOf(".") + 1);
|
316
|
|
String newFilename = uuid + "." + suffix;
|
317
|
|
File f = new File(picDir + newFilename);
|
318
|
|
if (f.exists()) {// 上传的文件已经存在,则提示用户重新上传 apk 或者重命名
|
319
|
|
// list.add("上传的文件已经存在,则提示用户重新上传 apk 或者重命名");
|
320
|
|
return list;
|
321
|
|
} else {
|
322
|
|
file.transferTo(f); // 将上传的文件写入到系统中
|
323
|
|
list.add(useUploadPath + newFilename);
|
324
|
|
}
|
325
|
|
}
|
326
|
|
} catch (Exception e) {
|
327
|
|
e.printStackTrace();
|
328
|
|
}
|
329
|
|
}
|
330
|
|
return list;
|
331
|
|
}
|
332
|
|
|
333
|
|
|
334
|
|
|
335
|
|
private Map<String, Object> saveFile(MultipartFile file) {
|
336
|
|
return saveFile(file, "", false); // 使用当前年月日目录,uuid新文件名
|
337
|
|
}
|
338
|
|
|
339
|
|
private Map<String, Object> saveFile(MultipartFile file, String desDir,
|
340
|
|
Boolean useOldname) {
|
341
|
|
Map<String, Object> result = new HashMap<String, Object>();
|
342
|
|
if (!file.isEmpty()) {
|
343
|
|
try {
|
344
|
|
String useUploadPath = "";
|
345
|
|
if (desDir != null && !desDir.equals("")) {
|
346
|
|
useUploadPath = desDir + "/";
|
347
|
|
} else {
|
348
|
|
// 使用配置文件中的指定上传目录与当前年月新建目录
|
349
|
|
LocalDateTime ldt = LocalDateTime.now();
|
350
|
|
String today = ldt.format(DateTimeFormatter.BASIC_ISO_DATE);
|
351
|
|
useUploadPath = uploadFolder + "/" + today + "/";
|
352
|
|
}
|
353
|
|
String absolutePath = dataPath + useUploadPath; // 目录绝对路径
|
354
|
|
|
355
|
|
// 判断目录是否存在, 如果不存在则自动创建目录
|
356
|
|
File createFile = new File(absolutePath);
|
357
|
|
if (!createFile.exists()) {
|
358
|
|
createFile.mkdir();
|
359
|
|
}
|
360
|
|
|
361
|
|
String newFilename = "";
|
362
|
|
String fileOldName = file.getOriginalFilename();
|
363
|
|
String suffix = fileOldName.substring(fileOldName.lastIndexOf(".") + 1);
|
364
|
|
if (useOldname) {
|
365
|
|
newFilename = fileOldName;
|
366
|
|
} else {
|
367
|
|
String uuid = UUID.randomUUID().toString().replace("-", "");// 随机生成一个唯一性的id 文件不重名
|
368
|
|
newFilename = uuid + "." + suffix;
|
369
|
|
}
|
370
|
|
|
371
|
|
File f = new File(absolutePath + newFilename);
|
372
|
|
if (f.exists()) {
|
373
|
|
result.put("status", 1); // 上传的文件已经存在,则提示用户重新上传图片或者重命名
|
374
|
|
result.put("msg", "上传的文件已经存在,则提示用户重新上传图片或者重命名"); // 上传的文件已经存在,则提示用户重新上传图片或者重命名
|
375
|
|
} else {
|
376
|
|
file.transferTo(f); // 将上传的文件写入到系统中
|
377
|
|
result.put("status", 0);
|
378
|
|
result.put("filename", useUploadPath + newFilename);
|
379
|
|
}
|
380
|
|
} catch (Exception e) {
|
381
|
|
e.printStackTrace();
|
382
|
|
result.put("status", 2);
|
383
|
|
result.put("msg", e.getMessage());
|
384
|
|
}
|
385
|
|
}
|
386
|
|
|
387
|
|
return result;
|
388
|
|
}
|
389
|
|
}
|