jialh
47 分钟以前 d28ee263b8a6fb95c9fe2add2ec58b7561d3fee9
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
110
111
package com.java110.api.controller.app.file;
 
import com.java110.core.client.FileUploadTemplate;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.user.UserDownloadFileDto;
import com.java110.intf.job.IUserDownloadFileV1InnerServiceSMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.PayUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
 
/**
 * 用户 下载中心中下载
 */
@RestController
@RequestMapping(value = "/app/file/userfile")
public class UserDownloadFileController {
    private final static Logger logger = LoggerFactory.getLogger(UserDownloadFileController.class);
 
    @Autowired
    private IUserDownloadFileV1InnerServiceSMO userDownloadFileV1InnerServiceSMOImpl;
 
    @Autowired
    private FileUploadTemplate fileUploadTemplate;
 
    // /app/file/userfile/download/
    @RequestMapping(path = "/download/{downloadId}/{token}", method = RequestMethod.GET)
    public void download(@PathVariable String downloadId,
                         @PathVariable String token,
                         HttpServletRequest request, HttpServletResponse response) {
 
        logger.debug("用户开始下载文件" + downloadId);
 
 
        UserDownloadFileDto userDownloadFileDto = new UserDownloadFileDto();
        userDownloadFileDto.setDownloadId(downloadId);
        //userDownloadFileDto.setDownloadUserId(userId);
        List<UserDownloadFileDto> userDownloadFileDtos = userDownloadFileV1InnerServiceSMOImpl.queryUserDownloadFiles(userDownloadFileDto);
        Assert.listOnlyOne(userDownloadFileDtos, "文件不存在");
 
        String date = DateUtil.getFormatTimeStringB(DateUtil.getCurrentDate());
        String newToken = PayUtil.md5(userDownloadFileDtos.get(0).getDownloadId() + date);
 
        if (!newToken.equals(token)) {
            throw new IllegalArgumentException("token 失效请刷新页面重新下载");
        }
 
        String tempUrl = userDownloadFileDtos.get(0).getTempUrl();
        String originalFileName = userDownloadFileDtos.get(0).getFileTypeName() + tempUrl.substring(tempUrl.lastIndexOf("/"));
        String fileName ;
        try {
            fileName = URLEncoder.encode(originalFileName, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        int lastDotIndex = fileName.lastIndexOf(".");
        if (lastDotIndex != -1) { // 确保存在点
            fileName = fileName.substring(0, lastDotIndex);
        }
        response.setHeader("content-type", "application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType("application/octet-stream");
 
        InputStream is = null;
        BufferedInputStream bis = null;
        byte[] buffer = new byte[1024];
 
        try {
            is = fileUploadTemplate.downloadFile(userDownloadFileDtos.get(0).getTempUrl());
            bis = new BufferedInputStream(is);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}