File tree Expand file tree Collapse file tree 2 files changed +10
-3
lines changed
eladmin-common/src/main/java/me/zhengjie/utils
eladmin-tools/src/main/java/me/zhengjie/service/impl Expand file tree Collapse file tree 2 files changed +10
-3
lines changed Original file line number Diff line number Diff line change 8
8
import cn .hutool .poi .excel .ExcelWriter ;
9
9
import me .zhengjie .exception .BadRequestException ;
10
10
import org .springframework .web .multipart .MultipartFile ;
11
+
12
+ import javax .activation .MimetypesFileTypeMap ;
11
13
import javax .servlet .ServletOutputStream ;
12
14
import javax .servlet .http .HttpServletResponse ;
13
15
import java .io .*;
@@ -231,7 +233,12 @@ public static String getFileType(String type) {
231
233
return "视频" ;
232
234
} else return "其他" ;
233
235
}
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
+ }
235
242
public static void checkSize (long maxSize , long size ) {
236
243
if (size > (maxSize * 1024 * 1024 )){
237
244
throw new BadRequestException ("文件超出规定大小" );
Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ public LocalStorageDTO findById(Long id) {
63
63
public LocalStorageDTO create (String name , MultipartFile multipartFile ) {
64
64
FileUtil .checkSize (maxSize , multipartFile .getSize ());
65
65
String suffix = FileUtil .getExtensionName (multipartFile .getOriginalFilename ());
66
- String type = FileUtil .getFileType (suffix );
66
+ String type = FileUtil .getFileTypeByMimeType (suffix );
67
67
File file = FileUtil .upload (multipartFile , path + type + File .separator );
68
68
try {
69
69
name = StringUtils .isBlank (name ) ? FileUtil .getFileNameNoEx (multipartFile .getOriginalFilename ()) : name ;
@@ -115,4 +115,4 @@ public void deleteAll(Long[] ids) {
115
115
localStorageRepository .delete (storage );
116
116
}
117
117
}
118
- }
118
+ }
You can’t perform that action at this time.
0 commit comments