|
@@ -1,106 +1,172 @@
|
|
|
package com.pj.project4sp.uploadfile;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.io.InputStream;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.Random;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
+import cn.hutool.core.img.ImgUtil;
|
|
|
+import cn.hutool.log.StaticLog;
|
|
|
+import com.drew.imaging.ImageMetadataReader;
|
|
|
+import com.drew.metadata.Metadata;
|
|
|
+import com.drew.metadata.MetadataException;
|
|
|
+import com.drew.metadata.exif.ExifDirectoryBase;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import com.pj.current.config.SystemObject;
|
|
|
|
|
|
+import javax.sound.midi.Soundbank;
|
|
|
+
|
|
|
/**
|
|
|
* 文件上传工具类(基于应用服务器的文件上传)
|
|
|
- * @author kong
|
|
|
*
|
|
|
+ * @author kong
|
|
|
*/
|
|
|
@Component
|
|
|
public class UploadUtil {
|
|
|
|
|
|
- /** 注入配置 */
|
|
|
- public static UploadConfig uploadConfig;
|
|
|
- @Autowired
|
|
|
- public void setUploadConfig(UploadConfig uploadConfig) {
|
|
|
- UploadUtil.uploadConfig = uploadConfig;
|
|
|
- }
|
|
|
-
|
|
|
- /** 将文件名保存在服务器硬盘上,并把文件对应的http地址返回给前台 */
|
|
|
- public static String saveFile(MultipartFile file, String flieTypeFolder) {
|
|
|
-
|
|
|
- // 1、计算路径
|
|
|
- // 根据日期计算需要保存的文件夹
|
|
|
- String currDateFolder = getCurrDateFolder();
|
|
|
- // 文件名
|
|
|
- String fileName = getMarking28() + '.' + getSuffixName(file.getOriginalFilename());
|
|
|
- // 需要保存到的文件夹地址
|
|
|
- String fileFolder = new File(uploadConfig.rootFolder).getAbsolutePath() + "/" +
|
|
|
- uploadConfig.httpPrefix + flieTypeFolder + currDateFolder + "/";
|
|
|
- // 对外暴露的http路径
|
|
|
- String httpUrl = getDoMain() + uploadConfig.httpPrefix + flieTypeFolder + currDateFolder + "/" + fileName;
|
|
|
-
|
|
|
- // 2、如果文件夹不存在,则先创建
|
|
|
- File dirFile = new File(fileFolder);
|
|
|
- if(dirFile.exists() == false) {
|
|
|
- dirFile.mkdirs();
|
|
|
- }
|
|
|
-
|
|
|
- // 3、开始转存文件
|
|
|
- try {
|
|
|
- File outFile = new File(fileFolder + fileName);
|
|
|
- file.transferTo(outFile);
|
|
|
+ /**
|
|
|
+ * 注入配置
|
|
|
+ */
|
|
|
+ public static UploadConfig uploadConfig;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setUploadConfig(UploadConfig uploadConfig) {
|
|
|
+ UploadUtil.uploadConfig = uploadConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将文件名保存在服务器硬盘上,并把文件对应的http地址返回给前台
|
|
|
+ */
|
|
|
+ public static String saveFile(MultipartFile file, String flieTypeFolder) {
|
|
|
+ AtomicInteger rote = new AtomicInteger(1);
|
|
|
+ try (InputStream is = file.getInputStream()) {
|
|
|
+ Metadata metadata = ImageMetadataReader.readMetadata(is);
|
|
|
+ metadata.getDirectories().forEach(directory -> {
|
|
|
+ if (directory.getName().equals("Exif IFD0")) {
|
|
|
+ directory.getTags().forEach(tag -> {
|
|
|
+ try {
|
|
|
+ rote.set(directory.getInt(tag.getTagType()));
|
|
|
+ } catch (MetadataException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ // 1、计算路径
|
|
|
+ // 根据日期计算需要保存的文件夹
|
|
|
+ String currDateFolder = getCurrDateFolder();
|
|
|
+ // 文件名
|
|
|
+ String fileName = getMarking28() + '.' + getSuffixName(file.getOriginalFilename());
|
|
|
+ // 需要保存到的文件夹地址
|
|
|
+ String fileFolder = new File(uploadConfig.rootFolder).getAbsolutePath() + "/" +
|
|
|
+ uploadConfig.httpPrefix + flieTypeFolder + currDateFolder + "/";
|
|
|
+ // 对外暴露的http路径
|
|
|
+ String httpPrefix = getDoMain() + uploadConfig.httpPrefix + flieTypeFolder + currDateFolder + "/";
|
|
|
+ String httpUrl;
|
|
|
+ // 2、如果文件夹不存在,则先创建
|
|
|
+ File dirFile = new File(fileFolder);
|
|
|
+ if (dirFile.exists() == false) {
|
|
|
+ dirFile.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3、开始转存文件
|
|
|
+ try {
|
|
|
+ File outFile = new File(fileFolder + fileName);
|
|
|
+ file.transferTo(outFile);
|
|
|
+ httpUrl = judgeRote(outFile, rote.get(), httpPrefix, fileFolder, fileName);
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
-
|
|
|
- // 4、将文件外网地址返回给前台
|
|
|
- return httpUrl;
|
|
|
- }
|
|
|
-
|
|
|
- /** 验证文件大小 */
|
|
|
- static void checkFileSize(MultipartFile file) {
|
|
|
- // 文件大小(B)
|
|
|
- long size = file.getSize();
|
|
|
+ // 4、将文件外网地址返回给前台
|
|
|
+ return httpUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 1 0°
|
|
|
+ * 3 180°
|
|
|
+ * 6 顺时针90°
|
|
|
+ * 8 逆时针90°
|
|
|
+ *
|
|
|
+ * @param inFile
|
|
|
+ * @param i
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String judgeRote(File inFile, int i, String httpUrl, String fileFolder, String filename) {
|
|
|
+ String newFilename = getMarking28() + ".jpg";
|
|
|
+ File outFile = new File(fileFolder + newFilename);
|
|
|
+ if (i == 1) {
|
|
|
+ return httpUrl + filename;
|
|
|
+ } else if (i == 3) {
|
|
|
+ ImgUtil.rotate(inFile, 180, outFile);
|
|
|
+ } else if (i == 6) {
|
|
|
+ ImgUtil.rotate(inFile, 90, outFile);
|
|
|
+ } else {
|
|
|
+ ImgUtil.rotate(inFile, -90, outFile);
|
|
|
+ }
|
|
|
+ return httpUrl + newFilename;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证文件大小
|
|
|
+ */
|
|
|
+ static void checkFileSize(MultipartFile file) {
|
|
|
+ // 文件大小(B)
|
|
|
+ long size = file.getSize();
|
|
|
if (size > uploadConfig.maxSize) {
|
|
|
- throw new RuntimeException("文件大小超出限制");
|
|
|
+ throw new RuntimeException("文件大小超出限制");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证指定文件名是否存在于指定后缀列表中
|
|
|
+ * 参数:文件名、后缀列表
|
|
|
+ * case:checkSubffix("123.jpg", "jpg,png,gif") 验证通过
|
|
|
+ */
|
|
|
+ static void checkSubffix(String fileName, String suffixList) {
|
|
|
+ // 获取后缀,并转为小写
|
|
|
+ String ext = getSuffixName(fileName).toLowerCase();
|
|
|
+ // 去空格,加逗号
|
|
|
+ String yxSuffix = suffixList.replace(" ", "") + ",";
|
|
|
+ if (yxSuffix.indexOf(ext + ",") == -1) {
|
|
|
+ throw new RuntimeException("文件后缀验证未通过:" + ext);
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 验证指定文件名是否存在于指定后缀列表中
|
|
|
- * 参数:文件名、后缀列表
|
|
|
- * case:checkSubffix("123.jpg", "jpg,png,gif") 验证通过
|
|
|
- */
|
|
|
- static void checkSubffix(String fileName, String suffixList) {
|
|
|
- // 获取后缀,并转为小写
|
|
|
- String ext = getSuffixName(fileName).toLowerCase();
|
|
|
- // 去空格,加逗号
|
|
|
- String yxSuffix = suffixList.replace(" ", "") + ",";
|
|
|
- if(yxSuffix.indexOf(ext + ",") == -1) {
|
|
|
- throw new RuntimeException("文件后缀验证未通过:" + ext);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /** 返回随机生成的唯一标示28位唯一标示符 */
|
|
|
- static String getMarking28() {
|
|
|
- return System.currentTimeMillis() + "" + new Random().nextInt(Integer.MAX_VALUE);
|
|
|
- }
|
|
|
-
|
|
|
- /** 取文件后缀 */
|
|
|
- static String getSuffixName(String fileName) {
|
|
|
- return fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
|
- }
|
|
|
-
|
|
|
- /** 返回今天的日期文件夹 */
|
|
|
- static String getCurrDateFolder() {
|
|
|
- String currDateFolder = new SimpleDateFormat("/yyyy/MM-dd").format(new Date());
|
|
|
- return currDateFolder;
|
|
|
- }
|
|
|
-
|
|
|
- /** 返回本服务器域名信息 */
|
|
|
- static String getDoMain() {
|
|
|
- return SystemObject.config.getDomain();
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回随机生成的唯一标示28位唯一标示符
|
|
|
+ */
|
|
|
+ static String getMarking28() {
|
|
|
+ return System.currentTimeMillis() + "" + new Random().nextInt(Integer.MAX_VALUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取文件后缀
|
|
|
+ */
|
|
|
+ static String getSuffixName(String fileName) {
|
|
|
+ return fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回今天的日期文件夹
|
|
|
+ */
|
|
|
+ static String getCurrDateFolder() {
|
|
|
+ String currDateFolder = new SimpleDateFormat("/yyyy/MM-dd").format(new Date());
|
|
|
+ return currDateFolder;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回本服务器域名信息
|
|
|
+ */
|
|
|
+ static String getDoMain() {
|
|
|
+ return SystemObject.config.getDomain();
|
|
|
+ }
|
|
|
+
|
|
|
}
|