“mitao”
2026-01-20 4e5beb844f03daec12f2b039365730b0683f8a9e
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.estate.expert.admin.controller.admin;
 
import com.estate.expert.admin.domain.misc.UserFile;
import com.estate.expert.admin.enums.BizResultCode;
import com.estate.expert.admin.service.UserFileService;
import com.estate.expert.common.auth.IgnoreAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.net.URLEncoder;
 
@Api(tags = "管理后台/文件管理")
@CrossOrigin
@Slf4j
@Controller
@RequestMapping("/public/file")
public class UserFileDownloadController {
 
    @Resource
    UserFileService userFileService;
 
    @SneakyThrows
    @ApiOperation(value = "下载文件(名称)")
    @IgnoreAuth
    @GetMapping("/download/{name}")
    public ResponseEntity<org.springframework.core.io.Resource> downloadFileByName(@PathVariable String name) {
        var userFile = userFileService.getByName(name);
        BizResultCode.FILE_NOT_FOUND.check(userFile == null);
 
        return downloadFile(userFile);
    }
 
    @SneakyThrows
    @ApiOperation(value = "下载文件(文件ID)")
    @IgnoreAuth
    @GetMapping("/download/file")
    public ResponseEntity<org.springframework.core.io.Resource> downloadFileById(@RequestParam(name = "id") Integer id) {
        var userFile = userFileService.get(id);
        BizResultCode.FILE_NOT_FOUND.check(userFile == null);
 
        return downloadFile(userFile);
    }
 
    @ApiOperation(value = "图片显示")
    @IgnoreAuth
    @GetMapping("/image/{name}")
    public ResponseEntity<org.springframework.core.io.Resource> showImage(@PathVariable String name) {
        var userFile = userFileService.getByName(name);
        BizResultCode.FILE_NOT_FOUND.check(userFile == null);
 
        return ResponseEntity.ok()
                .contentLength(userFile.getSize())
                .contentType(MediaType.parseMediaType(userFile.getContentType()))
                .body(userFileService.resolveResource(userFile));
    }
 
    private ResponseEntity<org.springframework.core.io.Resource> downloadFile(UserFile userFile) throws IOException {
        InputStreamResource resource = userFileService.resolveResource(userFile);
        var fileSize = userFile.getSize();
//        if(resource == null) {
//            resource = new InputStreamResource(getClass().getClassLoader().getResourceAsStream("images/wukong.png"));
//            fileSize = 6215360L;// resource.contentLength();
//        }
        BizResultCode.FILE_NOT_FOUND.check(resource==null || !resource.exists());
 
        String encodedFileName = URLEncoder.encode(userFile.getOriginFileName(), "UTF-8");
        String headerValue = String.format("attachment; filename=\"%s\"", encodedFileName);
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(userFile.getContentType()))
                .contentLength(fileSize)
                .header(HttpHeaders.CONTENT_DISPOSITION, headerValue)
                .body(resource);
    }
 
}