当前位置: 代码网 > it编程>编程语言>Java > Java中File、Base64、MultipartFile之间相互转换的代码详解

Java中File、Base64、MultipartFile之间相互转换的代码详解

2024年05月15日 Java 我要评论
文件转base64字符串/** * file转换为base64 * 注意:这里转换为base64后,是不包含文件head头的 */public static string filetobase64(f

文件转base64字符串

/**
 * file转换为base64
 * 注意:这里转换为base64后,是不包含文件head头的
 */
public static string filetobase64(file file) {
    base64.encoder base64 = base64.getencoder();
    string base64str = null;
    try (fileinputstream fis = new fileinputstream(file);
         bytearrayoutputstream bos = new bytearrayoutputstream()) {
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        base64str = base64.encodetostring(bos.tobytearray());
    } catch (ioexception e) {
        e.printstacktrace();
    }
    return base64str;
}

base64字符串转文件

/**
 * base64转化为file,并保存到指定路径下
 */
public static void base64tofile(string base, string path) {
    if (stringutils.isblank(base)) {
        return;
    }
    base64.decoder decoder = base64.getdecoder();
    try (outputstream out = new fileoutputstream(path)) {
        byte[] bytes = decoder.decode(base);
        for (int i = 0; i < bytes.length; ++i) {
            if (bytes[i] < 0) {
                bytes[i] += 256;
            }
        }
        out.write(bytes);
        out.flush();
    } catch (ioexception e) {
        e.printstacktrace();
    }
}

base64转化为file流

/**
 * base64转化为file流
 */
public static file base64tofile(string base64) {
    if (base64 == null || "".equals(base64)) {
        return null;
    }
    byte[] buff = base64.getdecoder().decode(base64);

    file file;
    try {
        file = file.createtempfile("tmp", null);
    } catch (ioexception e) {
        e.printstacktrace();
        return null;
    }
    try (fileoutputstream out = new fileoutputstream(file)) {
        out.write(buff);
    } catch (ioexception e) {
        e.printstacktrace();
    }
    return file;
}

base64转multipartfile

import org.springframework.web.multipart.multipartfile;

import java.io.*;
import java.util.base64;

/**
 * @author 笑小枫
 */
public class base64decodedmultipartfile implements multipartfile {
    private final byte[] imgcontent;
    private final string header;
    private final string filename;

    public base64decodedmultipartfile(byte[] imgcontent, string header, string filename) {
        this.imgcontent = imgcontent;
        this.header = header.split(";")[0];
        this.filename = filename;
    }

    @override
    public string getname() {
        return filename + "." + header.split("/")[1];
    }

    @override
    public string getoriginalfilename() {
        return filename + "." + header.split("/")[1];
    }

    @override
    public string getcontenttype() {
        return header.split(":")[1];
    }

    @override
    public boolean isempty() {
        return imgcontent == null || imgcontent.length == 0;
    }

    @override
    public long getsize() {
        return imgcontent.length;
    }

    @override
    public byte[] getbytes() {
        return imgcontent;
    }

    @override
    public inputstream getinputstream() {
        return new bytearrayinputstream(imgcontent);
    }

    @override
    public void transferto(file dest) throws ioexception, illegalstateexception {
        try (fileoutputstream fos = new fileoutputstream(dest)) {
            fos.write(imgcontent);
        }
    }

    /**
     * base64转multipartfile
     **/
    public static multipartfile base64convert(string base64, string header, string filename) {
        base64.decoder decoder = base64.getdecoder();
        byte[] b = decoder.decode(base64);
        //取索引为1的元素进行处理
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }
        // 处理过后的数据通过base64decodemultipartfile转换为multipartfile对象
        return new base64decodedmultipartfile(b, header, filename);
    }
}

byte数组转multipartfile

  • pom导入
<dependency>
    <groupid>org.apache.httpcomponents</groupid>
    <artifactid>httpclient</artifactid>
    <version>4.5.5</version>
</dependency>
<dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-test</artifactid>
      <version>release</version>
</dependency>
  • 代码部分
byte[] bytes = message.getpacket();
inputstream inputstream = new bytearrayinputstream(bytes);
multipartfile file = new mockmultipartfile(contenttype.application_octet_stream.tostring(), inputstream);

multipartfile转化为byte数组

byte[] bytes= file.getbytes();

以上就是java中file、base64、multipartfile之间相互转换的代码详解的详细内容,更多关于java file、base64、multipartfile转换的资料请关注代码网其它相关文章!

(0)

相关文章:

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

发表评论

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