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);
|
}
|
}
|
|
}
|