فهرست منبع

Merge remote-tracking branch 'origin/master'

root 3 سال پیش
والد
کامیت
b6a7e1d04a

+ 42 - 16
src/main/java/com/zhiqiyun/open/core/service/impl/UploadFileInfoServiceImpl.java

@@ -23,8 +23,11 @@ import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.RandomAccessFile;
 import java.util.Arrays;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 @Slf4j
 @Service
@@ -55,10 +58,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) {
@@ -80,26 +94,38 @@ public class UploadFileInfoServiceImpl extends ServiceImpl<UploadFileInfoMapper,
                     throw new UploadException("创建上传目录失败");
                 }
             }
-            filePath = String.format("%s/%s.%s", filePath, DigestUtils.md5Hex(fileBuff), extension);
-            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){
+                filePath = String.format("%s/%s.%s", filePath, DigestUtils.md5Hex(fileBuff), extension);
+                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);
+                    }
+                } catch (IOException e) {
+                    log.warn("upload is not image file", e);
+                    throw new UploadException("只能上传图片文件");
+                }
+            }else {
+                filePath = String.format("%s/%s.%s", filePath, originalFilename, extension);
+                File file = new File(filePath);
+                try {
+                    RandomAccessFile file_test = new RandomAccessFile(file,"rw");
+                    file_test.write(fileBuff);
+                } catch (IOException e) {
+                    log.warn("upload is failed", 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);

+ 1 - 1
src/main/resources/application-alpha.properties

@@ -12,7 +12,7 @@ spring.redis.database=0
 ####################### uploader config ###############################
 uploader.max-size=20480000
 uploader.file-host=http://47.114.32.188:9800/src
-uploader.allow-file-types=jpg,jpeg,png,gif,mp4,mp3,xls,xlsx,pdf
+uploader.allow-file-types=jpg,jpeg,png,gif,mp4,mp3,xls,xlsx,pdf,doc
 uploader.save-path=/data/uploads
 ####################### spider config ###############################
 spider.phantomjs_executable_path_property=/data/phantomjs-2.1.1-linux-x86_64/bin/phantomjs

+ 1 - 1
src/main/resources/application-prod.properties

@@ -12,7 +12,7 @@ spring.redis.database=1
 ####################### uploader config ###############################
 uploader.max-size=20480000
 uploader.file-host=https://dbadmin.zhlc.liucheng.gov.cn/src
-uploader.allow-file-types=jpg,jpeg,png,gif,mp4,mp3,xls,xlsx,pdf
+uploader.allow-file-types=jpg,jpeg,png,gif,mp4,mp3,xls,xlsx,pdf,doc
 uploader.save-path=/data/data-center/uploads
 ####################### spider config ###############################
 spider.phantomjs_executable_path_property=/data/data-center/phantomjs-2.1.1/bin/phantomjs

+ 1 - 1
src/main/resources/application.properties

@@ -39,7 +39,7 @@ spring.oap.security.service-access-controller-class=com.zhiqiyun.open.router.con
 ####################### uploader config ###############################
 uploader.max-size=20480000
 uploader.file-host=http://127.0.0.1:9800/src
-uploader.allow-file-types=jpg,jpeg,png,gif
+uploader.allow-file-types=jpg,jpeg,png,gif,mp4,mp3,xls,xlsx,pdf,doc
 uploader.save-path=/tmp/uploads
 
 ####################### spider config ###############################