Compare commits
No commits in common. "main" and "dev" have entirely different histories.
|
|
@ -1,10 +1,5 @@
|
|||
package com.ruoyi.file.controller;
|
||||
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.common.core.utils.file.FileUtils;
|
||||
import com.ruoyi.file.service.ISysFileService;
|
||||
import com.ruoyi.system.api.domain.SysFile;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -12,14 +7,20 @@ import org.springframework.web.bind.annotation.DeleteMapping;
|
|||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.common.core.utils.file.FileUtils;
|
||||
import com.ruoyi.file.service.ISysFileService;
|
||||
import com.ruoyi.system.api.domain.SysFile;
|
||||
|
||||
/**
|
||||
* 文件请求处理
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@RestController
|
||||
public class SysFileController {
|
||||
public class SysFileController
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
|
||||
|
||||
@Autowired
|
||||
|
|
@ -29,15 +30,19 @@ public class SysFileController {
|
|||
* 文件上传请求
|
||||
*/
|
||||
@PostMapping("upload")
|
||||
public R<SysFile> upload(MultipartFile file) {
|
||||
try {
|
||||
public R<SysFile> upload(MultipartFile file)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 上传并返回访问地址
|
||||
String url = sysFileService.uploadFile(file);
|
||||
SysFile sysFile = new SysFile();
|
||||
sysFile.setName(FileUtils.getName(url));
|
||||
sysFile.setUrl(url);
|
||||
return R.ok(sysFile);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("上传文件失败", e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
|
|
@ -47,30 +52,21 @@ public class SysFileController {
|
|||
* 文件删除请求
|
||||
*/
|
||||
@DeleteMapping("delete")
|
||||
public R<Boolean> delete(String fileUrl) {
|
||||
try {
|
||||
if (!FileUtils.validateFilePath(fileUrl)) {
|
||||
public R<Boolean> delete(String fileUrl)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!FileUtils.validateFilePath(fileUrl))
|
||||
{
|
||||
throw new Exception(StringUtils.format("资源文件({})非法,不允许删除。 ", fileUrl));
|
||||
}
|
||||
sysFileService.deleteFile(fileUrl);
|
||||
return R.ok();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("删除文件失败", e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件流上传请求
|
||||
*/
|
||||
@PostMapping("uploadStream")
|
||||
public R<String> uploadFileByStream(String filename, String extension, String data) {
|
||||
try {
|
||||
|
||||
return R.ok(sysFileService.uploadFileByData(filename, extension, data));
|
||||
} catch (Exception e) {
|
||||
log.error("上传文件失败", e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,35 +4,25 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
/**
|
||||
* 文件上传接口
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public interface ISysFileService {
|
||||
public interface ISysFileService
|
||||
{
|
||||
/**
|
||||
* 文件上传接口
|
||||
*
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @return 访问地址
|
||||
* @throws Exception
|
||||
*/
|
||||
String uploadFile(MultipartFile file) throws Exception;
|
||||
public String uploadFile(MultipartFile file) throws Exception;
|
||||
|
||||
/**
|
||||
* 文件删除接口
|
||||
*
|
||||
*
|
||||
* @param fileUrl 文件访问URL
|
||||
* @throws Exception
|
||||
*/
|
||||
void deleteFile(String fileUrl) throws Exception;
|
||||
|
||||
|
||||
/**
|
||||
* 文件上传接口
|
||||
*
|
||||
* @param filename 上传的文件的名称
|
||||
* @param extension 上传的文件后缀
|
||||
* @param out 上传的流
|
||||
* @return 访问地址
|
||||
*/
|
||||
String uploadFileByData(String filename, String extension, String out) throws Exception;
|
||||
public void deleteFile(String fileUrl) throws Exception;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
package com.ruoyi.file.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
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.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
/**
|
||||
* 本地文件存储
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Primary
|
||||
@Service
|
||||
@ConditionalOnProperty(name = "file.storage", havingValue = "local")
|
||||
public class LocalSysFileServiceImpl implements ISysFileService {
|
||||
public class LocalSysFileServiceImpl implements ISysFileService
|
||||
{
|
||||
/**
|
||||
* 资源映射路径 前缀
|
||||
*/
|
||||
|
|
@ -30,7 +28,7 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|||
*/
|
||||
@Value("${file.domain}")
|
||||
public String domain;
|
||||
|
||||
|
||||
/**
|
||||
* 上传文件存储在本地的根路径
|
||||
*/
|
||||
|
|
@ -39,44 +37,29 @@ public class LocalSysFileServiceImpl implements ISysFileService {
|
|||
|
||||
/**
|
||||
* 本地文件上传接口
|
||||
*
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @return 访问地址
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public String uploadFile(MultipartFile file) throws Exception {
|
||||
public String uploadFile(MultipartFile file) throws Exception
|
||||
{
|
||||
String name = FileUploadUtils.upload(localFilePath, file);
|
||||
return domain + localFilePrefix + name;
|
||||
String url = domain + localFilePrefix + name;
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地文件删除接口
|
||||
*
|
||||
*
|
||||
* @param fileUrl 文件访问URL
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void deleteFile(String fileUrl) throws Exception {
|
||||
public void deleteFile(String fileUrl) throws Exception
|
||||
{
|
||||
String localFile = StringUtils.substringAfter(fileUrl, localFilePrefix);
|
||||
FileUtils.deleteFile(localFilePath + localFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传接口
|
||||
*
|
||||
* @param filename 上传的文件的名称
|
||||
* @param extension 上传的文件后缀
|
||||
* @param out 上传的流
|
||||
* @return 访问地址
|
||||
*/
|
||||
@Override
|
||||
public String uploadFileByData(String filename, String extension, String out) throws Exception {
|
||||
try (ByteArrayInputStream in = new ByteArrayInputStream(out.getBytes())) {
|
||||
String fileName = FileUploadUtils.extractFilename(filename, extension);
|
||||
return FileUploadUtils.uploadByStream(localFilePath, fileName, in);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("Failed to upload file", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
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.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.file.config.MinioConfig;
|
||||
|
|
@ -7,14 +11,6 @@ import com.ruoyi.file.utils.FileUploadUtils;
|
|||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Minio 文件存储
|
||||
|
|
@ -22,8 +18,8 @@ import java.io.InputStream;
|
|||
* @author ruoyi
|
||||
*/
|
||||
@Service
|
||||
@ConditionalOnProperty(name = "file.storage", havingValue = "minio", matchIfMissing = true)
|
||||
public class MinioSysFileServiceImpl implements ISysFileService {
|
||||
public class MinioSysFileServiceImpl implements ISysFileService
|
||||
{
|
||||
@Autowired
|
||||
private MinioConfig minioConfig;
|
||||
|
||||
|
|
@ -38,9 +34,11 @@ public class MinioSysFileServiceImpl implements ISysFileService {
|
|||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public String uploadFile(MultipartFile file) throws Exception {
|
||||
public String uploadFile(MultipartFile file) throws Exception
|
||||
{
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
try
|
||||
{
|
||||
String fileName = FileUploadUtils.extractFilename(file);
|
||||
inputStream = file.getInputStream();
|
||||
PutObjectArgs args = PutObjectArgs.builder()
|
||||
|
|
@ -51,53 +49,34 @@ public class MinioSysFileServiceImpl implements ISysFileService {
|
|||
.build();
|
||||
client.putObject(args);
|
||||
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException("Minio Failed to upload file", e);
|
||||
} finally {
|
||||
}
|
||||
finally
|
||||
{
|
||||
IoUtils.closeQuietly(inputStream);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Minio文件删除接口
|
||||
*
|
||||
*
|
||||
* @param fileUrl 文件访问URL
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void deleteFile(String fileUrl) throws Exception {
|
||||
try {
|
||||
public void deleteFile(String fileUrl) throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
String minioFile = StringUtils.substringAfter(fileUrl, minioConfig.getBucketName());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadFileByData(String filename, String extension, String out) throws Exception {
|
||||
try (ByteArrayInputStream in = new ByteArrayInputStream(out.getBytes())) {
|
||||
String fileName = FileUploadUtils.extractFilename(filename, extension);
|
||||
System.out.println("========== MinIO Upload Debug ==========");
|
||||
System.out.println("原始文件名: " + filename);
|
||||
System.out.println("传入的扩展名: [" + extension + "]");
|
||||
System.out.println("生成的文件名: " + fileName);
|
||||
|
||||
String contentType = FileUploadUtils.getContentType(extension); // 获取文件类型
|
||||
System.out.println("获取到的 ContentType: " + contentType);
|
||||
System.out.println("========================================");
|
||||
|
||||
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,5 +1,11 @@
|
|||
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.FileNameLengthLimitExceededException;
|
||||
import com.ruoyi.common.core.exception.file.FileSizeLimitExceededException;
|
||||
|
|
@ -9,23 +15,14 @@ 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.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
|
||||
*/
|
||||
public class FileUploadUtils {
|
||||
public class FileUploadUtils
|
||||
{
|
||||
/**
|
||||
* 默认大小 50M
|
||||
*/
|
||||
|
|
@ -40,16 +37,22 @@ public class FileUploadUtils {
|
|||
* 根据文件路径上传
|
||||
*
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @param file 上传的文件
|
||||
* @param file 上传的文件
|
||||
* @return 文件名称
|
||||
* @throws IOException
|
||||
*/
|
||||
public static final String upload(String baseDir, MultipartFile file) throws IOException {
|
||||
try {
|
||||
public static final String upload(String baseDir, MultipartFile file) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||
} catch (FileException fe) {
|
||||
}
|
||||
catch (FileException fe)
|
||||
{
|
||||
throw new IOException(fe.getDefaultMessage(), fe);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IOException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
|
@ -57,20 +60,22 @@ public class FileUploadUtils {
|
|||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @param file 上传的文件
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @param file 上传的文件
|
||||
* @param allowedExtension 上传文件类型
|
||||
* @return 返回上传成功的文件名
|
||||
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||
* @throws FileNameLengthLimitExceededException 文件名太长
|
||||
* @throws IOException 比如读写文件出错时
|
||||
* @throws InvalidExtensionException 文件校验异常
|
||||
* @throws IOException 比如读写文件出错时
|
||||
* @throws InvalidExtensionException 文件校验异常
|
||||
*/
|
||||
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
|
||||
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
||||
InvalidExtensionException {
|
||||
InvalidExtensionException
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -83,41 +88,31 @@ public class FileUploadUtils {
|
|||
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(),
|
||||
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), FileTypeUtils.getExtension(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码文件名
|
||||
*/
|
||||
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 {
|
||||
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()) {
|
||||
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 {
|
||||
private static final String getPathFileName(String fileName) throws IOException
|
||||
{
|
||||
String pathFileName = "/" + fileName;
|
||||
return pathFileName;
|
||||
}
|
||||
|
|
@ -127,31 +122,43 @@ public class FileUploadUtils {
|
|||
*
|
||||
* @param file 上传的文件
|
||||
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||
* @throws InvalidExtensionException 文件校验异常
|
||||
* @throws InvalidExtensionException 文件校验异常
|
||||
*/
|
||||
public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
|
||||
throws FileSizeLimitExceededException, InvalidExtensionException {
|
||||
throws FileSizeLimitExceededException, InvalidExtensionException
|
||||
{
|
||||
long size = file.getSize();
|
||||
if (size > DEFAULT_MAX_SIZE) {
|
||||
if (size > DEFAULT_MAX_SIZE)
|
||||
{
|
||||
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
|
||||
}
|
||||
|
||||
String fileName = file.getOriginalFilename();
|
||||
String extension = FileTypeUtils.getExtension(file);
|
||||
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
|
||||
if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) {
|
||||
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
|
||||
{
|
||||
if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
|
||||
{
|
||||
throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
|
||||
fileName);
|
||||
} else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) {
|
||||
}
|
||||
else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
|
||||
{
|
||||
throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
|
||||
fileName);
|
||||
} else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) {
|
||||
}
|
||||
else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
|
||||
{
|
||||
throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
|
||||
fileName);
|
||||
} else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) {
|
||||
}
|
||||
else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
|
||||
{
|
||||
throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
|
||||
fileName);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidExtensionException(allowedExtension, extension, fileName);
|
||||
}
|
||||
}
|
||||
|
|
@ -160,89 +167,19 @@ public class FileUploadUtils {
|
|||
/**
|
||||
* 判断MIME类型是否是允许的MIME类型
|
||||
*
|
||||
* @param extension 上传文件类型
|
||||
* @param extension 上传文件类型
|
||||
* @param allowedExtension 允许上传文件类型
|
||||
* @return true/false
|
||||
*/
|
||||
public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
|
||||
for (String str : allowedExtension) {
|
||||
if (str.equalsIgnoreCase(extension)) {
|
||||
public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
|
||||
{
|
||||
for (String str : allowedExtension)
|
||||
{
|
||||
if (str.equalsIgnoreCase(extension))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static String getContentType(String FilenameExtension) {
|
||||
System.out.println("---------- getContentType Debug ----------");
|
||||
System.out.println("传入的扩展名参数: [" + FilenameExtension + "]");
|
||||
System.out.println("扩展名长度: " + (FilenameExtension != null ? FilenameExtension.length() : "null"));
|
||||
|
||||
// 统一处理:如果没有点,自动添加点
|
||||
String extension = FilenameExtension;
|
||||
if (extension != null && !extension.startsWith(".")) {
|
||||
extension = "." + extension;
|
||||
System.out.println("扩展名没有点,自动添加后: [" + extension + "]");
|
||||
}
|
||||
|
||||
if (extension.equalsIgnoreCase(".bmp")) {
|
||||
return "image/bmp";
|
||||
}
|
||||
if (extension.equalsIgnoreCase(".gif")) {
|
||||
return "image/gif";
|
||||
}
|
||||
if (extension.equalsIgnoreCase(".jpeg") ||
|
||||
extension.equalsIgnoreCase(".jpg") ||
|
||||
extension.equalsIgnoreCase(".png")) {
|
||||
return "image/jpeg";
|
||||
}
|
||||
if (extension.equalsIgnoreCase(".html")) {
|
||||
return "text/html";
|
||||
}
|
||||
if (extension.equalsIgnoreCase(".txt")) {
|
||||
return "text/plain";
|
||||
}
|
||||
if (extension.equalsIgnoreCase(".vsd")) {
|
||||
return "application/vnd.visio";
|
||||
}
|
||||
if (extension.equalsIgnoreCase(".pptx") ||
|
||||
extension.equalsIgnoreCase(".ppt")) {
|
||||
return "application/vnd.ms-powerpoint";
|
||||
}
|
||||
if (extension.equalsIgnoreCase(".docx") ||
|
||||
extension.equalsIgnoreCase(".doc")) {
|
||||
return "application/msword";
|
||||
}
|
||||
if (extension.equalsIgnoreCase(".xml")) {
|
||||
return "text/xml";
|
||||
}
|
||||
//PDF
|
||||
if (extension.equalsIgnoreCase(".pdf")) {
|
||||
return "application/pdf";
|
||||
}
|
||||
//excel
|
||||
if (".xls".equalsIgnoreCase(extension)) {
|
||||
return "application/vnd.ms-excel";
|
||||
}
|
||||
if (".xlsx".equalsIgnoreCase(extension)) {
|
||||
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
}
|
||||
//waypoints 拓恒+大疆的航线文件类型
|
||||
System.out.println("检查 waypoints 匹配:");
|
||||
System.out.println(" 与 '.waypoints' 比较: " + extension.equalsIgnoreCase(".waypoints"));
|
||||
System.out.println(" 与 '.kmz' 比较: " + extension.equalsIgnoreCase(".kmz"));
|
||||
|
||||
if (extension.equalsIgnoreCase(".waypoints") ||
|
||||
extension.equalsIgnoreCase(".kmz")) {
|
||||
System.out.println("匹配成功! 返回 application/octet-stream");
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
System.out.println("未匹配任何类型,返回默认值 application/octet-stream");
|
||||
System.out.println("------------------------------------------");
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,204 +0,0 @@
|
|||
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