java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > 将二进制文件流转化为MockMultipartFile文件

如何将二进制文件流转化为MockMultipartFile文件

作者:唯有遗忘最漫长

文章主要介绍了如何使用Spring框架中的MockMultipartFile类来模拟文件上传,并处理上传逻辑,包括获取二进制文件流、创建MockMultipartFile对象、处理上传逻辑和返回响应,还解释了MockMultipartFile的功能和二进制文件流的定义

一、名词解释及业务解释

1.具体业务流程

获取二进制文件流

创建 MockMultipartFile 对象

处理上传逻辑

返回响应

2.转换对象解释

1. MockMultipartFile

功能:用于模拟文件上传场景,特别是在测试中非常有用。

具体源码:

package org.springframework.mock.web;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

public class MockMultipartFile implements MultipartFile {
    private final String name;
    private final String originalFilename;
    @Nullable
    private final String contentType;
    private final byte[] content;

    public MockMultipartFile(String name, @Nullable byte[] content) {
        this(name, "", (String)null, (byte[])content);
    }

    public MockMultipartFile(String name, InputStream contentStream) throws IOException {
        this(name, "", (String)null, (byte[])FileCopyUtils.copyToByteArray(contentStream));
    }

    public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) {
        Assert.hasLength(name, "Name must not be empty");
        this.name = name;
        this.originalFilename = originalFilename != null ? originalFilename : "";
        this.contentType = contentType;
        this.content = content != null ? content : new byte[0];
    }

    public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream) throws IOException {
        this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
    }

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

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

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

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

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

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

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

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

构造参数

2. 二进制文件流

定义:文件的原始数据以字节形式存储,可以是任何类型的文件(如图片、文档等)。

获取方式

二、编码过程

1.引入spring依赖

        //引入spring-test依赖
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.8</version>
        </dependency>

2.写方法

private String extracted(byte[] data) {
    //创建缓存输出流
        BufferedOutputStream bos = null;
    //  FileOutputStream流是指文件字节输出流,专用于输出原始字节流如图像数据等,其继承OutputStream类,拥有输出流的基本特性
        FileOutputStream fos = null;
    //创建文件对象
        File file = null;
        String fileName1 = "1.png";
        String filePath = System.getProperty("user.dir") + File.separator + File.separator;
        try {
            File file1 = new File(filePath);
            if (!file1.exists() && file1.isDirectory()) {
                file1.mkdirs();
            }
        // file 进行赋值
            file = new File(filePath + "\\" + fileName1);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
        //将data写入文件中
            bos.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //创建 MockMultipartFile 对象 MockMultipartFile implements MultipartFile
        MockMultipartFile mockMultipartFile = null;
        try {
        //将本地的文件 转换为输出流 进行 转格式
            FileInputStream inputStream = new FileInputStream(file);
       //通过 MockMultipartFile 带参构造进行 创建对象 及赋值
            mockMultipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(),
                    inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文