史上最完整Java中将File转化为MultipartFile的方法(附阿里云腾讯云对象存储API对照)

业务中需要调用别人提供的接口进行文件上传,但别人的接口只能上传MultipartFile类型的文件(吐槽一下,也不知道是哪个二货设计的这种接口)。所以需要在我们的业务代码中将File转化为MultipartFile。提供两种方法给大家。

一、使用MockMultipartFile类进行转换

这个方法比较简单,代码如下:

import java.io.File;
import java.io.FileInputStream;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.mock.web.MockMultipartFile;
import org.apache.http.entity.ContentType;
 
 
File pdfFile = new File("D://test.pdf");
FileInputStream fileInputStream = new FileInputStream(pdfFile);
MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
					ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);

看上去很简捷,也很舒服,但需要注意,使用MockMultipartFile需要引入spring-test的依赖:版本要跟随你的spring版本定

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.0.8.RELEASE</version>
</dependency>

但有的项目中没有引这个jar包,而且也不允许自己引入其他jar包,并且,导入这个jar包可能会带来jar包冲突的问题。这时候就没办法了吗?并不,接下来看方法二。

二、自己实现MultipartFile接口

接口要求传MultipartFile类型,但MultipartFile是个接口,我们无法构造,也就无法传递,方法一其实就是使用了MultipartFile的一个实现类来进行传递的,很不幸,在Spring框架中,MultipartFile的实现类可不多,查看继承关系:

看上去还不少哈,其实能用的也就这个MockMultipartFile,第一个是我自己实现的,第二个是个内部类,第三个了不得,里面有FileItem这么个类,又要依赖于别的jar包,所以都不方便,只有这个MockMultipartFile,我们点进去,干干净净,就他了,为了少引一个jar包,我们就模仿MockMultipartFile自己实现MultipartFile。

有同学说,是不是实现挺麻烦呢,不,一点也不,步骤如下:

新建一个类,随便叫啥名字都可以,比如,我的类叫MultipartFileDto,去实现MultipartFile接口。然后找到MockMultipartFile类的源码,拷贝到自己这个类里面,然后,就没有然后了,完事。当然你懒直接拷贝我的代码就可以了。

MultipartFileDto的代码如下:

import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: szz
 * @Date: 2019/1/16 下午4:33
 * @Version 1.0
 *
 * 负责将InputStream转换MultipartFile,可以少引一个jar包,本来用的是spring-test-4.3.9中的MockMultipartFile,直接提取出来使用
 */
public class MultipartFileDto implements MultipartFile {
    private final String name;

    private String originalFilename;

    private String contentType;

    private final byte[] content;

    /**
     * Create a new MultipartFileDto with the given content.
     * @param name the name of the file
     * @param content the content of the file
     */
    public MultipartFileDto(String name, byte[] content) {
        this(name, "", null, content);
    }

    /**
     * Create a new MultipartFileDto with the given content.
     * @param name the name of the file
     * @param contentStream the content of the file as stream
     * @throws IOException if reading from the stream failed
     */
    public MultipartFileDto(String name, InputStream contentStream) throws IOException {
        this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
    }

    /**
     * Create a new MultipartFileDto with the given content.
     * @param name the name of the file
     * @param originalFilename the original filename (as on the client's machine)
     * @param contentType the content type (if known)
     * @param content the content of the file
     */
    public MultipartFileDto(String name, String originalFilename, String contentType, byte[] content) {
        this.name = name;
        this.originalFilename = (originalFilename != null ? originalFilename : "");
        this.contentType = contentType;
        this.content = (content != null ? content : new byte[0]);
    }

