|
@@ -1,5 +1,7 @@
|
1
|
1
|
package com.yunzhi.inte.controller;
|
2
|
2
|
|
|
3
|
+import com.yunzhi.inte.common.ResponseBean;
|
|
4
|
+import org.springframework.beans.factory.annotation.Value;
|
3
|
5
|
import org.springframework.web.bind.annotation.*;
|
4
|
6
|
import org.springframework.web.multipart.MultipartFile;
|
5
|
7
|
import javax.servlet.http.HttpServletResponse;
|
|
@@ -7,66 +9,63 @@ import java.io.File;
|
7
|
9
|
import java.io.FileInputStream;
|
8
|
10
|
import java.io.IOException;
|
9
|
11
|
import java.io.OutputStream;
|
|
12
|
+import java.text.SimpleDateFormat;
|
|
13
|
+import java.time.LocalDateTime;
|
|
14
|
+import java.time.format.DateTimeFormatter;
|
|
15
|
+import java.util.HashMap;
|
10
|
16
|
|
11
|
17
|
@RestController
|
12
|
18
|
@RequestMapping("/")
|
13
|
19
|
public class ImgVidController {
|
|
20
|
+
|
|
21
|
+ @Value("${my.upload-path}")
|
|
22
|
+ String uploadPath;
|
|
23
|
+
|
|
24
|
+ @Value("${my.server-base}")
|
|
25
|
+ String serverBase;
|
|
26
|
+
|
14
|
27
|
/**
|
15
|
28
|
* 图片上传。
|
16
|
29
|
*
|
17
|
30
|
* @param fileUpload
|
18
|
31
|
* @return JSONObject
|
19
|
32
|
*/
|
20
|
|
- @PostMapping(value = "/image/upload" )
|
|
33
|
+ @PostMapping(value = "/upload" )
|
21
|
34
|
@ResponseBody
|
22
|
|
- public void imageUpload(@RequestParam("file") MultipartFile fileUpload) {
|
|
35
|
+ public ResponseBean imageUpload(@RequestParam("file") MultipartFile fileUpload) throws Exception {
|
23
|
36
|
|
24
|
37
|
//获取文件名
|
25
|
38
|
String fileName = fileUpload.getOriginalFilename();
|
26
|
|
- String tmpFilePath = "E://image//" ;
|
|
39
|
+ String newFileName = String.format("%s/%d-%s", getMonth(), System.currentTimeMillis(), fileName);
|
27
|
40
|
|
28
|
|
- //没有路径就创建路径
|
29
|
|
- File tmp = new File(tmpFilePath);
|
|
41
|
+ // 上传目录以日期分类
|
|
42
|
+ File tmp = new File(String.format("%s/%s", getUploadPath(), getMonth()));
|
30
|
43
|
if (!tmp.exists()) {
|
31
|
|
- tmp.mkdirs();
|
|
44
|
+ tmp.mkdir();
|
32
|
45
|
}
|
33
|
|
- String resourcesPath = tmpFilePath + "//" + fileName;
|
34
|
46
|
|
35
|
|
- File upFile = new File(resourcesPath);
|
36
|
|
- try {
|
37
|
|
- fileUpload.transferTo(upFile);
|
38
|
|
- } catch (IOException e) {
|
39
|
|
- e.printStackTrace();
|
40
|
|
- }
|
|
47
|
+ //
|
|
48
|
+ File upFile = new File(String.format("%s/%s", getUploadPath(), newFileName));
|
|
49
|
+ fileUpload.transferTo(upFile);
|
41
|
50
|
|
|
51
|
+ return ResponseBean.success(new HashMap<String, Object>(){{
|
|
52
|
+ put("url", String.format("%s/%s", serverBase, newFileName));
|
|
53
|
+ }});
|
42
|
54
|
}
|
43
|
55
|
|
44
|
|
- /**
|
45
|
|
- * 视频上传。
|
46
|
|
- *
|
47
|
|
- * @param fileUpload
|
48
|
|
- * @return JSONObject
|
49
|
|
- */
|
50
|
|
- @PostMapping(value = "/video/upload" )
|
51
|
|
- @ResponseBody
|
52
|
|
- public void videoUpload(@RequestParam("file") MultipartFile fileUpload) {
|
53
|
|
-
|
54
|
|
- //获取文件名
|
55
|
|
- String fileName = fileUpload.getOriginalFilename();
|
56
|
|
- String tmpFilePath = "E://video//" ;
|
57
|
|
- //没有路径就创建路径
|
58
|
|
- File tmp = new File(tmpFilePath);
|
|
56
|
+ private String getUploadPath() {
|
|
57
|
+ File tmp = new File(uploadPath);
|
59
|
58
|
if (!tmp.exists()) {
|
60
|
|
- tmp.mkdirs();
|
61
|
|
- }
|
62
|
|
- String resourcesPath = tmpFilePath + "//" + fileName;
|
63
|
|
- File upFile = new File(resourcesPath);
|
64
|
|
- try {
|
65
|
|
- fileUpload.transferTo(upFile);
|
66
|
|
- } catch (IOException e) {
|
67
|
|
- e.printStackTrace();
|
|
59
|
+ tmp.mkdir();
|
68
|
60
|
}
|
69
|
61
|
|
|
62
|
+ return uploadPath;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ private String getMonth() {
|
|
66
|
+ LocalDateTime now = LocalDateTime.now();
|
|
67
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
|
|
68
|
+ return now.format(formatter);
|
70
|
69
|
}
|
71
|
70
|
}
|
72
|
71
|
|