cuilei
7 天以前 b0ec9895cde2519bc085ac40acbeea89ae8b6f9d
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
package org.jeecg.modules.system.service.impl;
 
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oss.OssBootUtil;
import org.jeecg.modules.system.service.IAliUploadService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
 
/**
 * 
 * @ClassName: AliUploadServiceImpl</br>
 * @Description: TODO</br>
 * @author yk</br>
 * @date 2021年4月6日</br>
 *       </br>
 */
@Service
public class AliUploadServiceImpl implements IAliUploadService {
 
    @Value(value = "${jeecg.oss.endpoint}")
    private String endPoint;
 
    @Value(value = "${jeecg.oss.accessKey}")
    private String accessKeyId;
 
    @Value(value = "${jeecg.oss.secretKey}")
    private String accessKeySecret;
 
    @Value(value = "${jeecg.oss.bucketName}")
    private String bucketName;
 
    @Override
    @Transactional(rollbackFor = { Exception.class })
    public String uploadFile(MultipartFile file) throws Exception {
        String path = "mes/" + DateUtils.date2Str(DateUtils.yyyyMMdd.get());
        OssBootUtil.setEndPoint(endPoint);
        OssBootUtil.setAccessKeyId(accessKeyId);
        OssBootUtil.setAccessKeySecret(accessKeySecret);
        OssBootUtil.setBucketName(bucketName);
        return OssBootUtil.upload(file, path);
    }
 
    @Override
    public void downloadFile(HttpServletResponse response, String url, String fileName) {
        OssBootUtil.setEndPoint(endPoint);
        OssBootUtil.setAccessKeyId(accessKeyId);
        OssBootUtil.setAccessKeySecret(accessKeySecret);
        OssBootUtil.setBucketName(bucketName);
        InputStream inputStream = OssBootUtil.download(url);
 
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            response.setHeader("Content-Type", "application/octet-stream;charset=utf-8"); // 告诉浏览器输出内容为流
            // response.setHeader("Content-Disposition", "attachment;fileName="+ new
            // String(toFileName.getBytes("UTF-8"),"ISO-8859-1"));
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
            // fis = new FileInputStream(file);
            bis = new BufferedInputStream(inputStream);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                }
            }
        }
    }
 
}