首页 > 其他分享 >base64ToMutipartFile(Base64转换为MutipartFile)

base64ToMutipartFile(Base64转换为MutipartFile)

时间:2023-02-06 09:35:45浏览次数:29  
标签:base64ToMutipartFile return String Base64 imgContent header MutipartFile Overrid

package com.education.common.utils;


import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import java.io.*;

/**
 * @author: liftsail
 * @create: 2022/6/20 9:41
 **/
public class MultipartFileUtils {

    public  static MultipartFile base64MutipartFile(String imgStr){
        try {
            String [] baseStr = imgStr.split(",");
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] b =  new byte[0];
            b = base64Decoder.decodeBuffer(baseStr[1]);
            for(int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new Base64DecodedMultipartFile(b, baseStr[0]);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
}
package com.education.common.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @Title:Base64DecodedMultipartFile
 */
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() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }

    @Override
    public String getOriginalFilename() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];
    }

    @Override
    public String getContentType() {
        // TODO - implementation depends on your requirements
        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 {
        new FileOutputStream(dest).write(imgContent);
    }

}

 

标签:base64ToMutipartFile,return,String,Base64,imgContent,header,MutipartFile,Overrid
From: https://www.cnblogs.com/liftsail/p/17094441.html

相关文章

  • base64文件前端下载
    遇到需要下载cer格式的证书文件时,后台获取base64字符串,然后根据以下的工具类,进行下载。其他格式的文件,修改后缀名即可。varbase64File="";    downloadFileByB......
  • vue.js客服系统实时聊天项目开发(十七)解决url get传参后进行base64解密问题
    有些参数需要在url的GET里传递,但是为了防止特殊字符问题,我转成了base64编码。但是js进行解码的时候,总是报错:报错:Failedtoexecute'atob'on'Window':Thestringto......
  • Base64编码
    介绍Base64编码Base64是一种使用64个可打印字符来表示二进制数据的编码方式。Base64中的64个可打印字符包括:大小写字母a-z、阿拉伯数字0-9,这样共有62个字......
  • Base64编码
    介绍Base64编码Base64是一种使用64个可打印字符来表示二进制数据的编码方式。Base64中的64个可打印字符包括:大小写字母a-z、阿拉伯数字0-9,这样共有62个......
  • 使用base64编码加密解密
    Base64编码简介Base64这个术语最初是在“MIME内容传输编码规范”中提出的。Base64不是一种加密算法,虽然编码后的字符串看起来有点加密的赶脚。它实际上是一种“二进制到文......
  • .net 对接极兔平台 java Base64与.net Base64数据不一致及POST 带header 和body 参数
    近几天对接极兔平台,遇到javaBase64与.netBase64数据不一致及POST带header和body参数的问题,费了一些周折,现在记录下来,供大家参考。1. javaBase64与.netBase64数据......
  • 谷歌邮箱无法显示使用 Base64 处理的图片的解决方法
    前言有时候图片会使用Base64编码来处理,然后再传到前端img标签的src属性里展示,这里记录遇到的一个问题,就是使用谷歌邮箱来打开图片,使用了Base64编码处理的图片是展......
  • 微信小程序 wx.previewImage()(传入base64数据)
    场景应用微信小程序使用wx.previewImage()浏览图片,但是后台传过来的是base64数据。思路先把base64作为临时文件存到本地,然后预览,预览结束时删除临时文件//获取应用实......
  • JS二进制:File、Blob、FileReader、ArrayBuffer、Base64
    JavaScript提供了一些API来处理文件或原始文件数据,例如:File、Blob、FileReader、ArrayBuffer、base64等。  Blob全称为binarylargeobject,即二进制大对象,它是......
  • element上传图片组件使用方法|图片回显|格式转换base64
    upload上传组件的使用方法上传图片后自动上传(也可以手动上传),图片上传成功后由后端返回特定图片地址,在表单提交后将表单数据同图片地址一并返回即可完成图片上传功能。组......