文件处理工具类
AppFileUtils
package com.example.hatech_env.server.easyExcel.utils;
import cn.com.hatechframework.utils.response.ResponseCode;
import com.example.hatech_env.config.constants.ExcelConstant;
import com.example.hatech_env.config.exception.BusinessException;
import com.example.hatech_env.server.common.enums.FileTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Slf4j
public class AppFileUtils {
private AppFileUtils() {
}
public static String PATH = "";
static {
try {
File file = new File("");
PATH = file.getCanonicalPath() + "\\src\\main\\resources\\template";
} catch (IOException e) {
e.printStackTrace();
}
}
public static ResponseEntity<Object> downloadFile(HttpServletResponse response, String path, String oldName) {
File file = new File(path);
if (file.exists()) {
try {
try {
oldName = URLEncoder.encode(oldName, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
byte[] bytes = FileUtils.readFileToByteArray(file);
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
header.setContentDispositionFormData("attachment", oldName);
ResponseEntity<Object> entity = new ResponseEntity<Object>(bytes, header, HttpStatus.CREATED);
return entity;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} else {
PrintWriter out;
try {
out = response.getWriter();
out.write("no file");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public static void deleteFileUsePath(String path) {
String realPath = PATH + path;
File file = new File(realPath);
if (file.exists()) {
file.delete();
}
}
public static String updateFileName(String filePath, String newFileName) {
Map<String, String> fileDirAndFileName = getFileDirAndFileName(filePath);
String dirPath = fileDirAndFileName.get("dir");
String relativeFilePath = fileDirAndFileName.get("fileName");
copyFile(filePath, dirPath, newFileName);
removeFileByPath(relativeFilePath);
return dirPath + File.separator + newFileName;
}
public static void removeFileByPath(String filePath) {
try {
File file = new File(PATH, filePath);
if (file.exists()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(String filePath, String newPath, String newFileName) {
if (StringUtils.isEmpty(filePath) && StringUtils.isEmpty(newPath) && StringUtils.isEmpty(newFileName)) {
return;
}
try (FileOutputStream fos = new FileOutputStream(newPath + File.separator + newFileName);
FileInputStream fis = new FileInputStream(new File(filePath))) {
final byte[] bytes = new byte[1024];
while (fis.read(bytes) != -1) {
fos.write(bytes);
}
fos.flush();
} catch (IOException e) {
log.error("文件复制出现问题!");
log.error(e.getMessage());
}
}
public static Map<String, String> getFileDirAndFileName(String filePath) {
if (StringUtils.isEmpty(filePath)) {
return null;
}
String dirPath = filePath.substring(0, filePath.lastIndexOf(File.separator));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
Map<String, String> map = new HashedMap<>();
map.put("pre", dirPath);
map.put("fileName", fileName);
return map;
}
public static void findFileList(File dir, List<String> fileNames) {
if (!dir.exists() || !dir.isDirectory()) {
return;
}
String[] files = dir.list();
assert files != null;
for (String s : files) {
File file = new File(dir, s);
if (file.isFile()) {
fileNames.add(dir + "\\" + file.getName());
log.info("fileNames:" + fileNames.toString());
} else {
findFileList(file, fileNames);
}
}
}
public static void checkFileType(MultipartFile file, FileTypeEnum fileTypeEnum) {
if (file == null) {
throw new BusinessException(ResponseCode.NOT_FOUND.code(), "请上传文件");
}
String fileName = file.getOriginalFilename();
if (StringUtils.isEmpty(fileName)) {
throw new BusinessException(ResponseCode.NOT_FOUND.code(), "请上传文件");
}
if (!fileName.toUpperCase().endsWith(fileTypeEnum.getValue())) {
throw new BusinessException(ResponseCode.NOT_FOUND.code(), "请上传." + fileTypeEnum.getValue() + "文件");
}
}
public static void excelZip() {
String fileName = ExcelConstant.ZIP_NAME;
File file_excel = new File(ExcelConstant.EXCEL_PATH);
File file_rar = new File(ExcelConstant.RAR_PATH);
List<String> fileNames = new ArrayList<>();
findFileList(file_excel, fileNames);
if (!file_rar.exists()) {
file_rar.mkdirs();
}
try (
FileOutputStream fos = new FileOutputStream(ExcelConstant.RAR_PATH + fileName);
ZipOutputStream zos = new ZipOutputStream(fos)
) {
for (String name : fileNames) {
try (
FileInputStream fis = new FileInputStream(name)
) {
String[] zipNames = ExcelConstant.EXCEL_PATH.split("[\\\\]");
ZipEntry zipEntry = new ZipEntry(name.split(zipNames[zipNames.length - 1])[1].substring(1));
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
while (fis.read(buffer) != -1) {
zos.write(buffer);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}