Quellcode durchsuchen

路径接口返回图片信息

yusm vor 6 Monaten
Ursprung
Commit
4881d0f117

+ 2 - 0
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/controller/CommonUploadController.java

@@ -1,6 +1,8 @@
 package com.firerock.webttkuaiban.demos.controller;
 
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;

+ 87 - 0
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/controller/UploadControoler.java

@@ -0,0 +1,87 @@
+package com.firerock.webttkuaiban.demos.controller;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.springframework.http.MediaTypeFactory.getMediaType;
+
+@RestController
+@RequestMapping("/upload")
+public class UploadControoler {
+
+    @Value(value = "${upload.path}")
+    private String path;
+
+//    @GetMapping("/{fileName}")
+//    public Object getImgData(@PathVariable("fileName") String fileName) {
+//        File dir = new File(path);
+//        if (!dir.exists()) {
+//            dir.mkdirs();
+//        }
+//        File file = new File(dir, fileName);
+//        if (!file.exists()) {
+//            //没有对应的图片则返回固定的
+//            return new File(dir, "20241111171010.png");
+//        } else {
+//            return file;
+//        }
+//    }
+
+    @GetMapping("/{fileNameNew}")
+    public ResponseEntity<InputStreamResource> getImgDataNew(@PathVariable("fileNameNew") String fileNameNew) {
+        File dir = new File(path);
+        if (!dir.exists()) {
+            dir.mkdirs();
+        }
+
+        File file = new File(dir, fileNameNew);
+        if (!file.exists()) {
+            file = new File(dir, "20241111171010.png"); // 默认图片
+        }
+
+        try {
+            InputStream inputStream = new FileInputStream(file);
+            InputStreamResource resource = new InputStreamResource(inputStream);
+
+            HttpHeaders headers = new HttpHeaders();
+            headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(file.length()));
+            headers.setContentType(MediaType.IMAGE_PNG); // 根据文件类型设置
+
+            return new ResponseEntity<>(resource, headers, HttpStatus.OK);
+        } catch (IOException e) {
+            // 处理异常
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
+        }
+
+    }
+
+    // 根据文件扩展名获取媒体类型
+    private MediaType getMediaType(String fileName) {
+        String extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
+        switch (extension) {
+            case "png":
+                return MediaType.IMAGE_PNG;
+            case "jpg":
+            case "jpeg":
+                return MediaType.IMAGE_JPEG;
+            case "gif":
+                return MediaType.IMAGE_GIF;
+            default:
+                return MediaType.APPLICATION_OCTET_STREAM; // fallback
+        }
+    }
+}

+ 1 - 1
fhKeeper/formulahousekeeper/webttkuaiban/src/main/resources/application.properties

@@ -26,7 +26,7 @@ spring.servlet.multipart.resolve-lazily=false
 #Swagger????
 mconfig.swagger-ui-open=true
 #??????�??
-#upload.path=C:/Users/12871/Downloads/
+#upload.path=D:/
 upload.path=/www/staticproject/Article/upload/
 default.pwd=1
 #freemarker