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

本文介绍了两种在Java中将文件路径转换为MultipartFile的方法:一是利用MockMultipartFile,二是通过数据流实现。在使用MockMultipartFile时,需要注意前端无法解析MultipartFile对象,因此不应直接传递给前端。第二种方式通过数据流成功实现了转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

思路:路径创建出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也可以;

在这里插入图片描述

看来是没错了~对象不为空,且和上传文件的属性中的文件字节大小一样;
在这里插入图片描述
然后F9放开断点~发现…
在这里插入图片描述
报错了!?!?!
对,就是报错了,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>

然后运行看转化后的文件大小,如下图,依然成功了~
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风暖云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值