| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package com.siwei.apply.utils;
- import com.siwei.common.core.exception.ServiceException;
- import com.siwei.common.core.utils.DateUtils;
- import com.siwei.common.core.utils.StringUtils;
- import com.siwei.common.core.utils.uuid.Seq;
- import org.apache.commons.io.FilenameUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.StandardCopyOption;
- import java.util.Arrays;
- /**
- * 文件工具类
- */
- public class ServiceFileUtil {
-
- private static final Logger logger = LoggerFactory.getLogger(ServiceFileUtil.class);
- private static final String createFilePath = "/home/siwei/uploadPath";
- /**
- * 编码目录名
- */
- public static final String extractFilename(String name)
- {
- return StringUtils.format("{}/{}_{}", DateUtils.datePath(),
- FilenameUtils.getBaseName(name), Seq.getId(Seq.uploadSeqType));
- }
- //给定一个名称创建一个目录,返回路径
- public static String createDirectory(String directoryName) {
- String directoryPathAndName = null;
- try {
- directoryPathAndName =createFilePath+"/"+ extractFilename(directoryName);
- Path file = Paths.get(directoryPathAndName);
- if (Files.exists(file)) {
- logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
- }else {
- //直接创建目录
- Path extractPath = Paths.get(directoryPathAndName);
- Files.createDirectories(extractPath);
- }
- } catch (IOException e) {
- throw new ServiceException("创建目录失败,请检查!"+e.getMessage());
- }
- return directoryPathAndName;
- }
- //给定一个名称创建一个目录,返回路径
- public static String createMultipartDirectory(String directoryName,String[] subDirectoryName) {
- String directoryPath = createFilePath;
- directoryName = extractFilename(directoryName);
- String directoryPathAndName = createDirectory(directoryPath,directoryName);
- Arrays.stream(subDirectoryName).forEach(name -> {
- createDirectory(directoryPathAndName,name);
- });
- return directoryPath+"/"+ directoryName;
- }
- //给定一个名称创建一个目录,返回路径
- public static String createDirectory(String directoryPath, String directoryName) {
- String directoryPathAndName = null;
- try {
- directoryPathAndName =directoryPath+"/"+ directoryName;
- Path file = Paths.get(directoryPathAndName);
- if (Files.exists(file)) {
- logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
- }else {
- //直接创建目录
- Path extractPath = Paths.get(directoryPathAndName);
- Files.createDirectories(extractPath);
- }
- } catch (IOException e) {
- throw new ServiceException("创建目录失败,请检查!"+e.getMessage());
- }
- return directoryPathAndName;
- }
- //给定一个名称创建一个目录,返回路径
- public static String moveFileToDirectory(String filePath,String directoryPath) {
- Path file = Paths.get(filePath);
- Path dirPath = Paths.get(directoryPath);
- if (!Files.exists(file)) {
- logger.warn("处理文件失败,文件不存在: {}",filePath);
- throw new ServiceException("处理文件失败:文件不存在: "+filePath);
- }
- if (!Files.exists(dirPath)) {
- logger.warn("目录不存在: {}", directoryPath);
- throw new ServiceException("目录不存在: "+directoryPath);
- }
- //这里拼接文件名
- if(directoryPath.endsWith("/")){
- directoryPath+=file.getFileName();
- }else {
- directoryPath+="/"+file.getFileName();
- }
- dirPath = Paths.get(directoryPath);
- try {
- //Files.copy(file, dirPath, StandardCopyOption.REPLACE_EXISTING);
- Files.move(file, dirPath, StandardCopyOption.REPLACE_EXISTING);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return directoryPath;
- }
- }
|