Commit 4b6c47cf authored by huyuchen's avatar huyuchen

huyc 项目档案代码提交

parent 0b9efb53
......@@ -40,6 +40,7 @@ import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
......@@ -84,21 +85,26 @@ public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoM
* @date: 2022/6/27
*/
@Override
public R<Boolean> importZip(MultipartFile zipFile) {
//C:\Users\登录用户~1\AppData\Local\Temp\
String pathName = System.getProperty("java.io.tmpdir") + "shpFileCache/";
String dec = System.getProperty("java.io.tmpdir") + "shpFileCache/";
//获取文件名(包括后缀)
String pname = zipFile.getOriginalFilename();
pathName = pathName + pname;
public R<Boolean> importZip(MultipartFile zipFile){
try {
String pathName = ResourceUtils.getURL("classpath:").getPath() + "unloap/";
String dec = ResourceUtils.getURL("classpath:").getPath() + "unloap/";
File file1 = new File(pathName);
//如果文件夹不存在 创建文件夹
if (!file1.exists()) {
file1.mkdir();
}
//获取文件名(包括后缀)
String pname = zipFile.getOriginalFilename();
pathName = pathName + pname;
File file = new File(pathName);
zipFile.transferTo(file);
// 获取解压出来的文件名 不带后缀
unZip(file, dec);
//解析完成删除本次解析中生成的文件 删除此目录下的所有文件
deleteFile(dec);
deleteFolder(dec);
} catch (Exception e) {
e.printStackTrace();
}
......@@ -136,8 +142,6 @@ public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoM
elecTypeMapData = dictMap.get("data");
elecTypeMap = elecTypeMapData.get("elec_archives_type");
}
//记录解压出来的所有文件名
List<String> filesName = new ArrayList<>();
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
......@@ -244,40 +248,72 @@ public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoM
return multipartFile;
}
/**
* 删除文件
*
* @param filePath
* @return
* 根据路径删除指定的目录或文件,无论存在与否
* @param path 要删除的目录或文件路径
* @return 删除成功返回 true,否则返回 false
*/
public static boolean deleteFile(String filePath) {
boolean flag = false;
File file = new File(filePath);
public static boolean deleteFolder(String path) {
File file = new File(path);
// 判断目录或文件是否存在
if (!file.exists()) {
return flag;
return false;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(path);
} else { // 为目录时调用删除目录方法
return deleteDirectory(path);
}
}
}
/**
* 删除单个文件
*/
private static boolean deleteFile(String path) {
File file = new File(path);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
return true;
}
if (!file.isDirectory()) {
return flag;
return false;
}
/**
* 删除目录(文件夹)以及目录下的文件
*/
private static boolean deleteDirectory(String path) {
//如果path不以文件分隔符结尾,自动添加文件分隔符
if (!path.endsWith(File.separator)) {
path = path + File.separator;
}
String[] tempList = file.list();
File temp;
for (int i = 0; i < tempList.length; i++) {
if (filePath.endsWith(File.separator)) {
temp = new File(filePath + tempList[i]);
} else {
temp = new File(filePath + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
File dirFile = new File(path);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
//删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (File file : files) {
//删除子文件
if (file.isFile()) {
flag = deleteFile(file.getAbsolutePath());
} //删除子目录
else {
flag = deleteDirectory(file.getAbsolutePath());
}
if (temp.isDirectory()) {
// 先删除文件夹里面的文件
deleteFile(filePath + "/" + tempList[i]);
// 再删除空文件夹
deleteFile(filePath + "/" + tempList[i]);
flag = true;
if (!flag) {
break;
}
}
return flag;
if (!flag) {
return false;
}
//删除当前目录
return dirFile.delete();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment