yanghb
2024-12-24 fe6e43d5e1144156d0ca4e9d6080c9821c25d97c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.zzg.common.utils;
 
import cn.hutool.core.util.IdUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
/**
 * Description: 文件处理工具类
 *
 * @author wangjiang
 * @date 2018/11/12
 */
public class ZZGFileUtil {
 
    private ZZGFileUtil() {
        throw new IllegalStateException("Utility class");
    }
 
    private static final Logger log = LoggerFactory.getLogger(ZZGFileUtil.class);
 
    /**
     * 后缀名与文件名的分隔符
     */
    public static final String FILE_PUF = ".";
 
    /**
     * 常用图片文件后缀名
     */
    public static final String IMAGE_SUFFIX = "bmp,jpg,png,gif,jpeg";
 
    /**
     * 获取文件后缀名
     *
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf(FILE_PUF) + 1);
    }
 
    /**
     * 去掉文件后缀名
     *
     * @param fileName
     * @return
     */
    public static String removeSuffix(String fileName) {
        return fileName.substring(0, fileName.lastIndexOf(FILE_PUF));
    }
 
    /**
     * 获得一个嵌入了UUID的文件
     *
     * @param fileName 原始文件名
     * @return 嵌入了UUID的文件名
     */
    public static String getUUIDFileName(String fileName) {
        return IdUtil.fastSimpleUUID() + "_" + fileName;
    }
 
 
    /**
     * 保存文件
     *
     * @param inputStream 输入流
     * @param pathName    要保存的文件名
     * @see #saveFile(InputStream, File)
     */
    public static void saveFile(InputStream inputStream, String pathName) {
        saveFile(inputStream, new File(pathName));
    }
 
    /**
     * 保存文件
     *
     * @param inputStream 输入流
     * @param file        要保存的文件
     */
    public static void saveFile(InputStream inputStream, File file) {
        try {
            FileUtils.copyInputStreamToFile(inputStream, file);
        } catch (IOException e) {
            log.error("保存文件失败", e);
            throw new RuntimeException("文件保存失败");
        }
    }
 
 
    /**
     * @param file     要输出的文件
     * @param fileName 下载时的文件名
     * @see #outFile(InputStream, String, String)
     */
    public static void outFile(File file, String fileName) {
        try (InputStream inputStream = FileUtils.openInputStream(file)) {
            outFile(inputStream, fileName, String.valueOf(file.length()));
        } catch (IOException e) {
            throw new RuntimeException("打开服务器文件失败" + e.getMessage(), e);
        }
    }
 
    /**
     * 实现文件下载
     *
     * @param input    输入流
     * @param fileName 文件名
     * @see #setOutFileHeader(HttpServletResponse, String, String)
     */
    public static void outFile(InputStream input, String fileName, String length) {
        try {
            HttpServletResponse response = ContextUtil.getServletResponse();
            setOutFileHeader(response, fileName, length);
            OutputStream output = response.getOutputStream();
            IOUtils.copyLarge(input, output);
        } catch (IOException e) {
            throw new RuntimeException("文件输出失败", e);
        }
    }
 
    /**
     * 拷贝文件到指定目录
     *
     * @param fileNames
     * @param destDir
     * @throws IOException
     */
    public static void synFiles(List<String> fileNames, String destDir) throws IOException {
        List<File> files = fileNames.stream().map(File::new).collect(Collectors.toList());
        File destFile = new File(destDir);
        if (!destFile.exists()) {
            boolean bool = destFile.mkdirs();
            if (!destFile.isDirectory() || !bool) {
                throw new IllegalAccessError("目录:" + destDir + "创建失败");
            }
        }
        FileUtils.copyToDirectory(files, destFile);
    }
 
    /**
     * @param response 响应请求
     * @param fileName 下载的文件名字
     * @param length   下载文件的长度
     */
    public static void setOutFileHeader(HttpServletResponse response, String fileName, String length) {
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        try {
            String encode = URLEncoder.encode(fileName, "UTF-8");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + encode + "\"");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("设置下载文件响应头失败!", e);
        }
        if (StringUtils.isNotBlank(length)) {
            response.addHeader("Content-Length", length);
        }
    }
 
    /**
     * @see #copyZipOut(ZipOutputStream, InputStream, ZipEntry)
     */
    public static void copyZipOut(ZipOutputStream outputStream, File... files) {
        for (File file : files) {
            try (InputStream inputStream = FileUtils.openInputStream(file)) {
                copyZipOut(outputStream, inputStream, new ZipEntry(file.getName()));
            } catch (IOException e) {
                throw new RuntimeException("打开服务器文件失败" + e.getMessage(), e);
            }
        }
    }
 
    /**
     * 将一个输入流数据 输出到压缩包里
     *
     * @param outputStream 输出流
     * @param inputStream  输入流
     * @param zipEntry     压缩对象
     */
    public static void copyZipOut(ZipOutputStream outputStream, InputStream inputStream, ZipEntry zipEntry) {
        try {
            outputStream.putNextEntry(zipEntry);
            IOUtils.copyLarge(inputStream, outputStream);
            outputStream.closeEntry();
        } catch (IOException e) {
            throw new RuntimeException("zip压缩输出文件:" + zipEntry.getName() + "时失败", e);
        }
    }
 
}