java 通过文件路径获得MulipartFile类型文件 - File转MulipartFile

java 通过文件路径获得MulipartFile类型文件 - File转MulipartFile

  • 方式一:使用MockMultipartFile函数
  • 方式二:数据流实现

思路:路径创建出File文件类型的对象,然后通过MultipartFile对象的MockMultipartFile方法将生成的File文件转化为MultipartFile文件,上代码;

方式一:使用MockMultipartFile函数

controller层:

package com.java.product.module.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Api(value = "uploadFile", tags = "上传文件")
@RequestMapping("/uploadFile")
@RestController
public class uploadFileController {
     
    @ApiOperation(value = "上传2", notes = "filePath:文件路径")
    @GetMapping("/getMulipartFile2")
    public MultipartFile getMulipartFiles2(String filePath) throws IOException {
     
        File file = new File(filePath);
        FileInputStream fileInputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                "application/sql", fileInputStream);
        long size = multipartFile.getSize();
        return multipartFile;
    }
}

依赖的包:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>

下面直接使用swagger来进行测试,没有配置swagger的同学使用postMan也可以;

java 通过文件路径获得MulipartFile类型文件 - File转MulipartFile_第1张图片

看来是没错了~对象不为空,且和上传文件的属性中的文件字节大小一样;
java 通过文件路径获得MulipartFile类型文件 - File转MulipartFile_第2张图片
然后F9放开断点~发现…
java 通过文件路径获得MulipartFile类型文件 - File转MulipartFile_第3张图片
报错了!?!?!
对,就是报错了,Type definition error - 类型定义错误,因为前端虽然能上传MultipartFile类型的文件,但是却无法解析,所以,不要把MultipartFile文件对象直接从后台给前端哦~


方式二:数据流实现

直接看代码吧

Controller层

package com.java.product.module.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

@Api(value = "uploadFile", tags = "上传文件")
@RequestMapping("/uploadFile")
@RestController
public class uploadFileController {
     

    @ApiOperation(value = "上传1", notes = "filePath:文件路径")
    @GetMapping("/getMulipartFile1")
    public MultipartFile getMulipartFile1(String filePath) {
     
        File f = new File(filePath);
        String fieldName = f.getName();
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem("file", "application/sql", false, fieldName);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
     
            FileInputStream fis = new FileInputStream(f);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
     
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
     
            e.printStackTrace();
        }

        MultipartFile mfile = new CommonsMultipartFile(item);
        long size = mfile.getSize();
        return mfile;
    }
}

POM文件依赖:

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.2</version>
</dependency>

然后运行看转化后的文件大小,如下图,依然成功了~
java 通过文件路径获得MulipartFile类型文件 - File转MulipartFile_第4张图片

你可能感兴趣的:(Java开发点滴,随记,File转Mulipart)