package com.mm.util;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.nio.channels.FileChannel;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipOutputStream;
|
|
@Slf4j
|
@Component
|
public class FileUtil {
|
|
|
|
public static String changeFileFormatKb(String flow) {
|
BigDecimal flows = new BigDecimal(flow);
|
if (flows.compareTo(new BigDecimal(0)) > 0 && flows.compareTo(new BigDecimal(1024)) < 0) {//小于1M
|
return flows.toString() + "B";
|
} else if(flows.compareTo(new BigDecimal(1024)) >= 0){
|
BigDecimal result = flows.divide(new BigDecimal(1024),2,BigDecimal.ROUND_HALF_UP);
|
return result.toString() + "KB";
|
} else {
|
return "0";
|
}
|
}
|
|
public static void copyFileBakZip(File file, File target) {
|
target = new File(target, file.getName());
|
if (!target.exists()) {
|
target.mkdirs();
|
}
|
if (file.isDirectory()) {
|
File[] files = file.listFiles();
|
for (File f : files) {
|
if (f.isDirectory()) {
|
copyFileBakZip(f, target);
|
}
|
else {
|
copyFileDeleteZip(f, target);
|
}
|
}
|
} else {
|
copyFileDeleteZip(file, target);
|
}
|
}
|
|
/**
|
*
|
* @param lastFile 文件路径 需要
|
* @param filePath 新路径
|
* @return
|
*/
|
public static boolean copyFile(String lastFile,String filePath,String bakFilePath) {
|
//文件迁移删除
|
String absolutePathSend = lastFile;
|
File abFile = new File(absolutePathSend);
|
if (abFile.getParentFile().exists()) {
|
/*File bak = new File(bakFilePath);
|
if (!bak.getParentFile().exists()){
|
//文件夹不存在 生成
|
bak.getParentFile().mkdirs();
|
}*/
|
deleteFile(filePath);
|
}
|
if (!abFile.getParentFile().exists()){
|
//文件夹不存在 生成
|
return false;
|
}
|
FileChannel in = null;
|
FileChannel out = null;
|
try{
|
in = new FileInputStream(absolutePathSend).getChannel();
|
File file = new File(filePath);
|
if (!file.getParentFile().exists()){
|
//文件夹不存在 生成
|
file.getParentFile().mkdirs();
|
}
|
out = new FileOutputStream(filePath, true).getChannel();
|
out.transferFrom(in, 0, in.size());
|
in.close();
|
out.close();
|
return true;
|
}
|
catch (IOException e) {
|
e.printStackTrace();
|
try {
|
if (in != null) {
|
in.close();
|
}
|
if (out != null) {
|
out.close();
|
}
|
} catch (IOException e1) {
|
e1.printStackTrace();
|
}
|
return false;
|
} finally {
|
try {
|
if (in != null) {
|
in.close();
|
}
|
if (out != null) {
|
out.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
public static boolean fileRecDelete(String lastFile) {
|
String absolutePathSend = lastFile;
|
Path path = Paths.get(lastFile);
|
try {
|
System.gc();
|
Files.delete(path);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
try{
|
File file = new File(lastFile);
|
file.delete();
|
return true;
|
}
|
catch (Exception e) {
|
e.printStackTrace();
|
return false;
|
}
|
}
|
public static Boolean deleteFile(File file) {
|
//判断文件不为null或文件目录存在
|
if (file == null || !file.exists()) {
|
System.out.println("文件删除失败,请检查文件是否存在以及文件路径是否正确");
|
return false;
|
}
|
//获取目录下子文件
|
File[] files = file.listFiles();
|
//遍历该目录下的文件对象
|
if(files != null && files.length>0) {
|
for (File f : files) {
|
//判断子目录是否存在子目录,如果是文件则删除
|
if (f.isDirectory()) {
|
//递归删除目录下的文件
|
deleteFile(f);
|
} else {
|
//文件删除
|
f.delete();
|
//打印文件名
|
System.out.println("文件名:" + f.getName());
|
}
|
}
|
}
|
|
//文件夹删除
|
file.delete();
|
System.out.println("目录名:" + file.getName());
|
return true;
|
}
|
|
|
|
/**
|
* 删除文件
|
*
|
* @param filePath
|
* @return
|
*/
|
public static boolean deleteFile(String filePath) {
|
boolean flag = false;
|
File file = new File(filePath);
|
if (!file.exists()) {
|
return flag;
|
}
|
boolean b = file.delete();
|
if (!b) {
|
try {
|
System.gc();
|
Path path = Paths.get(filePath);
|
Files.deleteIfExists(path);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return true;
|
}
|
|
|
public static void compressFolder(File folder, String zipFilePath) throws IOException {
|
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
|
compressFolder(folder, folder.getName(), zipOut);
|
zipOut.close();
|
}
|
|
private static void compressFolder(File folder, String parentFolder, ZipOutputStream zipOut) throws IOException {
|
if (folder.isFile()) {
|
String filePath = folder.getPath();
|
String entryPath = filePath.substring(parentFolder.length() + 1);
|
ZipEntry zipEntry = new ZipEntry(entryPath);
|
zipOut.putNextEntry(zipEntry);
|
FileInputStream fileIn = new FileInputStream(filePath);
|
byte[] buffer = new byte[1024];
|
int bytesRead;
|
while ((bytesRead = fileIn.read(buffer)) != -1) {
|
zipOut.write(buffer, 0, bytesRead);
|
}
|
fileIn.close();
|
zipOut.closeEntry();
|
} else {
|
for (File file : folder.listFiles()) {
|
compressFolder(file, parentFolder, zipOut);
|
}
|
}
|
}
|
|
|
|
|
|
|
public static void copyFileDeleteZip(File file, File directory) {
|
try {
|
FileInputStream input = new FileInputStream(file);
|
FileOutputStream output = new FileOutputStream(new File(directory, file.getName()));
|
byte[] buff = new byte[8192];
|
int len = -1;
|
while ((len = input.read(buff)) > -1) {
|
output.write(buff);
|
}
|
input.close();
|
output.close();
|
} catch (Exception e) {
|
|
}
|
}
|
|
|
|
/**
|
* 上传文件工具类
|
* @param multipartFile
|
* @param path
|
* @param fileNewName 新的文件名
|
* @return
|
*/
|
public static boolean uploadFile(MultipartFile multipartFile, String path, String fileNewName) {
|
File targetFile = new File(path, fileNewName);
|
if(!targetFile.getParentFile().exists()){
|
targetFile.getParentFile().mkdirs();
|
}
|
try {
|
multipartFile.transferTo(targetFile);
|
return true;
|
} catch (Exception e) {
|
log.error(e.getMessage(), e.getStackTrace());
|
return false;
|
}
|
}
|
|
/**
|
/**
|
* 获取文件后缀 无后缀文件返回NULL
|
* @param fileName
|
* @return
|
*/
|
public static String getFileSuffix(String fileName) {
|
if (fileName.contains(".")) {
|
String suffix = fileName.substring(fileName.lastIndexOf('.') + 1);
|
return suffix;
|
}else {
|
return null;
|
}
|
|
}
|
|
/**
|
* 获取文件名 不带后缀和点号
|
* @param fileName
|
* @return
|
*/
|
public static String getFilenameNonSuffix(String fileName) {
|
if (fileName.contains(".")) {
|
String filename = fileName.substring(0, fileName.lastIndexOf('.'));
|
return filename;
|
}else {
|
return fileName;
|
}
|
|
}
|
|
|
|
|
|
}
|