【Java】如何将二进制转换成MultipartFile

业务场景:前端发送请求到后端进行文件上传,后端接收文件并调用第三方接口进行文件处理,响应格式为二进制,然后我们需要将二进制进行转换为MultipartFile进行文件上传。

如果你想要将File转换成MultipartFile,可以参考:【Java】如何将File转换成MultipartFile_java file 转multipartfile_MXin5的博客-CSDN博客

1.传递二进制responseBytes和file的名称即可。

java 复制代码
// responseBytes为二进制流,file.getOriginalFilename()为文件的名称。
ByteArrayMultipartFile responseFile = new ByteArrayMultipartFile(responseBytes, "file", file.getOriginalFilename(), "application/octet-stream");
  1. 将二进制流转换成MultipartFile的类:
java 复制代码
import org.springframework.web.multipart.MultipartFile;

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

import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

/**
 * 将二进制流转换成MultipartFile类的对象
 */
public class ByteArrayMultipartFile implements MultipartFile {

    private final byte[] content;
    private final String name;
    private final String originalFilename;
    private final String contentType;

    public ByteArrayMultipartFile(byte[] content, String name, String originalFilename, String contentType) {
        this.content = content;
        this.name = name;
        this.originalFilename = originalFilename;
        this.contentType = contentType;
    }

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

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

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

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

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

    @Override
    public byte[] getBytes() {
        return content;
    }

    @Override
    public InputStream getInputStream() {
        return new ByteArrayInputStream(content);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        Files.copy(new ByteArrayInputStream(content), dest.toPath(), new CopyOption[]{StandardCopyOption.REPLACE_EXISTING});
    }

    @Override
    public void transferTo(Path dest) throws IOException, IllegalStateException {
        Files.copy(new ByteArrayInputStream(content), dest, new CopyOption[]{StandardCopyOption.REPLACE_EXISTING});
    }
}

直接使用

相关推荐
低代码布道师3 分钟前
外包项目管理平台全栈实战课02搭建数据底座:Next.js + PostgreSQL + Prisma 7
开发语言·javascript·postgresql·全栈开发
CV艺术家4 分钟前
openssL生成免费的证书
java·linux·服务器
格林威6 分钟前
C# 图像使用AVX2指令集:使用OpenCvSharp实现字节图像解压缩速度和map_image算子速度提升
开发语言·图像处理·人工智能·计算机视觉·c#·视觉检测·工业相机
(Charon)11 分钟前
【C++】网络缓冲区设计(四):epoll + Reactor中Buffer的完整读写流程
开发语言·c++
全速向光12 分钟前
手机玩我的世界Java版:FCL启动器下载使用指南
java·智能手机·游戏程序
pjj1985414 分钟前
Hadoop-python中使用大数据1
java·ide·eclipse
好好沉淀19 分钟前
巧用 Set 去重与复合键:高效统计分组内不重复元素的数量
java·spring boot·后端
西西弗Sisyphus24 分钟前
Qt 实现一个 Windows 启动项查看器
开发语言·windows·qt
Wang's Blog27 分钟前
Java框架快速入门: Spring Security+OAuth2之工程结构与开发环境配置
java·spring·状态模式