|
@@ -20,7 +20,7 @@ import java.util.stream.Collectors;
|
|
|
* 文件工具类
|
|
* 文件工具类
|
|
|
*/
|
|
*/
|
|
|
public class ServiceFileUtil {
|
|
public class ServiceFileUtil {
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ServiceFileUtil.class);
|
|
private static final Logger logger = LoggerFactory.getLogger(ServiceFileUtil.class);
|
|
|
|
|
|
|
|
private static final String createFilePath = "/home/siwei/uploadPath";
|
|
private static final String createFilePath = "/home/siwei/uploadPath";
|
|
@@ -28,83 +28,80 @@ public class ServiceFileUtil {
|
|
|
/**
|
|
/**
|
|
|
* 编码目录名
|
|
* 编码目录名
|
|
|
*/
|
|
*/
|
|
|
- public static final String extractFilename(String name)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public static final String extractFilename(String name) {
|
|
|
return StringUtils.format("{}/{}_{}", DateUtils.datePath(),
|
|
return StringUtils.format("{}/{}_{}", DateUtils.datePath(),
|
|
|
FilenameUtils.getBaseName(name), Seq.getId(Seq.uploadSeqType));
|
|
FilenameUtils.getBaseName(name), Seq.getId(Seq.uploadSeqType));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
//给定一个名称创建一个目录,返回路径
|
|
//给定一个名称创建一个目录,返回路径
|
|
|
- public static String createDirectory(String directoryName) {
|
|
|
|
|
|
|
+ public static String createDirectory(String directoryName) {
|
|
|
String directoryPathAndName = null;
|
|
String directoryPathAndName = null;
|
|
|
try {
|
|
try {
|
|
|
- directoryPathAndName =createFilePath+"/"+ extractFilename(directoryName);
|
|
|
|
|
|
|
+ directoryPathAndName = createFilePath + "/" + extractFilename(directoryName);
|
|
|
Path file = Paths.get(directoryPathAndName);
|
|
Path file = Paths.get(directoryPathAndName);
|
|
|
if (Files.exists(file)) {
|
|
if (Files.exists(file)) {
|
|
|
logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
|
|
logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
|
|
|
- }else {
|
|
|
|
|
|
|
+ } else {
|
|
|
//直接创建目录
|
|
//直接创建目录
|
|
|
Path extractPath = Paths.get(directoryPathAndName);
|
|
Path extractPath = Paths.get(directoryPathAndName);
|
|
|
Files.createDirectories(extractPath);
|
|
Files.createDirectories(extractPath);
|
|
|
}
|
|
}
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
- throw new ServiceException("创建目录失败,请检查!"+e.getMessage());
|
|
|
|
|
|
|
+ throw new ServiceException("创建目录失败,请检查!" + e.getMessage());
|
|
|
}
|
|
}
|
|
|
return directoryPathAndName;
|
|
return directoryPathAndName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//给定一个名称创建一个目录,返回路径
|
|
//给定一个名称创建一个目录,返回路径
|
|
|
- public static String createMultipartDirectory(String directoryName,String[] subDirectoryName) {
|
|
|
|
|
|
|
+ public static String createMultipartDirectory(String directoryName, String[] subDirectoryName) {
|
|
|
String directoryPath = createFilePath;
|
|
String directoryPath = createFilePath;
|
|
|
directoryName = extractFilename(directoryName);
|
|
directoryName = extractFilename(directoryName);
|
|
|
- String directoryPathAndName = createDirectory(directoryPath,directoryName);
|
|
|
|
|
|
|
+ String directoryPathAndName = createDirectory(directoryPath, directoryName);
|
|
|
Arrays.stream(subDirectoryName).forEach(name -> {
|
|
Arrays.stream(subDirectoryName).forEach(name -> {
|
|
|
- createDirectory(directoryPathAndName,name);
|
|
|
|
|
|
|
+ createDirectory(directoryPathAndName, name);
|
|
|
});
|
|
});
|
|
|
- return directoryPath+"/"+ directoryName;
|
|
|
|
|
|
|
+ return directoryPath + "/" + directoryName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
//给定一个名称创建一个目录,返回路径
|
|
//给定一个名称创建一个目录,返回路径
|
|
|
- public static String createDirectory(String directoryPath, String directoryName) {
|
|
|
|
|
|
|
+ public static String createDirectory(String directoryPath, String directoryName) {
|
|
|
String directoryPathAndName = null;
|
|
String directoryPathAndName = null;
|
|
|
try {
|
|
try {
|
|
|
- directoryPathAndName =directoryPath+"/"+ directoryName;
|
|
|
|
|
|
|
+ directoryPathAndName = directoryPath + "/" + directoryName;
|
|
|
Path file = Paths.get(directoryPathAndName);
|
|
Path file = Paths.get(directoryPathAndName);
|
|
|
if (Files.exists(file)) {
|
|
if (Files.exists(file)) {
|
|
|
logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
|
|
logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
|
|
|
- }else {
|
|
|
|
|
|
|
+ } else {
|
|
|
//直接创建目录
|
|
//直接创建目录
|
|
|
Path extractPath = Paths.get(directoryPathAndName);
|
|
Path extractPath = Paths.get(directoryPathAndName);
|
|
|
Files.createDirectories(extractPath);
|
|
Files.createDirectories(extractPath);
|
|
|
}
|
|
}
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
- throw new ServiceException("创建目录失败,请检查!"+e.getMessage());
|
|
|
|
|
|
|
+ throw new ServiceException("创建目录失败,请检查!" + e.getMessage());
|
|
|
}
|
|
}
|
|
|
return directoryPathAndName;
|
|
return directoryPathAndName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
//给定一个名称创建一个目录,返回路径
|
|
//给定一个名称创建一个目录,返回路径
|
|
|
- public static String moveFileToDirectory(String filePath,String directoryPath) {
|
|
|
|
|
|
|
+ public static String moveFileToDirectory(String filePath, String directoryPath) {
|
|
|
Path file = Paths.get(filePath);
|
|
Path file = Paths.get(filePath);
|
|
|
Path dirPath = Paths.get(directoryPath);
|
|
Path dirPath = Paths.get(directoryPath);
|
|
|
if (!Files.exists(file)) {
|
|
if (!Files.exists(file)) {
|
|
|
- logger.warn("处理文件失败,文件不存在: {}",filePath);
|
|
|
|
|
- throw new ServiceException("处理文件失败:文件不存在: "+filePath);
|
|
|
|
|
|
|
+ logger.warn("处理文件失败,文件不存在: {}", filePath);
|
|
|
|
|
+ throw new ServiceException("处理文件失败:文件不存在: " + filePath);
|
|
|
}
|
|
}
|
|
|
if (!Files.exists(dirPath)) {
|
|
if (!Files.exists(dirPath)) {
|
|
|
logger.warn("目录不存在: {}", directoryPath);
|
|
logger.warn("目录不存在: {}", directoryPath);
|
|
|
- throw new ServiceException("目录不存在: "+directoryPath);
|
|
|
|
|
|
|
+ throw new ServiceException("目录不存在: " + directoryPath);
|
|
|
}
|
|
}
|
|
|
//这里拼接文件名
|
|
//这里拼接文件名
|
|
|
- if(directoryPath.endsWith("/")){
|
|
|
|
|
- directoryPath+=file.getFileName();
|
|
|
|
|
- }else {
|
|
|
|
|
- directoryPath+="/"+file.getFileName();
|
|
|
|
|
|
|
+ if (directoryPath.endsWith("/")) {
|
|
|
|
|
+ directoryPath += file.getFileName();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ directoryPath += "/" + file.getFileName();
|
|
|
}
|
|
}
|
|
|
dirPath = Paths.get(directoryPath);
|
|
dirPath = Paths.get(directoryPath);
|
|
|
try {
|
|
try {
|
|
@@ -121,7 +118,7 @@ public class ServiceFileUtil {
|
|
|
Path dirPath = Paths.get(directoryPath);
|
|
Path dirPath = Paths.get(directoryPath);
|
|
|
if (!Files.exists(dirPath)) {
|
|
if (!Files.exists(dirPath)) {
|
|
|
logger.warn("目录不存在: {}", directoryPath);
|
|
logger.warn("目录不存在: {}", directoryPath);
|
|
|
- throw new ServiceException("目录不存在: "+directoryPath);
|
|
|
|
|
|
|
+ throw new ServiceException("目录不存在: " + directoryPath);
|
|
|
}
|
|
}
|
|
|
Files.walk(dirPath)
|
|
Files.walk(dirPath)
|
|
|
.sorted(Comparator.reverseOrder())
|
|
.sorted(Comparator.reverseOrder())
|
|
@@ -132,6 +129,7 @@ public class ServiceFileUtil {
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
* 获取目录下的所有文件列表
|
|
* 获取目录下的所有文件列表
|
|
|
|
|
+ *
|
|
|
* @param directoryPath
|
|
* @param directoryPath
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
|
*/
|
|
*/
|
|
@@ -142,8 +140,8 @@ public class ServiceFileUtil {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
List<Path> list = Files.walk(dirPath, Integer.MAX_VALUE)
|
|
List<Path> list = Files.walk(dirPath, Integer.MAX_VALUE)
|
|
|
- .filter(Files::isRegularFile).collect(Collectors.toList());
|
|
|
|
|
- return list;
|
|
|
|
|
|
|
+ .filter(Files::isRegularFile).collect(Collectors.toList());
|
|
|
|
|
+ return list;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -166,9 +164,4 @@ public class ServiceFileUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|