|
@@ -1,7 +1,11 @@
|
|
package com.ruoyi.common.utils.file;
|
|
package com.ruoyi.common.utils.file;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.io.*;
|
|
|
|
+
|
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
|
+import org.apache.commons.fileupload.FileItem;
|
|
|
|
+import org.apache.commons.fileupload.FileItemFactory;
|
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
import org.apache.commons.io.FilenameUtils;
|
|
import org.apache.commons.io.FilenameUtils;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import com.ruoyi.common.config.RuoYiConfig;
|
|
import com.ruoyi.common.config.RuoYiConfig;
|
|
@@ -12,6 +16,7 @@ import com.ruoyi.common.exception.file.InvalidExtensionException;
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
import com.ruoyi.common.utils.uuid.IdUtils;
|
|
import com.ruoyi.common.utils.uuid.IdUtils;
|
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 文件上传工具类
|
|
* 文件上传工具类
|
|
@@ -97,15 +102,19 @@ public class FileUploadUtils
|
|
* @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,
|
|
|
|
- InvalidExtensionException
|
|
|
|
- {
|
|
|
|
|
|
+ throws Exception {
|
|
int fileNamelength = file.getOriginalFilename().length();
|
|
int fileNamelength = 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);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ String extension = getExtension(file);
|
|
|
|
+ if (isPicture(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
|
|
|
|
+ File files = multipartFileToFile(file);
|
|
|
|
+// Thumbnails.of(files).scale(0.1f).toFile(files);//按比例缩小
|
|
|
|
+ Thumbnails.of(files).scale(1f).outputQuality(0.5f).toFile(files);
|
|
|
|
+ file = fileToMultipartFile(files);
|
|
|
|
+ }
|
|
assertAllowed(file, allowedExtension);
|
|
assertAllowed(file, allowedExtension);
|
|
|
|
|
|
String fileName = extractFilename(file);
|
|
String fileName = extractFilename(file);
|
|
@@ -117,6 +126,95 @@ public class FileUploadUtils
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * MultipartFile 转 File
|
|
|
|
+ *
|
|
|
|
+ * @param file
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public static File multipartFileToFile(MultipartFile file) throws Exception {
|
|
|
|
+
|
|
|
|
+ File toFile = null;
|
|
|
|
+ if (file.equals("") || file.getSize() <= 0) {
|
|
|
|
+ file = null;
|
|
|
|
+ } else {
|
|
|
|
+ InputStream ins = null;
|
|
|
|
+ ins = file.getInputStream();
|
|
|
|
+ toFile = new File(file.getOriginalFilename());
|
|
|
|
+ inputStreamToFile(ins, toFile);
|
|
|
|
+ ins.close();
|
|
|
|
+ }
|
|
|
|
+ return toFile;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //获取流文件
|
|
|
|
+ private static void inputStreamToFile(InputStream ins, File file) {
|
|
|
|
+ try {
|
|
|
|
+ OutputStream os = new FileOutputStream(file);
|
|
|
|
+ int bytesRead = 0;
|
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
|
+ while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
|
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ os.close();
|
|
|
|
+ ins.close();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 删除本地临时文件
|
|
|
|
+ * @param file
|
|
|
|
+ */
|
|
|
|
+ public static void delteTempFile(File file) {
|
|
|
|
+ if (file != null) {
|
|
|
|
+ File del = new File(file.toURI());
|
|
|
|
+ del.delete();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public static MultipartFile fileToMultipartFile(File file) {
|
|
|
|
+ FileItem fileItem = createFileItem(file);
|
|
|
|
+ MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
|
|
|
|
+ return multipartFile;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static FileItem createFileItem(File file) {
|
|
|
|
+ FileItemFactory factory = new DiskFileItemFactory(16, null);
|
|
|
|
+ FileItem item = factory.createItem("textField", "text/plain", true, file.getName());
|
|
|
|
+ int bytesRead = 0;
|
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
|
+ try {
|
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
|
+ OutputStream os = item.getOutputStream();
|
|
|
|
+ while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
|
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ os.close();
|
|
|
|
+ fis.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return item;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 是否是图片类型
|
|
|
|
+ * @param extension 后缀名
|
|
|
|
+ * @param allowedExtension 校验规则
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static boolean isPicture (String extension, String[] allowedExtension)
|
|
|
|
+ {
|
|
|
|
+ for (String str : allowedExtension)
|
|
|
|
+ {
|
|
|
|
+ if (str.equalsIgnoreCase(extension))
|
|
|
|
+ {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 编码文件名
|
|
* 编码文件名
|
|
*/
|
|
*/
|
|
public static final String extractFilename(MultipartFile file)
|
|
public static final String extractFilename(MultipartFile file)
|