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 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(); } } }