Pārlūkot izejas kodu

新增图片以外的上传

s1eepycat 3 gadi atpakaļ
vecāks
revīzija
dc08b28740

+ 29 - 14
src/main/java/com/zhiqiyun/open/core/service/impl/UploadFileInfoServiceImpl.java

@@ -25,6 +25,8 @@ import java.io.File;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 @Slf4j
 @Service
@@ -55,10 +57,21 @@ public class UploadFileInfoServiceImpl extends ServiceImpl<UploadFileInfoMapper,
                 throw new UploadException("上传文件大小不能超过" + (maxSize / 1024) + "M");
             }
 
-            MagicMatch magicMatch = Magic.getMagicMatch(fileBuff);
+            String pattern = ".[a-zA-Z]{3,4}$";
 
-            String extension = magicMatch.getExtension();
-            String mimeType = magicMatch.getMimeType();
+            String extension;
+            String mimeType=null;
+            Pattern r = Pattern.compile(pattern);
+            Matcher m = r.matcher(originalFilename);
+            if (m.find()){
+                extension = m.group(0).substring(1);
+            }else {
+                MagicMatch magicMatch = Magic.getMagicMatch(fileBuff);
+
+                extension = magicMatch.getExtension();
+                mimeType = magicMatch.getMimeType();
+
+            }
 
             // 验证文件类型
             if (extension == null) {
@@ -84,22 +97,24 @@ public class UploadFileInfoServiceImpl extends ServiceImpl<UploadFileInfoMapper,
             File file = new File(filePath);
 
 
-            try {
-                ByteArrayInputStream in = new ByteArrayInputStream(fileBuff);
-                BufferedImage bi = ImageIO.read(in);
-                if (bi.getHeight() > 1000 || bi.getWidth() > 1000) {
-                    Thumbnails.of(bi).size(1000, 1000).toFile(file);
-                } else {
-                    Thumbnails.of(bi).size(bi.getWidth(), bi.getHeight()).toFile(file);
+            if(mimeType!=null){
+                try {
+                    ByteArrayInputStream in = new ByteArrayInputStream(fileBuff);
+                    BufferedImage bi = ImageIO.read(in);
+                    if (bi.getHeight() > 1000 || bi.getWidth() > 1000) {
+                        Thumbnails.of(bi).size(1000, 1000).toFile(file);
+                    } else {
+                        Thumbnails.of(bi).size(bi.getWidth(), bi.getHeight()).toFile(file);
+                    }
+                } catch (IOException e) {
+                    log.warn("upload is not image file", e);
+                    throw new UploadException("只能上传图片文件");
                 }
-            } catch (IOException e) {
-                log.warn("upload is not image file", e);
-                throw new UploadException("只能上传图片文件");
             }
 
             result.setSuccess(true);
             result.setMessage("上传文件成功");
-            result.setMimeType(mimeType);
+            if(mimeType!=null) result.setMimeType(mimeType);
             // 获取服务地址和端口
             String url = filePath.replaceFirst(this.savePath, "");
             result.setSrc(fileHost + url);