Skip to content

Commit d6fb61b

Browse files
committed
Merge branch 'master' of github.com:elunez/eladmin into 2.3dev
2 parents 0a2ae87 + db1d6e4 commit d6fb61b

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import cn.hutool.poi.excel.ExcelWriter;
99
import me.zhengjie.exception.BadRequestException;
1010
import org.springframework.web.multipart.MultipartFile;
11+
12+
import javax.activation.MimetypesFileTypeMap;
1113
import javax.servlet.ServletOutputStream;
1214
import javax.servlet.http.HttpServletResponse;
1315
import java.io.*;
@@ -231,7 +233,12 @@ public static String getFileType(String type) {
231233
return "视频";
232234
} else return "其他";
233235
}
234-
236+
public static String getFileTypeByMimeType(String type) {
237+
// URLConnection.guessContentTypeFromName("." + type)
238+
// way 2 new MimetypesFileTypeMap().getContentType("."+ "txt")
239+
String mimeType = new MimetypesFileTypeMap().getContentType("." + type);
240+
return mimeType.split("\\/")[0];
241+
}
235242
public static void checkSize(long maxSize, long size) {
236243
if(size > (maxSize * 1024 * 1024)){
237244
throw new BadRequestException("文件超出规定大小");

eladmin-tools/src/main/java/me/zhengjie/service/impl/LocalStorageServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public LocalStorageDTO findById(Long id) {
6363
public LocalStorageDTO create(String name, MultipartFile multipartFile) {
6464
FileUtil.checkSize(maxSize, multipartFile.getSize());
6565
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
66-
String type = FileUtil.getFileType(suffix);
66+
String type = FileUtil.getFileTypeByMimeType(suffix);
6767
File file = FileUtil.upload(multipartFile, path + type + File.separator);
6868
try {
6969
name = StringUtils.isBlank(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name;
@@ -115,4 +115,4 @@ public void deleteAll(Long[] ids) {
115115
localStorageRepository.delete(storage);
116116
}
117117
}
118-
}
118+
}

0 commit comments

Comments
 (0)