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