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

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

文件转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 

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 

byte数组转MultiPartFile

  • POM导入
org.apache.httpcomponentshttpclient4.5.5org.springframeworkspring-testRELEASE
  • 代码部分
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转换的资料请关注IT俱乐部其它相关文章!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/java/10634.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部