Java 实现文件和base64字符串互转
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java 实现文件和base64字符串互转,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2889字,纯文字阅读大概需要5分钟。
内容图文
![Java 实现文件和base64字符串互转](/upload/InfoBanner/zyjiaocheng/647/c223731cf69e4119a81c736433208bd3.jpg)
项目中遇到需要将图片转成base64编码的字符串的需求,但是,考虑到扩展性,写了一个可以转换任务类型文件的方法。需要引入的包:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.13</version> </dependency>
源码如下:
import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; ? ? import java.io.*; ? ? public class Base64FileUtil { ? ? ????private static String targetFilePath = "E:\\base2Img\\target\\test.txt"; ? ? ????public static void main(String[] args) throws Exception { ????????String fileStr = getFileStr("E:\\base2Img\\big test.txt"); ????????System.out.println("fileStr ===" + fileStr); ????????System.out.println(generateFile(fileStr, targetFilePath)); ????????System.out.println("end"); ????} ? ? ????/** ?????* 文件转化成base64字符串 ?????* 将文件转化为字节数组字符串,并对其进行Base64编码处理 ?????*/ ????public static String getFileStr(String filePath) { ????????InputStream in = null; ????????byte[] data = null; ????????// 读取文件字节数组 ????????try { ????????????in = new FileInputStream(filePath); ????????????data = new byte[in.available()]; ????????????in.read(data); ????????????in.close(); ????????} catch (IOException e) { ????????????e.printStackTrace(); ????????} finally { ????????????try { ????????????????in.close(); ????????????} catch (IOException e) { ????????????????e.printStackTrace(); ????????????} ????????} ????????// 对字节数组Base64编码 ????????BASE64Encoder encoder = new BASE64Encoder(); ????????// 返回 Base64 编码过的字节数组字符串 ????????return encoder.encode(data); ????} ? ? ????/** ?????* base64字符串转化成文件,可以是JPEG、PNG、TXT和AVI等等 ?????* ?????* @param base64FileStr ?????* @param filePath ?????* @return ?????* @throws Exception ?????*/ ????public static boolean generateFile(String base64FileStr, String filePath) throws Exception { ????????// 数据为空 ????????if (base64FileStr == null) { ????????????System.out.println(" 不行,oops! "); ????????????return false; ????????} ????????BASE64Decoder decoder = new BASE64Decoder(); ? ? ????????// Base64解码,对字节数组字符串进行Base64解码并生成文件 ????????byte[] byt = decoder.decodeBuffer(base64FileStr); ????????for (int i = 0, len = byt.length; i < len; ++i) { ????????????// 调整异常数据 ????????????if (byt[i] < 0) { ????????????????byt[i] += 256; ????????????} ????????} ????????OutputStream out = null; ????????InputStream input = new ByteArrayInputStream(byt); ????????try { ????????????// 生成指定格式的文件 ????????????out = new FileOutputStream(filePath); ????????????byte[] buff = new byte[1024]; ????????????int len = 0; ????????????while ((len = input.read(buff)) != -1) { ????????????????out.write(buff, 0, len); ????????????} ????????} catch (IOException e) { ????????????e.printStackTrace(); ????????} finally { ????????????out.flush(); ????????????out.close(); ????????} ????????return true; ????} ? }内容总结
以上是互联网集市为您收集整理的Java 实现文件和base64字符串互转全部内容,希望文章能够帮你解决Java 实现文件和base64字符串互转所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。