kongdeqiang
2023-12-18 05e0bb9b28295d1e80c6c47783e53d6879285198
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.boying.service.impl;
 
import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boying.common.SystemConfigProperties;
import com.boying.entity.EnterPark;
import com.boying.entity.FileInfo;
import com.boying.mapper.EnterParkMapper;
import com.boying.mapper.FileInfoMapper;
import com.boying.service.FileInfoService;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
@Service
@AllArgsConstructor
public class FileInfoServiceImpl  extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {
 
    private final SystemConfigProperties systemConfigProperties;
    private final FileInfoMapper fileInfoMapper;
    @Override
    public String downloadPathFile(String downloadPath, HttpServletRequest request, HttpServletResponse response) {
        //设置文件路径
        File file = new File(downloadPath);
        //获取文件名称
        String fileName = file.getName();
        //判断文件是否存在
        if (file.exists()) {
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                file.delete();
                return "下载成功";
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return "下载失败";
    }
 
    @Override
    public void downForZip(List<FileInfo> fileInfoList, HttpServletRequest req, HttpServletResponse response) {
        // 创建临时文件
        File zipFile = null;
        FileInputStream fis = null;
        BufferedInputStream buff = null;
        try {
            //临时文件名称
            zipFile = File.createTempFile("test", ".zip");
            FileOutputStream fot = new FileOutputStream(zipFile);
            // 为任何OutputStream产生校验,第一个参数是制定产生校验和的输出流,第二个参数是指定Checksum的类型 (Adler32(较快)和CRC32两种)
            CheckedOutputStream cos = new CheckedOutputStream(fot, new Adler32());
            // 用于将数据压缩成Zip文件格式
            ZipOutputStream zos = new ZipOutputStream(cos);
            for (FileInfo file: fileInfoList) {
                //根据文件信息获取输入流。这个看实际的业务目的就是获取当前文件的输入流
                InputStream inputStream = new FileInputStream(new File(file.getFileBaseDir()+file.getFileName()+file.getFileLast()));
                if (null == inputStream) {
                    System.out.println("输入流为空");
                    break;
                }
                // 对于每一个要被存放到压缩包的文件,都必须调用ZipOutputStream对象的putNextEntry()方法,确保压缩包里面文件不同名
                //多个文件名称重复时zos.putNextEntry()会报错!! 可以再文件名称后面加编号等操作
                zos.putNextEntry(new ZipEntry(file.getFileName()+file.getFileLast()));
                int bytesRead = 0;
                // 向压缩文件中输出数据
                while ((bytesRead = inputStream.read()) != -1) {
                    System.out.println("写入数据");
                    zos.write(bytesRead);
                }
                inputStream.close();
                // 当前文件写完,写入下一个文件
                zos.closeEntry();
            }
            zos.close();
            ServletOutputStream os = response.getOutputStream();
            //下载文件,使用spring框架中的FileCopyUtils工具
            //response.setCharacterEncoding("GB2312");
            response.setContentType("application/octet-stream");
            //设置响应头,attachment表示以附件的形式下载,inline表示在线打开
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            response.setHeader("Content-Disposition", "attachment;fileName=" + new String((sdf.format(new Date())+".zip").getBytes("utf-8"),"iso8859-1"));
            fis = new FileInputStream(zipFile);
            buff = new BufferedInputStream(fis);
            FileCopyUtils.copy(buff, os);
        } catch (Exception e1) {
            System.out.println("批量下载失败");
            e1.printStackTrace();
            // 关闭流
        } finally {
            try {
                if (null != fis) {
                    fis.close();
                }
                if (null != buff) {
                    buff.close();
                }
            } catch (IOException e) {
                System.out.println("流关闭异常");
                log.error( "流关闭异常");
            }
            // 删除临时文件
            if (null != zipFile) {
                zipFile.delete();
            }
        }
    }
 
    @Override
    public void downloadPng(FileInfo byId, HttpServletRequest request, HttpServletResponse response) {
        try {
            InputStream input = new FileInputStream(new File(byId.getFileBaseDir()+byId.getFileName()+byId.getFileLast()));
            response.setContentType("application/octet-stream; charset=UTF-8");
            IoUtil.copy(input, response.getOutputStream());
        }catch (Exception e){
            System.out.println("文件读取异常,"+e.getLocalizedMessage());
        }
    }
 
    @Override
    public void getFile(String downloadPath, HttpServletResponse response) {
        try {
            InputStream input = new FileInputStream(new File(systemConfigProperties.getUploadImgPath()+downloadPath));
            response.setContentType(MediaType.IMAGE_PNG_VALUE);
            IoUtil.copy(input, response.getOutputStream());
        }catch (Exception e){
            System.out.println("文件读取异常,"+e.getLocalizedMessage());
        }
    }
 
    @Override
    public void getFileById(Long fileId, HttpServletResponse response) {
        try {
            FileInfo fileInfo = fileInfoMapper.selectById(fileId);
            InputStream input = new FileInputStream(new File(fileInfo.getFileBaseDir()+fileInfo.getFileName()+fileInfo.getFileLast()));
            response.setContentType(MediaType.IMAGE_PNG_VALUE);
            IoUtil.copy(input, response.getOutputStream());
        }catch (Exception e){
            System.out.println("文件读取异常,"+e.getLocalizedMessage());
        }
    }
}