ANDRU-PC\Andru
2022-08-11 f01be5b2e298fa14f4094be8a51fcb891a9be07a
Merge remote-tracking branch 'origin/master'
1个文件已添加
1个文件已修改
43 ■■■■■ 已修改文件
pom.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/util/MultipartFileUtil.java 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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>
src/main/java/com/hx/util/MultipartFileUtil.java
New file
@@ -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);
    }
}