当前位置: 代码网 > it编程>编程语言>Java > 如何将二进制文件流转化为MockMultipartFile文件

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

2025年02月13日 Java 我要评论
一、名词解释及业务解释1.具体业务流程获取二进制文件流:文件可以来自多个来源,如用户上传的文件、数据库存储的二进制数据、文件系统中的文件等。读取文件内容,转换为字节数组。创建 mockmultipar

一、名词解释及业务解释

1.具体业务流程

获取二进制文件流

  • 文件可以来自多个来源,如用户上传的文件、数据库存储的二进制数据、文件系统中的文件等。
  • 读取文件内容,转换为字节数组。

创建 mockmultipartfile 对象

  • 使用 mockmultipartfile 类,将字节数组、文件名和文件类型作为参数传入构造函数。
  • mockmultipartfile 是 spring 提供的一个类,主要用于在测试中模拟文件上传。

处理上传逻辑

  • 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);
    }
}

构造参数

  • name: 表单中对应的字段名(如 "file")。
  • originalfilename: 上传文件的原始文件名(如 "test.txt")。
  • contenttype: 文件的内容类型(如 "text/plain")。
  • content: 文件的字节内容(如 byte[])。

2. 二进制文件流

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

获取方式

  • 从输入流读取(如 inputstream)。
  • 从数据库中读取二进制数据。
  • 直接从文件系统中读取。

二、编码过程

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();
        }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com