base64转MultipartFile方法

34 篇文章 0 订阅
本文介绍了如何在SpringMVC中使用`BASE64Decoder`将Base64编码的字符串转换为`MultipartFile`,以便处理上传的图片文件。
摘要由CSDN通过智能技术生成

实践过,这个方法可用,小记一下,防止以后再用到

import sun.misc.BASE64Decoder;    

public MultipartFile base64ToMultipartFile (String s) {
        MultipartFile image = null;
        StringBuilder base64 = new StringBuilder("");
        if (s != null && !"".equals(s)) {
            base64.append(s);
            String[] baseStrs = base64.toString().split(",");
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] b = new byte[0];
            try {
                b = decoder.decodeBuffer(baseStrs[1]);
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (int j = 0; j < b.length; ++j) {
                if (b[j] < 0) {
                    b[j] += 256;
                }
            }
            image = new  BASE64DecodedMultipartFile(b, baseStrs[0]);
        }
        return image;
    }

BASE64DecodedMultipartFile


import org.springframework.web.multipart.MultipartFile;
import java.io.*;

public class BASE64DecodedMultipartFile implements MultipartFile {

    private final byte[] imgContent;
    private final String header;

    public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
        this.imgContent = imgContent;
        this.header = header.split(";")[0];
    }

    @Override
    public String getName() {
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }

    @Override
    public String getOriginalFilename() {
        return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + 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() throws IOException {
        return imgContent;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(imgContent);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(imgContent);
    }

}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中,Base64编码通常用于将二进制数据(如图像、文件内容)换为可文本表示的形式。当你想要从Base64字符串换回`MultipartFile`,这是因为`MultipartFile`是Spring MVC中用于处理上传文件的模型属性,而Base64的解码可以让你把字符串还原成原始二进制文件。 首先,你需要一个Base64编码的字符串,然后将其解码成字节数组。接着,你可以创建一个新的`InputStream`或`ByteArrayInputStream`,并将这个字节数组传递给`MultipartFile`构造函数,或者使用`FileUtils.readFileToByteArray()`等方法读取为字节流。 以下是一个简单的示例代码片段: ```java import org.springframework.web.multipart.MultipartFile; import java.util.Base64; import java.io.ByteArrayInputStream; import org.springframework.util.FileCopyUtils; public MultipartFile base64ToMultipartFile(String base64EncodedData) { byte[] decodedBytes = Base64.getDecoder().decode(base64EncodedData); ByteArrayInputStream bais = new ByteArrayInputStream(decodedBytes); try { // 使用FileCopyUtils确保字节流被正确关闭 return new MultipartFile("file", "originalFilename", "application/octet-stream", bais, decodedBytes.length); } catch (Exception e) { throw new RuntimeException("Failed to convert Base64 to MultipartFile", e); } } // 如果你有完整的Base64字符串,可以这样使用: String base64String = "your_base64_string_here"; MultipartFile file = base64ToMultipartFile(base64String); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sunny_yiyi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值