Skip to content

Commit

Permalink
2.2 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Sep 29, 2019
1 parent d6fb61b commit 8b5c04c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 2 additions & 5 deletions eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import me.zhengjie.exception.BadRequestException;
import org.springframework.web.multipart.MultipartFile;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -233,11 +231,10 @@ public static String getFileType(String type) {
return "视频";
} else return "其他";
}

public static String getFileTypeByMimeType(String type) {
// URLConnection.guessContentTypeFromName("." + type)
// way 2 new MimetypesFileTypeMap().getContentType("."+ "txt")
String mimeType = new MimetypesFileTypeMap().getContentType("." + type);
return mimeType.split("\\/")[0];
return mimeType.split("\\/")[0];
}
public static void checkSize(long maxSize, long size) {
if(size > (maxSize * 1024 * 1024)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public LocalStorageDTO findById(Long id) {
public LocalStorageDTO create(String name, MultipartFile multipartFile) {
FileUtil.checkSize(maxSize, multipartFile.getSize());
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
String type = FileUtil.getFileTypeByMimeType(suffix);
// 可自行选择方式
// String type = FileUtil.getFileTypeByMimeType(suffix);
String type = FileUtil.getFileType(suffix);
File file = FileUtil.upload(multipartFile, path + type + File.separator);
try {
name = StringUtils.isBlank(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name;
Expand Down

0 comments on commit 8b5c04c

Please sign in to comment.