将要上传的文件夹目录压缩,前端传给后端,后端解压文件目录,读取文件目录,保存文件目录
// 文件上传
String fileName = EvString.getUUID() + fileViewModel.getFileName();
FileUtils.decryptByBase64(fileViewModel.getBase64(), fileName);
try {
// 解压文件
FileUtils.unZipFiles(new File(fileName), EvCommonTool.getTempPath());
} catch (IOException e) {
e.printStackTrace();
}
List mapList=new ArrayList<>();
Floder floder=new Floder();
floder.setName(FileUtils.getFileName(fileViewModel.getFileName()));
floder.setParentId("-1");
floder.setId(EvString.getUUID());
mapList.add(floder);
// 解压后的文件
String upzipPath=FileUtils.getFileName(fileName)+"/"+FileUtils.getFileName(fileViewModel.getFileName());
// 读取文件
FileUtils.readfile(upzipPath,mapList,floder .getId());
for (Floder data:mapList){
save(data);
}
// 删除缓存文件
File file=new File(fileName);
FileUtils.delFile(file);
File unzipFile=new File(FileUtils.getFileName(fileName));
FileUtils.delFile(unzipFile);
package com.gkbim.Utils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* @ClassName:FileUtils
* @Description: 文件工具类
* @Author: liuhm
* @Date: 2019/9/3 9:57
*/
public class FileUtils {
/**
* @description: 去除后缀取文件名字
* @params:
* @return:
* @author: liuhm
* @Date: 2019/9/27 11:23
*/
public static String getFileName( String fileName) {
return fileName.substring(0,fileName.lastIndexOf("."));
}
/**
* @description: 解压文件到指定目录 解压后的文件名,和之前一致
* @param zipFile 待解压的zip文件 descDir 指定目录
* @author: liuhm
* @Date: 2019/9/27 11:03
*/
public static void unZipFiles(File zipFile, String descDir) throws IOException {
// 解决中文文件夹乱码
ZipFile zip = new ZipFile(zipFile,Charset.forName("GBK"));
String name = zip.getName().substring(zip.getName().lastIndexOf(File.separatorChar)+1, zip.getName().lastIndexOf('.'));
File pathFile = new File(descDir+name);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
for (Enumeration extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (descDir + name +"/"+ zipEntryName);
// 判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
if (!file.exists()) {
file.mkdirs();
}
// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()) {
continue;
}
// 输出文件路径信息
// System.out.println(outPath);
FileOutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
in.close();
out.close();
out.flush();
in.reset();
}
return;
}
/**
* @description: 读取某个文件夹下的所有文件
* @params:
* @return:
* @author: liuhm
* @Date: 2019/9/27 11:02
*/
public static boolean readfile(String filepath, List mapList, String parentId) {
File file = new File(filepath);
if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "/" + filelist[i]);
if (readfile.isDirectory()) {
Floder floder=new Floder();
floder.setParentId(parentId);
floder.setId(EvString.getUUID());
floder.setName(readfile.getName());
floder.setProj(readfile.getParentFile().getName());
mapList.add(floder);
readfile(filepath + "/" + filelist[i],mapList,floder.getId());
}
}
}
return true;
}
/**
* @description:
* @params: base64文件, name 文件名, prePath 前路径, relativePath 相对路径
* @return: void
* @author: liuhm
* @Date: 2019/9/3 9:55
*/
public static String uploadFile(String base64,String name,String prePath,String relativePath){
String[] aa = base64.split(",");
// 获取文件
String file = aa[1];
String ext=name.substring(name.lastIndexOf(".")+1);
// 拼写文件名
String fileName = EvString.getUUID() +"."+ext ;
// 绝对路径
String path = prePath + relativePath;
// 验证路径是否存在
try {
if (!(new File(path).isDirectory())) {
new File(path).mkdirs();
}
} catch (SecurityException e) {
e.printStackTrace();
throw new JrsfServiceException("创建文件夹失败");
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// 解密
byte[] b = decoder.decodeBuffer(file);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(path + fileName);
out.write(b);
out.flush();
out.close();
}catch (Exception e){
throw new JrsfServiceException("文件上传失败");
}
return fileName;
}
/**
* @description: 删除文件
* @params:
* @return:
* @author: liuhm
* @Date: 2019/9/6 17:02
*/
public static Boolean delFile(String prePath,String relativePath){
String path = prePath + relativePath;
return delPathFile(path);
}
/**
* @description: 删除文件 全路径
* @params: String path
* @return:
* @author: liuhm
* @Date: 2019/9/6 17:02
*/
public static Boolean delPathFile(String path){
File file = new File(path);
if (file.exists()) {
if (file.delete()) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* base64转化为文件.
*
* @param base64 base64
* @param filePath 目标文件路径
* @return boolean isTrue
*/
public static boolean decryptByBase64(String base64, String filePath) {
if (StringUtils.isBlank(base64) && StringUtils.isBlank(filePath)) {
return false;
}
try {
File dest = new File(filePath);
//不存在就创建路径
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
Files.write(Paths.get(filePath),
Base64.decodeBase64(base64.substring(base64.indexOf(",") + 1)), StandardOpenOption.CREATE);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
/**
* @description: 删除文件
* @params:
* @return:
* @author: liuhm
* @Date: 2019/9/27 11:55
*/
public static boolean delFile(File file) {
if (!file.exists()) {
return false;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
delFile(f);
}
}
return file.delete();
}
/**
* @description: 根据64位编码和文件绝对路径,创建文件
* @params: [base64, fileFullName]
* @return: void
* @author: wushiping
* @Date: 2019/3/5 17:49
*/
public static File base64ToFile(String base64, String fileFullName) {
Path path = Paths.get(fileFullName);
if (path == null || base64 == null) {
return null;
}
File file = null;
//创建文件目录
String filePath = path.getParent().toString();
File dir = new File(filePath);
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
BufferedOutputStream bos = null;
java.io.FileOutputStream fos = null;
try {
file = new File(fileFullName);
String type = getFileType(file.getName());
if (base64.startsWith("data:")){//截取base64的前缀
int index = base64.indexOf("base64,");
if (index>0){
base64 = base64.substring(index+7);
}
}
byte[] bytes = java.util.Base64.getDecoder().decode(base64);
fos = new java.io.FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
EvLog.error(e);
} finally {
if (bos != null) {
try {
bos.close();
bos.flush();
} catch (IOException e) {
EvLog.error(e);
}
}
if (fos != null) {
try {
fos.close();
fos.flush();
} catch (IOException e) {
EvLog.error(e);
}
}
}
return file;
}
/**
* @description: 获取文件类型
* @params: [fileFullName]
* @return: java.lang.String
* @author: liuhm
* @Date: 2019/3/5 18:16
*/
public static String getFileType(String fileName) {
String fileType = null;
if (!EvString.isEmpty(fileName)) {
int index = fileName.lastIndexOf(".");
if (index >= 0) {
fileType = fileName.substring(index + 1);
}
}
return fileType;
}
}