zjh
2 天以前 14ecea8537d5b29ca64c75aad4ff49265018415d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
        }
    }
}