|
|
@@ -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);
|