支持上传任意格式文件

master
RuoYi 5 years ago committed by Limy
parent b6cdee5bcc
commit ca116c0166

@ -2,10 +2,12 @@ package com.ruoyi.common.utils.file;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.Md5Utils;
@ -31,10 +33,29 @@ public class FileUploadUtils
*/
private static String defaultBaseDir = Global.getProfile();
/**
* jpg
*/
public static final String IMAGE_JPG_EXTENSION = ".jpg";
public static final String[] IMAGE_EXTENSION = {
"bmp", "gif", "jpg", "jpeg", "png"
};
public static final String[] FLASH_EXTENSION = {
"swf", "flv"
};
public static final String[] MEDIA_EXTENSION = {
"swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg", "asf", "rm", "rmvb"
};
public static final String[] DEFAULT_ALLOWED_EXTENSION = {
//图片
"bmp", "gif", "jpg", "jpeg", "png",
//word excel powerpoint
"doc", "docx", "xls", "xlsx", "ppt", "pptx",
"html", "htm", "txt",
//压缩文件
"rar", "zip", "gz", "bz2",
//pdf
"pdf"
};
private static int counter = 0;
@ -59,7 +80,7 @@ public class FileUploadUtils
{
try
{
return upload(getDefaultBaseDir(), file, FileUploadUtils.IMAGE_JPG_EXTENSION);
return upload(getDefaultBaseDir(), file, DEFAULT_ALLOWED_EXTENSION);
}
catch (Exception e)
{
@ -79,7 +100,7 @@ public class FileUploadUtils
{
try
{
return upload(baseDir, file, FileUploadUtils.IMAGE_JPG_EXTENSION);
return upload(baseDir, file, DEFAULT_ALLOWED_EXTENSION);
}
catch (Exception e)
{
@ -97,36 +118,40 @@ public class FileUploadUtils
* @throws FileSizeLimitExceededException
* @throws FileNameLengthLimitExceededException
* @throws IOException
* @throws InvalidExtensionException
*/
public static final String upload(String baseDir, MultipartFile file, String extension)
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, InvalidExtensionException
{
int fileNamelength = file.getOriginalFilename().length();
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
{
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
}
assertAllowed(file);
assertAllowed(file, allowedExtension);
String fileName = extractFilename(file, extension);
String fileName = extractFilename(file);
File desc = getAbsoluteFile(baseDir, baseDir + fileName);
File desc = getAbsoluteFile(baseDir, fileName);
file.transferTo(desc);
return fileName;
}
public static final String extractFilename(MultipartFile file, String extension)
/**
*
*/
public static final String extractFilename(MultipartFile file)
{
String filename = file.getOriginalFilename();
filename = DateUtils.datePath() + "/" + encodingFilename(filename) + extension;
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
filename = DateUtils.datePath() + File.separator + encodingFilename(filename) + "." + extension;
return filename;
}
private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException
{
File desc = new File(File.separator + filename);
File desc = new File(uploadDir + File.separator + filename);
if (!desc.getParentFile().exists())
{
@ -155,13 +180,57 @@ public class FileUploadUtils
* @param file
* @return
* @throws FileSizeLimitExceededException
* @throws InvalidExtensionException
*/
public static final void assertAllowed(MultipartFile file) throws FileSizeLimitExceededException
public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
throws FileSizeLimitExceededException, InvalidExtensionException
{
long size = file.getSize();
if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
{
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
}
String filename = file.getOriginalFilename();
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
{
if (allowedExtension == IMAGE_EXTENSION)
{
throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, filename);
}
else if (allowedExtension == FLASH_EXTENSION)
{
throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, filename);
}
else if (allowedExtension == MEDIA_EXTENSION)
{
throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, filename);
}
else
{
throw new InvalidExtensionException(allowedExtension, extension, filename);
}
}
}
/**
* MIMEMIME
*
* @param extension
* @param allowedExtension
* @return
*/
public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
{
for (String str : allowedExtension)
{
if (str.equalsIgnoreCase(extension))
{
return true;
}
}
return false;
}
}

Loading…
Cancel
Save