|
@@ -0,0 +1,37 @@
|
|
|
+package com.fuzamei.utils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import com.mchange.util.AssertException;
|
|
|
+
|
|
|
+public final class FileTransferUtil {
|
|
|
+ private static final String SP = File.separator;
|
|
|
+
|
|
|
+ private FileTransferUtil(){
|
|
|
+ throw new AssertException("instaniation is not permitted");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final void transfer(MultipartFile multipartFile,String directoryPath){
|
|
|
+ File dir = new File(directoryPath);
|
|
|
+ if(!dir.isDirectory()){
|
|
|
+ throw new RuntimeException("路径需要是文件夹路径");
|
|
|
+ }
|
|
|
+ if(!dir.exists()){
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ String filename = System.currentTimeMillis() + multipartFile.getOriginalFilename();//在文件名前面加一个时间戳
|
|
|
+ String filePath = directoryPath+SP+filename;
|
|
|
+ File newFile = new File(filePath);
|
|
|
+ try {
|
|
|
+ multipartFile.transferTo(newFile);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if(newFile!=null){
|
|
|
+ newFile.delete();
|
|
|
+ }
|
|
|
+ throw new RuntimeException("文件上传出现异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|