zhaowenxuan
2025-04-27 44e994e9d3d39dec4d9995a77082b052523509fc
上传问题查看接口
1个文件已添加
53 ■■■■■ 已修改文件
ltkj-admin/src/main/java/com/ltkj/web/controller/ProfileuploadController.java 53 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ltkj-admin/src/main/java/com/ltkj/web/controller/ProfileuploadController.java
New file
@@ -0,0 +1,53 @@
package com.ltkj.web.controller;
import com.ltkj.common.config.ltkjConfig;
import io.lettuce.core.dynamic.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
 * @Company: 西安路泰科技有限公司
 * @Author: zhaowenxuan
 * @Date: 2025/4/27 18:03
 */
@RestController
@RequestMapping("/profileupload")
public class ProfileuploadController {
    @GetMapping("/{type}/{year}/{month}/{day}/{filename:.+}")
    public ResponseEntity<Resource> getFile(
            @PathVariable String type,
            @PathVariable String year,
            @PathVariable String month,
            @PathVariable String day,
            @PathVariable String filename) {
        try {
            // 拼接完整路径
            Path filePath = Paths.get(ltkjConfig.getUploadPath(),type, year, month, day, filename);
            Resource resource = new UrlResource(filePath.toUri());
            if (!resource.exists()) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + resource.getFilename() + "\"")
                    .contentType(MediaType.IMAGE_JPEG) // 这里默认图片是jpg,如果有别的类型可以加判断
                    .body(resource);
        } catch (MalformedURLException e) {
            return ResponseEntity.badRequest().build();
        }
    }
}