优化文件上传逻辑,支持流式
This commit is contained in:
parent
2a0b7ecbfb
commit
f9c6e66731
|
|
@ -2,13 +2,14 @@ package com.ruoyi.file.service;
|
||||||
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传接口
|
* 文件上传接口
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public interface ISysFileService
|
public interface ISysFileService {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* 文件上传接口
|
* 文件上传接口
|
||||||
*
|
*
|
||||||
|
|
@ -16,7 +17,7 @@ public interface ISysFileService
|
||||||
* @return 访问地址
|
* @return 访问地址
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public String uploadFile(MultipartFile file) throws Exception;
|
String uploadFile(MultipartFile file) throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件删除接口
|
* 文件删除接口
|
||||||
|
|
@ -24,5 +25,16 @@ public interface ISysFileService
|
||||||
* @param fileUrl 文件访问URL
|
* @param fileUrl 文件访问URL
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public void deleteFile(String fileUrl) throws Exception;
|
void deleteFile(String fileUrl) throws Exception;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传接口
|
||||||
|
*
|
||||||
|
* @param filename 上传的文件的名称
|
||||||
|
* @param extension 上传的文件后缀
|
||||||
|
* @param out 上传的流
|
||||||
|
* @return 访问地址
|
||||||
|
*/
|
||||||
|
String uploadFileByStream(String filename, String extension, ByteArrayOutputStream out) throws Exception;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
package com.ruoyi.file.service;
|
package com.ruoyi.file.service;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
|
import com.ruoyi.common.core.utils.file.FileUtils;
|
||||||
|
import com.ruoyi.file.utils.FileUploadUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import com.ruoyi.common.core.utils.StringUtils;
|
|
||||||
import com.ruoyi.common.core.utils.file.FileUtils;
|
import java.io.ByteArrayInputStream;
|
||||||
import com.ruoyi.file.utils.FileUploadUtils;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本地文件存储
|
* 本地文件存储
|
||||||
|
|
@ -15,8 +18,7 @@ import com.ruoyi.file.utils.FileUploadUtils;
|
||||||
*/
|
*/
|
||||||
@Primary
|
@Primary
|
||||||
@Service
|
@Service
|
||||||
public class LocalSysFileServiceImpl implements ISysFileService
|
public class LocalSysFileServiceImpl implements ISysFileService {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* 资源映射路径 前缀
|
* 资源映射路径 前缀
|
||||||
*/
|
*/
|
||||||
|
|
@ -43,11 +45,9 @@ public class LocalSysFileServiceImpl implements ISysFileService
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String uploadFile(MultipartFile file) throws Exception
|
public String uploadFile(MultipartFile file) throws Exception {
|
||||||
{
|
|
||||||
String name = FileUploadUtils.upload(localFilePath, file);
|
String name = FileUploadUtils.upload(localFilePath, file);
|
||||||
String url = domain + localFilePrefix + name;
|
return domain + localFilePrefix + name;
|
||||||
return url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -57,9 +57,26 @@ public class LocalSysFileServiceImpl implements ISysFileService
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void deleteFile(String fileUrl) throws Exception
|
public void deleteFile(String fileUrl) throws Exception {
|
||||||
{
|
|
||||||
String localFile = StringUtils.substringAfter(fileUrl, localFilePrefix);
|
String localFile = StringUtils.substringAfter(fileUrl, localFilePrefix);
|
||||||
FileUtils.deleteFile(localFilePath + localFile);
|
FileUtils.deleteFile(localFilePath + localFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传接口
|
||||||
|
*
|
||||||
|
* @param filename 上传的文件的名称
|
||||||
|
* @param extension 上传的文件后缀
|
||||||
|
* @param out 上传的流
|
||||||
|
* @return 访问地址
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String uploadFileByStream(String filename, String extension, ByteArrayOutputStream out) throws Exception {
|
||||||
|
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
|
||||||
|
String fileName = FileUploadUtils.extractFilename(filename, extension);
|
||||||
|
return FileUploadUtils.uploadByStream(localFilePath, fileName, in);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new Exception("Failed to upload file", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,5 @@
|
||||||
package com.ruoyi.file.service;
|
package com.ruoyi.file.service;
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
import com.alibaba.nacos.common.utils.IoUtils;
|
import com.alibaba.nacos.common.utils.IoUtils;
|
||||||
import com.ruoyi.common.core.utils.StringUtils;
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
import com.ruoyi.file.config.MinioConfig;
|
import com.ruoyi.file.config.MinioConfig;
|
||||||
|
|
@ -11,6 +7,13 @@ import com.ruoyi.file.utils.FileUploadUtils;
|
||||||
import io.minio.MinioClient;
|
import io.minio.MinioClient;
|
||||||
import io.minio.PutObjectArgs;
|
import io.minio.PutObjectArgs;
|
||||||
import io.minio.RemoveObjectArgs;
|
import io.minio.RemoveObjectArgs;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minio 文件存储
|
* Minio 文件存储
|
||||||
|
|
@ -18,8 +21,7 @@ import io.minio.RemoveObjectArgs;
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class MinioSysFileServiceImpl implements ISysFileService
|
public class MinioSysFileServiceImpl implements ISysFileService {
|
||||||
{
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MinioConfig minioConfig;
|
private MinioConfig minioConfig;
|
||||||
|
|
||||||
|
|
@ -34,11 +36,9 @@ public class MinioSysFileServiceImpl implements ISysFileService
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String uploadFile(MultipartFile file) throws Exception
|
public String uploadFile(MultipartFile file) throws Exception {
|
||||||
{
|
|
||||||
InputStream inputStream = null;
|
InputStream inputStream = null;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
String fileName = FileUploadUtils.extractFilename(file);
|
String fileName = FileUploadUtils.extractFilename(file);
|
||||||
inputStream = file.getInputStream();
|
inputStream = file.getInputStream();
|
||||||
PutObjectArgs args = PutObjectArgs.builder()
|
PutObjectArgs args = PutObjectArgs.builder()
|
||||||
|
|
@ -49,13 +49,9 @@ public class MinioSysFileServiceImpl implements ISysFileService
|
||||||
.build();
|
.build();
|
||||||
client.putObject(args);
|
client.putObject(args);
|
||||||
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
|
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
throw new RuntimeException("Minio Failed to upload file", e);
|
throw new RuntimeException("Minio Failed to upload file", e);
|
||||||
}
|
} finally {
|
||||||
finally
|
|
||||||
{
|
|
||||||
IoUtils.closeQuietly(inputStream);
|
IoUtils.closeQuietly(inputStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -67,16 +63,31 @@ public class MinioSysFileServiceImpl implements ISysFileService
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void deleteFile(String fileUrl) throws Exception
|
public void deleteFile(String fileUrl) throws Exception {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
String minioFile = StringUtils.substringAfter(fileUrl, minioConfig.getBucketName());
|
String minioFile = StringUtils.substringAfter(fileUrl, minioConfig.getBucketName());
|
||||||
client.removeObject(RemoveObjectArgs.builder().bucket(minioConfig.getBucketName()).object(minioFile).build());
|
client.removeObject(RemoveObjectArgs.builder().bucket(minioConfig.getBucketName()).object(minioFile).build());
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
throw new RuntimeException("Minio Failed to delete file", e);
|
throw new RuntimeException("Minio Failed to delete file", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String uploadFileByStream(String filename, String extension, ByteArrayOutputStream out) throws Exception {
|
||||||
|
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
|
||||||
|
String fileName = FileUploadUtils.extractFilename(filename, extension);
|
||||||
|
String contentType = FileUploadUtils.getContentType(extension); // 获取文件类型
|
||||||
|
PutObjectArgs args = PutObjectArgs.builder()
|
||||||
|
.bucket(minioConfig.getBucketName())
|
||||||
|
.object(fileName)
|
||||||
|
.stream(in, in.available(), -1)
|
||||||
|
.contentType(contentType)
|
||||||
|
.build();
|
||||||
|
client.putObject(args);
|
||||||
|
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new Exception("Minio Failed to upload file", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,5 @@
|
||||||
package com.ruoyi.file.utils;
|
package com.ruoyi.file.utils;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.Objects;
|
|
||||||
import org.apache.commons.io.FilenameUtils;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
import com.ruoyi.common.core.exception.file.FileException;
|
import com.ruoyi.common.core.exception.file.FileException;
|
||||||
import com.ruoyi.common.core.exception.file.FileNameLengthLimitExceededException;
|
import com.ruoyi.common.core.exception.file.FileNameLengthLimitExceededException;
|
||||||
import com.ruoyi.common.core.exception.file.FileSizeLimitExceededException;
|
import com.ruoyi.common.core.exception.file.FileSizeLimitExceededException;
|
||||||
|
|
@ -15,14 +9,23 @@ import com.ruoyi.common.core.utils.StringUtils;
|
||||||
import com.ruoyi.common.core.utils.file.FileTypeUtils;
|
import com.ruoyi.common.core.utils.file.FileTypeUtils;
|
||||||
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
|
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
|
||||||
import com.ruoyi.common.core.utils.uuid.Seq;
|
import com.ruoyi.common.core.utils.uuid.Seq;
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
|
import org.springframework.util.FileCopyUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传工具类
|
* 文件上传工具类
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
public class FileUploadUtils
|
public class FileUploadUtils {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* 默认大小 50M
|
* 默认大小 50M
|
||||||
*/
|
*/
|
||||||
|
|
@ -37,22 +40,16 @@ public class FileUploadUtils
|
||||||
* 根据文件路径上传
|
* 根据文件路径上传
|
||||||
*
|
*
|
||||||
* @param baseDir 相对应用的基目录
|
* @param baseDir 相对应用的基目录
|
||||||
* @param file 上传的文件
|
* @param file 上传的文件
|
||||||
* @return 文件名称
|
* @return 文件名称
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static final String upload(String baseDir, MultipartFile file) throws IOException
|
public static final String upload(String baseDir, MultipartFile file) throws IOException {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||||
}
|
} catch (FileException fe) {
|
||||||
catch (FileException fe)
|
|
||||||
{
|
|
||||||
throw new IOException(fe.getDefaultMessage(), fe);
|
throw new IOException(fe.getDefaultMessage(), fe);
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
throw new IOException(e.getMessage(), e);
|
throw new IOException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -60,22 +57,20 @@ public class FileUploadUtils
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
*
|
*
|
||||||
* @param baseDir 相对应用的基目录
|
* @param baseDir 相对应用的基目录
|
||||||
* @param file 上传的文件
|
* @param file 上传的文件
|
||||||
* @param allowedExtension 上传文件类型
|
* @param allowedExtension 上传文件类型
|
||||||
* @return 返回上传成功的文件名
|
* @return 返回上传成功的文件名
|
||||||
* @throws FileSizeLimitExceededException 如果超出最大大小
|
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||||
* @throws FileNameLengthLimitExceededException 文件名太长
|
* @throws FileNameLengthLimitExceededException 文件名太长
|
||||||
* @throws IOException 比如读写文件出错时
|
* @throws IOException 比如读写文件出错时
|
||||||
* @throws InvalidExtensionException 文件校验异常
|
* @throws InvalidExtensionException 文件校验异常
|
||||||
*/
|
*/
|
||||||
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
|
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
|
||||||
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
||||||
InvalidExtensionException
|
InvalidExtensionException {
|
||||||
{
|
|
||||||
int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
||||||
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
|
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
|
||||||
{
|
|
||||||
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
|
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,31 +83,41 @@ public class FileUploadUtils
|
||||||
return getPathFileName(fileName);
|
return getPathFileName(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String uploadByStream(String localFilePath, String fileName, ByteArrayInputStream in)
|
||||||
|
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException {
|
||||||
|
String absPath = FileUploadUtils.getAbsoluteFile(localFilePath, fileName).getAbsolutePath();
|
||||||
|
FileCopyUtils.copy(in, Files.newOutputStream(Paths.get(absPath)));
|
||||||
|
return FileUploadUtils.getPathFileName(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 编码文件名
|
* 编码文件名
|
||||||
*/
|
*/
|
||||||
public static final String extractFilename(MultipartFile file)
|
public static final String extractFilename(MultipartFile file) {
|
||||||
{
|
|
||||||
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
||||||
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), FileTypeUtils.getExtension(file));
|
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), FileTypeUtils.getExtension(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
|
/**
|
||||||
{
|
* 编码文件名
|
||||||
|
*/
|
||||||
|
public static final String extractFilename(String fileName, String extension) {
|
||||||
|
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
||||||
|
FilenameUtils.getBaseName(fileName), Seq.getId(Seq.uploadSeqType), extension);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
|
||||||
File desc = new File(uploadDir + File.separator + fileName);
|
File desc = new File(uploadDir + File.separator + fileName);
|
||||||
|
|
||||||
if (!desc.exists())
|
if (!desc.exists()) {
|
||||||
{
|
if (!desc.getParentFile().exists()) {
|
||||||
if (!desc.getParentFile().exists())
|
|
||||||
{
|
|
||||||
desc.getParentFile().mkdirs();
|
desc.getParentFile().mkdirs();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return desc.isAbsolute() ? desc : desc.getAbsoluteFile();
|
return desc.isAbsolute() ? desc : desc.getAbsoluteFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String getPathFileName(String fileName) throws IOException
|
private static final String getPathFileName(String fileName) throws IOException {
|
||||||
{
|
|
||||||
String pathFileName = "/" + fileName;
|
String pathFileName = "/" + fileName;
|
||||||
return pathFileName;
|
return pathFileName;
|
||||||
}
|
}
|
||||||
|
|
@ -122,43 +127,31 @@ public class FileUploadUtils
|
||||||
*
|
*
|
||||||
* @param file 上传的文件
|
* @param file 上传的文件
|
||||||
* @throws FileSizeLimitExceededException 如果超出最大大小
|
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||||
* @throws InvalidExtensionException 文件校验异常
|
* @throws InvalidExtensionException 文件校验异常
|
||||||
*/
|
*/
|
||||||
public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
|
public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
|
||||||
throws FileSizeLimitExceededException, InvalidExtensionException
|
throws FileSizeLimitExceededException, InvalidExtensionException {
|
||||||
{
|
|
||||||
long size = file.getSize();
|
long size = file.getSize();
|
||||||
if (size > DEFAULT_MAX_SIZE)
|
if (size > DEFAULT_MAX_SIZE) {
|
||||||
{
|
|
||||||
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
|
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
|
||||||
}
|
}
|
||||||
|
|
||||||
String fileName = file.getOriginalFilename();
|
String fileName = file.getOriginalFilename();
|
||||||
String extension = FileTypeUtils.getExtension(file);
|
String extension = FileTypeUtils.getExtension(file);
|
||||||
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
|
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
|
||||||
{
|
if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) {
|
||||||
if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
|
|
||||||
{
|
|
||||||
throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
|
throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
|
||||||
fileName);
|
fileName);
|
||||||
}
|
} else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) {
|
||||||
else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
|
|
||||||
{
|
|
||||||
throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
|
throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
|
||||||
fileName);
|
fileName);
|
||||||
}
|
} else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) {
|
||||||
else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
|
|
||||||
{
|
|
||||||
throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
|
throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
|
||||||
fileName);
|
fileName);
|
||||||
}
|
} else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) {
|
||||||
else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
|
|
||||||
{
|
|
||||||
throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
|
throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
|
||||||
fileName);
|
fileName);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new InvalidExtensionException(allowedExtension, extension, fileName);
|
throw new InvalidExtensionException(allowedExtension, extension, fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -167,19 +160,71 @@ public class FileUploadUtils
|
||||||
/**
|
/**
|
||||||
* 判断MIME类型是否是允许的MIME类型
|
* 判断MIME类型是否是允许的MIME类型
|
||||||
*
|
*
|
||||||
* @param extension 上传文件类型
|
* @param extension 上传文件类型
|
||||||
* @param allowedExtension 允许上传文件类型
|
* @param allowedExtension 允许上传文件类型
|
||||||
* @return true/false
|
* @return true/false
|
||||||
*/
|
*/
|
||||||
public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
|
public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
|
||||||
{
|
for (String str : allowedExtension) {
|
||||||
for (String str : allowedExtension)
|
if (str.equalsIgnoreCase(extension)) {
|
||||||
{
|
|
||||||
if (str.equalsIgnoreCase(extension))
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String getContentType(String FilenameExtension) {
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".bmp")) {
|
||||||
|
return "image/bmp";
|
||||||
|
}
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".gif")) {
|
||||||
|
return "image/gif";
|
||||||
|
}
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
|
||||||
|
FilenameExtension.equalsIgnoreCase(".jpg") ||
|
||||||
|
FilenameExtension.equalsIgnoreCase(".png")) {
|
||||||
|
return "image/jpeg";
|
||||||
|
}
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".html")) {
|
||||||
|
return "text/html";
|
||||||
|
}
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".txt")) {
|
||||||
|
return "text/plain";
|
||||||
|
}
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".vsd")) {
|
||||||
|
return "application/vnd.visio";
|
||||||
|
}
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".pptx") ||
|
||||||
|
FilenameExtension.equalsIgnoreCase(".ppt")) {
|
||||||
|
return "application/vnd.ms-powerpoint";
|
||||||
|
}
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".docx") ||
|
||||||
|
FilenameExtension.equalsIgnoreCase(".doc")) {
|
||||||
|
return "application/msword";
|
||||||
|
}
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".xml")) {
|
||||||
|
return "text/xml";
|
||||||
|
}
|
||||||
|
//PDF
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".pdf")) {
|
||||||
|
return "application/pdf";
|
||||||
|
}
|
||||||
|
//excel
|
||||||
|
if (".xls".equalsIgnoreCase(FilenameExtension)) {
|
||||||
|
return "application/vnd.ms-excel";
|
||||||
|
}
|
||||||
|
if (".xlsx".equalsIgnoreCase(FilenameExtension)) {
|
||||||
|
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||||
|
}
|
||||||
|
//waypoints 拓恒+大疆的航线文件类型
|
||||||
|
if (FilenameExtension.equalsIgnoreCase(".waypoints") ||
|
||||||
|
FilenameExtension.equalsIgnoreCase(".kmz")) {
|
||||||
|
return "application/octet-stream";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "image/jpeg";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,204 @@
|
||||||
|
package com.ruoyi.file.utils;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.exception.file.FileException;
|
||||||
|
import com.ruoyi.common.core.exception.file.FileNameLengthLimitExceededException;
|
||||||
|
import com.ruoyi.common.core.exception.file.FileSizeLimitExceededException;
|
||||||
|
import com.ruoyi.common.core.exception.file.InvalidExtensionException;
|
||||||
|
import com.ruoyi.common.core.utils.DateUtils;
|
||||||
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
|
import com.ruoyi.common.core.utils.file.FileTypeUtils;
|
||||||
|
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
|
||||||
|
import com.ruoyi.common.core.utils.uuid.Seq;
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
|
import org.springframework.util.FileCopyUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传工具类
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public class FileUploadUtils_bak {
|
||||||
|
/**
|
||||||
|
* 默认大小 50M
|
||||||
|
*/
|
||||||
|
public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认的文件名最大长度 100
|
||||||
|
*/
|
||||||
|
public static final int DEFAULT_FILE_NAME_LENGTH = 100;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件路径上传
|
||||||
|
*
|
||||||
|
* @param baseDir 相对应用的基目录
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @return 文件名称
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static final String upload(String fileName, String baseDir, MultipartFile file) throws IOException {
|
||||||
|
try {
|
||||||
|
return upload(baseDir, fileName, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||||
|
} catch (FileException fe) {
|
||||||
|
throw new IOException(fe.getDefaultMessage(), fe);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传
|
||||||
|
*
|
||||||
|
* @param baseDir 相对应用的基目录
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @param allowedExtension 上传文件类型
|
||||||
|
* @return 返回上传成功的文件名
|
||||||
|
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||||
|
* @throws FileNameLengthLimitExceededException 文件名太长
|
||||||
|
* @throws IOException 比如读写文件出错时
|
||||||
|
* @throws InvalidExtensionException 文件校验异常
|
||||||
|
*/
|
||||||
|
public static final String upload(String baseDir, String fileName, MultipartFile file, String[] allowedExtension)
|
||||||
|
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
||||||
|
InvalidExtensionException {
|
||||||
|
int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
||||||
|
assertFileNameLength(fileNameLength);
|
||||||
|
assertAllowed(file);
|
||||||
|
assertExtension(file, allowedExtension);
|
||||||
|
|
||||||
|
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
|
||||||
|
file.transferTo(Paths.get(absPath));
|
||||||
|
return getPathFileName(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传
|
||||||
|
*
|
||||||
|
* @param baseDir 相对应用的基目录
|
||||||
|
* @param inputStream 上传文件的流
|
||||||
|
* @param allowedExtension 上传文件类型
|
||||||
|
* @return 返回上传成功的文件名
|
||||||
|
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||||
|
* @throws FileNameLengthLimitExceededException 文件名太长
|
||||||
|
* @throws IOException 比如读写文件出错时
|
||||||
|
* @throws InvalidExtensionException 文件校验异常
|
||||||
|
*/
|
||||||
|
public static final String upload(String baseDir, String fileName, String extension, InputStream inputStream, String[] allowedExtension)
|
||||||
|
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
||||||
|
InvalidExtensionException {
|
||||||
|
assertFileNameLength(fileName.length() + extension.length());
|
||||||
|
assertAllowed(inputStream.available());
|
||||||
|
assertExtension(fileName, extension, allowedExtension);
|
||||||
|
|
||||||
|
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
|
||||||
|
FileCopyUtils.copy(inputStream, Files.newOutputStream(Paths.get(absPath)));
|
||||||
|
|
||||||
|
return getPathFileName(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertFileNameLength(int length)
|
||||||
|
throws FileNameLengthLimitExceededException {
|
||||||
|
if (length > FileUploadUtils_bak.DEFAULT_FILE_NAME_LENGTH) {
|
||||||
|
throw new FileNameLengthLimitExceededException(FileUploadUtils_bak.DEFAULT_FILE_NAME_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码文件名
|
||||||
|
*/
|
||||||
|
public static final String extractFilename(MultipartFile file) {
|
||||||
|
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
||||||
|
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), FileTypeUtils.getExtension(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
|
||||||
|
File desc = new File(uploadDir + File.separator + fileName);
|
||||||
|
|
||||||
|
if (!desc.exists()) {
|
||||||
|
if (!desc.getParentFile().exists()) {
|
||||||
|
desc.getParentFile().mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return desc.isAbsolute() ? desc : desc.getAbsoluteFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String getPathFileName(String fileName) throws IOException {
|
||||||
|
String pathFileName = "/" + fileName;
|
||||||
|
return pathFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件大小校验
|
||||||
|
*
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||||
|
* @throws InvalidExtensionException 文件校验异常
|
||||||
|
*/
|
||||||
|
public static final void assertAllowed(MultipartFile file)
|
||||||
|
throws FileSizeLimitExceededException, InvalidExtensionException {
|
||||||
|
long size = file.getSize();
|
||||||
|
assertAllowed(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final void assertAllowed(long size)
|
||||||
|
throws FileSizeLimitExceededException, InvalidExtensionException {
|
||||||
|
if (size > DEFAULT_MAX_SIZE) {
|
||||||
|
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final void assertExtension(MultipartFile file, String[] allowedExtension)
|
||||||
|
throws FileSizeLimitExceededException, InvalidExtensionException {
|
||||||
|
|
||||||
|
String fileName = file.getOriginalFilename();
|
||||||
|
String extension = FileTypeUtils.getExtension(file);
|
||||||
|
assertExtension(fileName, extension, allowedExtension);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final void assertExtension(String fileName, String extension, String[] allowedExtension)
|
||||||
|
throws FileSizeLimitExceededException, InvalidExtensionException {
|
||||||
|
if (allowedExtension != null && !
|
||||||
|
isAllowedExtension(extension, allowedExtension)) {
|
||||||
|
if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) {
|
||||||
|
throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
|
||||||
|
fileName);
|
||||||
|
} else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) {
|
||||||
|
throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
|
||||||
|
fileName);
|
||||||
|
} else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) {
|
||||||
|
throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
|
||||||
|
fileName);
|
||||||
|
} else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) {
|
||||||
|
throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
|
||||||
|
fileName);
|
||||||
|
} else {
|
||||||
|
throw new InvalidExtensionException(allowedExtension, extension, fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断MIME类型是否是允许的MIME类型
|
||||||
|
*
|
||||||
|
* @param extension 上传文件类型
|
||||||
|
* @param allowedExtension 允许上传文件类型
|
||||||
|
* @return true/false
|
||||||
|
*/
|
||||||
|
public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
|
||||||
|
for (String str : allowedExtension) {
|
||||||
|
if (str.equalsIgnoreCase(extension)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue