pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.1</version>
</dependency>
FileUtil
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtil {
/**
* 功能描述:
* <存储文件>
*
* @param file 1
* @param serverPath 2
* @param filename 3
* @return java.lang.String
* @author zhoulipu
* @date 2019/9/16 16:04
*/
public static String uploadFile(MultipartFile file, String serverPath, String filename) {
File myPath = new File(serverPath);
if (!myPath.exists()) {
myPath.mkdir();
}
try (InputStream in = file.getInputStream();
FileOutputStream out = new FileOutputStream(serverPath + filename)) {
int bs = 1024;
byte[] buff = new byte[bs];
int length = 0;
//循环读取
while ((length = in.read(buff, 0, bs)) > 0) {
byte[] data = new byte[length];
System.arraycopy(buff, 0, data, 0, length);
out.write(buff, 0, length);
out.flush();
}
} catch (IOException e) {
e.getStackTrace();
}
return serverPath + filename;
}
/**
* 功能描述:
* <复制文件>
*
* @param filePath 1
* @param newPath 2
* @return void
* @author zhoulipu
* @date 2019/9/2 11:11
*/
public static void copyFile(String filePath, String newPath) {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(filePath);//创建输入流对象
out = new FileOutputStream(newPath); //创建输出流对象
byte buff[] = new byte[1024 * 8];//创建搬运工具
int len = 0;//创建长度
//循环读取数据
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 功能描述:
* <移动文件(修改文件路径&名称)>
*
* @param filePath 1
* @param newPath 2
* @return void
* @author zhoulipu
* @date 2019/9/2 11:11
*/
public static void moveFile(String filePath, String newPath) {
try {
FileUtils.moveFile(new File(filePath), new File(newPath));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 功能描述:
* <文件或文件夹压缩成zip包>
*
* @param sourcePath 1
* @param zipPath 2
* @return void
* @author zhoulipu
* @date 2019/9/16 16:06
*/
public static void compressFile(String sourcePath, String zipPath) {
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(zipPath);
zos = new ZipOutputStream(fos);
//zos.setEncoding("gbk");//此处修改字节码方式。
writeZip(new File(sourcePath), "", zos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (zos != null) {
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 功能描述:
* <写入zip包>
*
* @param file 1
* @param parentPath 2
* @param zos 3
* @return void
* @author zhoulipu
* @date 2019/9/16 16:07
*/
private static void writeZip(File file, String parentPath, ZipOutputStream zos) {
long start = System.currentTimeMillis();
//测试此抽象路径名表示的文件或目录是否存在。
if (file.exists()) {
try {
compress(file, zos, file.getName(), true);
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 功能描述:
* <文件压缩>
*
* @param sourceFile 1
* @param zos 2
* @param name 3
* @param isKeepDirStructure 4
* @return void
* @author zhoulipu
* @date 2019/9/16 16:07
*/
private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean isKeepDirStructure) {
FileInputStream in = null;
try {
byte[] buf = new byte[1024 * 2];
if (sourceFile.isFile()) {
// 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
zos.putNextEntry(new ZipEntry(name));
// copy文件到zip输出流中
int len;
in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
// Complete the entry
zos.closeEntry();
} else {
File[] listFiles = sourceFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
// 需要保留原来的文件结构时,需要对空文件夹进行处理
if (isKeepDirStructure) {
// 空文件的处理
zos.putNextEntry(new ZipEntry(name));
// 没有文件,不需要文件的copy
zos.closeEntry();
}
} else {
for (File file : listFiles) {
// 判断是否需要保留原来的文件结构
if (isKeepDirStructure) {
// 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
compress(file, zos, name + "/" + file.getName(), true);
} else {
compress(file, zos, file.getName(), false);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.getStackTrace();
}
}
}
}
/**
* 功能描述:
* <获取文件大小>
*
* @param fileLength 1 : MultipartFile.getSize()
* @param unit 2
* @return double
* @author zhoulipu
* @date 2019/9/2 10:31
*/
public static double getFileSize(Long fileLength, String unit) {
double fileSize = 0;
switch (unit){
case "B":
fileSize = (double) fileLength;
break;
case "K":
fileSize = (double) fileLength / 1024;
break;
case "M":
fileSize = (double) fileLength / 1048576;
break;
case "G":
fileSize = (double) fileLength / 1073741824;
break;
}
return fileSize;
}
}