    /**
     * Create a new MultipartFileDto with the given content.
     * @param name the name of the file
     * @param originalFilename the original filename (as on the client's machine)
     * @param contentType the content type (if known)
     * @param contentStream the content of the file as stream
     * @throws IOException if reading from the stream failed
     */
    public MultipartFileDto(String name, String originalFilename, String contentType, InputStream contentStream)
            throws IOException {

        this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getOriginalFilename() {
        return this.originalFilename;
    }

    @Override
    public String getContentType() {
        return this.contentType;
    }

    @Override
    public boolean isEmpty() {
        return (this.content.length == 0);
    }

    @Override
    public long getSize() {
        return this.content.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return this.content;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(this.content);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(this.content, dest);
    }

}

他的使用方式跟MockMultipartFile一模一样,我就是直接拷贝的嘛。直接new出来就完事了。

例如:

MultipartFile multipartFile = new MultipartFileDto("temp.jpg","temp.jpg",httpEntity.getContentType().getValue(), inputStream);

三、一个非常坑的地方

我是使用的Feign的方式进行上传,feign的接口如下:

大家看红框的地方,我已经指定了上传文件的key为file,接下来进行调用,代码如下:

我使用了 public MultipartFileDto(String name, String originalFilename, String contentType, byte[] content) 这个构造方法,第一个参数name我开始写的就是realFIle的文件名,结果老是报远程服务找不到,经过我司老司机的一顿调试,发现,我们在接口的定义的@RequestPart("file") MultipartFile file中的@RequestPart("file")是无效的,这里定义的file不是请求参数的key,相当于匹配不到对应的方法,所以会报服务找不到。

而MultipartFileDto构造方法中的第一个参数name才是请求参数的key,接口中要求第一个参数是file,所以构造方法中的第一个参数写死为"file",这样再去调用就没问题了。

四、总结

接口设计者应该考虑到接口的通用性,把接收参数设计成MultipartFile看似是一种MVC的方式,其实却是非常不好的,我们来对比一下阿里云的OSS和腾讯云的COS接口设计,大家一看便知。以Java的SDK为例:

1.阿里云的文件上传SDK

https://help.aliyun.com/document_detail/61872.html?spm=5176.8465980.0.0.44781450Rethay

阿里云的上传做的极为丰富,我们只拿简单上传来对照,简单上传分为:

  • 流式上传:使用ossClient.putObject上传数据流到OSS。分文上传字符串、上传Byte数组、上传网络流、上传文件流。
// 上传字符串。
String content = "Hello OSS";
ossClient.putObject("<yourBucketName>", "<yourObjectName>", new ByteArrayInputStream(content.getBytes()));

// 上传Byte数组。
byte[] content = "Hello OSS".getBytes();
ossClient.putObject("<yourBucketName>", "<yourObjectName>", new ByteArrayInputStream(content));

// 上传网络流。
InputStream inputStream = new URL("https://www.aliyun.com/").openStream();
ossClient.putObject("<yourBucketName>", "<yourObjectName>", inputStream);

// 上传文件流。
InputStream inputStream = new FileInputStream("<yourlocalFile>");
ossClient.putObject("<yourBucketName>", "<yourObjectName>", inputStream);
  • 文件上传
// 上传文件。<yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
ossClient.putObject("<yourBucketName>", "<yourObjectName>", new File("<yourLocalFile>"));

2.腾讯云的文件上传SDK

我司用的就是腾讯云的对象存储,不得不再次吐槽一下。

https://cloud.tencent.com/document/product/436/6474

腾讯云上传做的很简陋,接下来看看API,跟上传相关的就一个PUT Object,只能传File格式。

    // 指定要上传的文件
    File localFile = new File("exampleobject");
    // 指定要上传到的存储桶
    String bucketName = "examplebucket-1250000000";
    // 指定要上传到 COS 上对象键
    String key = "exampleobject";
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);

不解释,高下立判!


我的微信公众号:架构真经(id:gentoo666),分享Java干货,高并发编程,热门技术教程,微服务及分布式技术,架构设计,区块链技术,人工智能,大数据,Java面试题,以及前沿热门资讯等。每日更新哦!

参考资料:

  1. https://blog.csdn.net/hui008/article/details/81703342

 

  • 46
    点赞
  • 125
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

十步杀一人_千里不留行

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

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

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

打赏作者

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

抵扣说明:

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

余额充值