xiangpei
2025-02-14 c5dafd7dba14635643b4340597fb14c563c8dc7e
common/src/main/java/com/ycl/common/utils/file/FileUploadUtils.java
@@ -2,7 +2,9 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
@@ -85,7 +87,44 @@
            throw new IOException(e.getMessage(), e);
        }
    }
    /**
     * 文件上传
     *
     * @param baseDir 相对应用的基目录
     * @param file 上传的文件
     * @return 返回上传成功的文件名
     * @throws FileSizeLimitExceededException 如果超出最大大小
     * @throws FileNameLengthLimitExceededException 文件名太长
     * @throws IOException 比如读写文件出错时
     * @throws InvalidExtensionException 文件校验异常
     */
    public static final String uploadIOFile(String baseDir, java.io.File file)
            throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException
    {
        int fileNamelength = Objects.requireNonNull(file.getName()).length();
        if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
        {
            throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
        }
        //上传IOFile文件逻辑
        String fileName = file.getName();
        //带日期前缀的文件名
        String formatName = StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
                FilenameUtils.getBaseName(fileName), Seq.getId(Seq.uploadSeqType), fileName.substring(fileName.lastIndexOf(".") + 1));
        // /home/projectManagement/uploadPath/upload/2024/11/11/xxxx
        String url = baseDir + File.separator + formatName;
        File newFile = new File(url);
        if (!newFile.exists())
        {
            if (!newFile.getParentFile().exists())
            {
                newFile.getParentFile().mkdirs();
            }
        }
        //复制文件到上传路径
        Files.copy(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        return getPathFileName(baseDir, formatName);
    }
    /**
     * 文件上传
     *