|
@@ -1,5 +1,6 @@
|
|
|
package sqgxy.xxydz.module;
|
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import sqgxy.xxydz.exception.HintException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -32,13 +33,14 @@ public class HeaderImgUpload {
|
|
|
public static String headPortraitUpload(MultipartFile file) throws IOException, HintException {
|
|
|
|
|
|
System.out.println(file.getContentType());
|
|
|
- if (!FILE_TYPE.contains(file.getContentType())) {
|
|
|
- throw new HintException("上传的文件类型只能是:" + FILE_TYPE.toString());
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
String path = System.getProperty("user.dir");
|
|
|
File upload = new File(path, FILE_PATH_PREFIX);
|
|
|
-
|
|
|
+ String suffix = getFileExtension(file);
|
|
|
log.info("目录upload: " + upload.toString());
|
|
|
if (!upload.exists()) {
|
|
|
log.info("目录不存在,开始创建: " + upload.toString());
|
|
@@ -52,12 +54,19 @@ public class HeaderImgUpload {
|
|
|
if (!file.isEmpty()) {
|
|
|
log.info("开始保存");
|
|
|
|
|
|
- file1 = new File(upload, fileName + FILE_PATH_SUFFIX);
|
|
|
+ file1 = new File(upload, fileName + "." + suffix);
|
|
|
file.transferTo(file1);
|
|
|
} else {
|
|
|
throw new NullPointerException("文件不能为空");
|
|
|
}
|
|
|
- return FILE_PATH_PREFIX_READ + File.separator + fileName + FILE_PATH_SUFFIX;
|
|
|
+ return FILE_PATH_PREFIX_READ + File.separator + fileName + "."+ suffix;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getFileExtension(MultipartFile file) {
|
|
|
+ String fileName = StringUtils.cleanPath(file.getOriginalFilename());
|
|
|
+ int dotIndex = fileName.lastIndexOf('.');
|
|
|
+ return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|