张延森 2 年前
父节点
当前提交
08a033be1f

二进制
db/ybai.db 查看文件


+ 35
- 36
src/main/java/com/yunzhi/inte/controller/ImgVidController.java 查看文件

@@ -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
 

+ 19
- 0
src/main/java/com/yunzhi/inte/exception/GlobalException.java 查看文件

@@ -0,0 +1,19 @@
1
+package com.yunzhi.inte.exception;
2
+
3
+import com.yunzhi.inte.common.ResponseBean;
4
+import org.springframework.web.bind.annotation.ControllerAdvice;
5
+import org.springframework.web.bind.annotation.ExceptionHandler;
6
+import org.springframework.web.bind.annotation.ResponseBody;
7
+
8
+@ControllerAdvice
9
+@ResponseBody
10
+public class GlobalException {
11
+    @ExceptionHandler(Exception.class)
12
+    public ResponseBean handleException(Exception e){
13
+        e.printStackTrace();
14
+        ResponseBean response = new ResponseBean();
15
+        response.setMessage(e.getMessage());
16
+        response.setCode(ResponseBean.ERROR_UNAVAILABLE);
17
+        return response;
18
+    }
19
+}

+ 10
- 2
src/main/resources/application.yml 查看文件

@@ -7,7 +7,7 @@ server:
7 7
 spring:
8 8
   datasource:
9 9
     driver-class-name: org.sqlite.JDBC
10
-    url: jdbc:sqlite:E:/interface/yunzhi/demo/src/main/resources/db/ybai.db
10
+    url: jdbc:sqlite:E:\work\jgz\service\db\ybai.db
11 11
     username:
12 12
     password:
13 13
   application:
@@ -15,7 +15,10 @@ spring:
15 15
   mvc:
16 16
     pathmatch:
17 17
       matching-strategy: ANT_PATH_MATCHER
18
-
18
+    static-path-pattern: ${my.static-prefix}/**
19
+  web:
20
+    resources:
21
+      static-locations: file:///${my.upload-path}
19 22
   servlet:
20 23
     multipart:
21 24
       max-file-size: 50MB #设置允许单个文件上传的大小
@@ -27,6 +30,11 @@ spring:
27 30
 #      ddl-auto: update
28 31
 #    show-sql: true
29 32
 
33
+my:
34
+  server-base: http://192.168.89.147:8087/api
35
+  upload-path: E:\work\jgz\upload
36
+  static-prefix: /assets
37
+
30 38
 ###
31 39
 mybatis-plus:
32 40
   configuration: