From 3432ffb9f111fd5f75e7d9a3fce6e4c3f459a3f9 Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期四, 11 八月 2022 17:16:48 +0800
Subject: [PATCH] 文件转化

---
 src/main/java/com/hx/util/MultipartFileUtil.java |   38 ++++++++++++++++++++++++++++++++++++++
 pom.xml                                          |    5 +++++
 2 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/pom.xml b/pom.xml
index 03e7429..8352058 100644
--- a/pom.xml
+++ b/pom.xml
@@ -39,6 +39,11 @@
             <artifactId>cos_api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>commons-fileupload</groupId>
+            <artifactId>commons-fileupload</artifactId>
+        </dependency>
+
         <!--鍙戦�侀偖浠�-->
         <dependency>
             <groupId>com.sun.mail</groupId>
diff --git a/src/main/java/com/hx/util/MultipartFileUtil.java b/src/main/java/com/hx/util/MultipartFileUtil.java
new file mode 100644
index 0000000..04cac56
--- /dev/null
+++ b/src/main/java/com/hx/util/MultipartFileUtil.java
@@ -0,0 +1,38 @@
+package com.hx.util;
+
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileItemFactory;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.io.IOUtils;
+import org.springframework.http.MediaType;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
+
+import java.io.*;
+
+public class MultipartFileUtil {
+
+
+    /**鏂囦欢杞寲
+     * File to MultipartFile
+     * @param file
+     * @return
+     */
+    public static MultipartFile getMultipartFile(File file) {
+        FileItem item = new DiskFileItemFactory().createItem("file"
+                , MediaType.MULTIPART_FORM_DATA_VALUE
+                , true
+                , file.getName());
+        try (InputStream input = new FileInputStream(file);
+             OutputStream os = item.getOutputStream()) {
+            // 娴佽浆绉�
+            IOUtils.copy(input, os);
+        } catch (Exception e) {
+            throw new IllegalArgumentException("Invalid file: " + e, e);
+        }
+        return new CommonsMultipartFile(item);
+    }
+
+
+
+}

--
Gitblit v1.8.0