zhubaomin
2024-10-28 1e409f99905c894363248239b856bce6735fd3d5
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.dy.pipIrrWebFile.download;
 
import com.dy.pipIrrGlobal.dyFile.FileOperate;
import com.dy.pipIrrGlobal.dyFile.FileRestVo;
import com.dy.pipIrrGlobal.pojoFi.WebFile;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
 
/**
 * web文件下载
 */
@Slf4j
@RestController
@RequestMapping(path="download")
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked")
public class DownloadFileCtrl {
 
    @Autowired
    private DownloadFileSv sv ;
 
    @Autowired
    private FileOperate fileOp ;
 
    @Value("${dy.webFile.fmUrl}")
    private String fmUrl ;
 
 
    /**
     * 通过web文件数据库实体id下载该文件
     * @param id
     * @return
     */
    @GetMapping("/down")
    public void down(String id, HttpServletRequest req, HttpServletResponse rep){
        try{
            WebFile fPo = sv.selectById(id) ;
            if(fPo != null){
                FileRestVo frVo = fileOp.parseHashcode(fmUrl, fPo.hash) ;
                if(frVo.fileSysAbsolutePath != null){
                    if(!frVo.fileSysAbsolutePath.endsWith("\\\\") && !frVo.fileSysAbsolutePath.endsWith("/") ){
                        frVo.fileSysAbsolutePath = frVo.fileSysAbsolutePath + "/" ;
                    }
                }
                String filePath = frVo.fileSysAbsolutePath + fPo.filePath ;
                File f = new File(filePath) ;
                if(f.exists()){
                    String fileReName = fPo.orgName + "." + fPo.extName ;
                    //URLEncoder.encode可以防止中文乱码
                    fileReName = URLEncoder.encode(fileReName, "UTF-8").replaceAll("\\+", "%20");
                    rep.addHeader("content-type", "application/octet-stream");
                    rep.addHeader("Content-Disposition", "attachment;fileName=" + fileReName);
 
                    ServletOutputStream out = null;
                    FileInputStream in = null ;
                    try {
                        out = rep.getOutputStream() ;
                    } catch (Exception ee) {
                        out = null ;
                    }finally{
                        if(out != null){
                            byte[] bs = new byte[1024] ;
                            int len = -1 ;
                            try {
                                in = new FileInputStream(f);
                                len = in.read(bs) ;
                                while(len != -1){
                                    out.write(bs, 0, len);
                                    len = in.read(bs) ;
                                }
                            } catch (Exception eee) {
                            } finally {
                                if(out != null){
                                    try{
                                        out.flush();
                                        out.close();
                                    }catch(Exception e){
                                    }finally{
                                        if(in != null){
                                            try{
                                                in.close();
                                            }catch(Exception e){
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }else{
                }
            }else{
            }
        }catch (Exception e){
            log.error("下载文件时异常", e);
        }
 
    }
}