Base64编码文件转换

编程入门 行业动态 更新时间:2024-10-06 18:32:08

Base64编码<a href=https://www.elefans.com/category/jswz/34/1709460.html style=文件转换"/>

Base64编码文件转换

Base64编码中的 64表示的为 0-16 A-Z a-z 和+ / 一共64个字符。

将数据转为二进制数、再将六位作为一组转为十进制数、对应64个字符中进行拼接

1.将文件转为base64编码字符串

public class TestBase64 {public static void main(String[] args) throws Exception {// 读取文件字节数组InputStream in = new FileInputStream("C:\\Users\\Administrator\\Desktop\\扩展系统字典.png");byte[] data = new byte[in.available()];in.read(data);in.close();// 对字节数组Base64编码BASE64Encoder encoder = new BASE64Encoder();// 返回 Base64 编码过的字节数组字符串String base64Str = encoder.encode(data);System.out.println(base64Str);}
}

2.将base64字符串转为文件

  public static void main(String[] args) throws Exception {BASE64Decoder decoder = new BASE64Decoder();// Base64解码,对字节数组字符串进行Base64解码并生成文件byte[] byt = decoder.decodeBuffer(str);for (int i = 0, len = byt.length; i < len; ++i) {// 调整异常数据if (byt[i] < 0) {byt[i] += 256;}}InputStream input = new ByteArrayInputStream(byt);// 生成指定格式的文件OutputStream out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\扩展系统字典1.png");byte[] buff = new byte[1024];int len = 0;while ((len = input.read(buff)) != -1) {out.write(buff, 0, len);}out.flush();out.close();}

3.将base64字符串转为MultipartFile文件

3.1 字节转换文件工具类

public class Base64File implements MultipartFile {private final byte[] imgContent;private final String header;private String fileName;public Base64File(byte[] imgContent, String header,String fileName) {this.imgContent = imgContent;this.header = header.split(";")[0];this.fileName = fileName;}@Overridepublic String getName() {return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];}@Overridepublic String getOriginalFilename() {if (StringUtils.isNoneBlank(fileName)){return fileName;}return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];}@Overridepublic String getContentType() {return header.split(":")[1];}@Overridepublic boolean isEmpty() {return imgContent == null || imgContent.length == 0;}@Overridepublic long getSize() {return imgContent.length;}@Overridepublic byte[] getBytes() throws IOException {return imgContent;}@Overridepublic InputStream getInputStream() throws IOException {return new ByteArrayInputStream(imgContent);}@Overridepublic void transferTo(File dest) throws IOException, IllegalStateException {new FileOutputStream(dest).write(imgContent);}public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}public static MultipartFile base64ToMultipart(String base64,String fileName) {try {String[] baseStrs = base64.split(",");BASE64Decoder decoder = new BASE64Decoder();byte[] b = new byte[0];b = decoder.decodeBuffer(baseStrs[1]);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {b[i] += 256;}}return new Base64File(b, baseStrs[0],fileName);} catch (IOException e) {e.printStackTrace();return null;}}}

3.2 调用文件转换

            MultipartFile file = Base64File.base64ToMultipart((String)datasMap.get("reportFile"),"自定义文件名");

 

 

更多推荐

Base64编码文件转换

本文发布于:2024-03-07 17:47:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1718489.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件转换

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!