Forráskód Böngészése

4/10 登录修改

lwhhszx 1 éve
szülő
commit
1ff04ad232

+ 29 - 2
src/main/java/com/example/xiaoshiweixinback/business/utils/FileUtils.java

@@ -4,6 +4,7 @@ import cn.hutool.core.util.IdUtil;
 import com.example.xiaoshiweixinback.XiaoshiWeixinbackApplication;
 import org.springframework.boot.system.ApplicationHome;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
 import java.net.URL;
@@ -122,6 +123,32 @@ public class FileUtils {
             return  file;
         }
     }
-
-
+    public static File multipartFileToFile(MultipartFile file) throws Exception {
+        File toFile = null;
+        if (file.equals("") || file.getSize() <= 0) {
+            file = null;
+        } else {
+            InputStream ins = null;
+            ins = file.getInputStream();
+            toFile = new File(file.getOriginalFilename());
+            inputStreamToFile(ins, toFile);
+            ins.close();
+        }
+        return toFile;
+    }
+    //获取流文件
+    private static void inputStreamToFile(InputStream ins, File file) {
+        try {
+            OutputStream os = new FileOutputStream(file);
+            int bytesRead = 0;
+            byte[] buffer = new byte[8192];
+            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
+                os.write(buffer, 0, bytesRead);
+            }
+            os.close();
+            ins.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 }