Commit 4b6c47cf authored by huyuchen's avatar huyuchen

huyc 项目档案代码提交

parent 0b9efb53
...@@ -40,6 +40,7 @@ import org.apache.commons.fileupload.FileItemFactory; ...@@ -40,6 +40,7 @@ import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile;
...@@ -84,21 +85,26 @@ public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoM ...@@ -84,21 +85,26 @@ public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoM
* @date: 2022/6/27 * @date: 2022/6/27
*/ */
@Override @Override
public R<Boolean> importZip(MultipartFile zipFile) { 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;
try { 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); File file = new File(pathName);
zipFile.transferTo(file); zipFile.transferTo(file);
// 获取解压出来的文件名 不带后缀 // 获取解压出来的文件名 不带后缀
unZip(file, dec); unZip(file, dec);
//解析完成删除本次解析中生成的文件 删除此目录下的所有文件 //解析完成删除本次解析中生成的文件 删除此目录下的所有文件
deleteFile(dec); deleteFolder(dec);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -136,8 +142,6 @@ public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoM ...@@ -136,8 +142,6 @@ public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoM
elecTypeMapData = dictMap.get("data"); elecTypeMapData = dictMap.get("data");
elecTypeMap = elecTypeMapData.get("elec_archives_type"); elecTypeMap = elecTypeMapData.get("elec_archives_type");
} }
//记录解压出来的所有文件名
List<String> filesName = new ArrayList<>();
// 判断源文件是否存在 // 判断源文件是否存在
if (!srcFile.exists()) { if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在"); throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
...@@ -244,40 +248,72 @@ public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoM ...@@ -244,40 +248,72 @@ public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoM
return multipartFile; return multipartFile;
} }
/** /**
* 删除文件 * 根据路径删除指定的目录或文件,无论存在与否
* * @param path 要删除的目录或文件路径
* @param filePath * @return 删除成功返回 true,否则返回 false
* @return
*/ */
public static boolean deleteFile(String filePath) { public static boolean deleteFolder(String path) {
boolean flag = false; File file = new File(path);
File file = new File(filePath); // 判断目录或文件是否存在
if (!file.exists()) { 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 false;
return flag; }
/**
* 删除目录(文件夹)以及目录下的文件
*/
private static boolean deleteDirectory(String path) {
//如果path不以文件分隔符结尾,自动添加文件分隔符
if (!path.endsWith(File.separator)) {
path = path + File.separator;
} }
String[] tempList = file.list(); File dirFile = new File(path);
File temp; //如果dir对应的文件不存在,或者不是一个目录,则退出
for (int i = 0; i < tempList.length; i++) { if (!dirFile.exists() || !dirFile.isDirectory()) {
if (filePath.endsWith(File.separator)) { return false;
temp = new File(filePath + tempList[i]); }
} else { boolean flag = true;
temp = new File(filePath + File.separator + tempList[i]); //删除文件夹下的所有文件(包括子目录)
} File[] files = dirFile.listFiles();
if (temp.isFile()) { for (File file : files) {
temp.delete(); //删除子文件
if (file.isFile()) {
flag = deleteFile(file.getAbsolutePath());
} //删除子目录
else {
flag = deleteDirectory(file.getAbsolutePath());
} }
if (temp.isDirectory()) { if (!flag) {
// 先删除文件夹里面的文件 break;
deleteFile(filePath + "/" + tempList[i]);
// 再删除空文件夹
deleteFile(filePath + "/" + tempList[i]);
flag = true;
} }
} }
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