Java实现文件和base64流的相互转换功能示例
作者:痕_影 时间:2023-11-18 07:45:01
本文实例讲述了Java实现文件和base64流的相互转换功能。分享给大家供大家参考,具体如下:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 文件与base64的互相转换操作
*/
public class testFile {
public static void main(String[] args) {
testFile t = new testFile();
try {
String ret = t.encodeBase64File("d://IE和火狐js或css差异.docx");
System.err.println(ret);
t.decoderBase64File(ret, "d://ghsTest/retFile.docx", "d://ghsTest/");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 将文件转成base64 字符串
*
* @param path文件路径
* @return *
* @throws Exception
*/
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
}
/**
* 将base64字符解码保存文件
*
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static void decoderBase64File(String base64Code, String targetPath,String catalogue)
throws Exception {
File file = new File(catalogue);
if(file.exists()==false){
file.mkdirs();
}
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
}
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/thghs/article/details/43233199
标签:Java,文件,base64
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Hadoop组件简介
2023-08-20 14:07:00
![](https://img.aspxhome.com/file/2023/5/57725_0s.png)
Java中PriorityQueue实现最小堆和最大堆的用法
2022-03-25 14:32:18
![](https://img.aspxhome.com/file/2023/8/62758_0s.png)
Unity3D实现渐变颜色效果
2022-09-03 08:14:32
![](https://img.aspxhome.com/file/2023/0/116580_0s.jpg)
C#中判断本地系统的网络连接状态的方法
2023-07-02 15:39:41
![](https://img.aspxhome.com/file/2023/7/84457_0s.jpg)
Java之Rsync并发迁移数据并校验详解
2023-07-17 23:01:22
java泛型详解
2023-06-07 23:54:47
Java中Druid连接池连接超时获取不到连接的解决
2022-09-15 04:49:59
![](https://img.aspxhome.com/file/2023/4/63004_0s.png)
Android源码系列之深入理解ImageView的ScaleType属性
2022-12-14 07:51:59
![](https://img.aspxhome.com/file/2023/7/128987_0s.png)
JSON Web Token(JWT)原理入门教程详解
2023-02-05 06:34:43
![](https://img.aspxhome.com/file/2023/3/87223_0s.jpg)
Java多线程编程之ThreadLocal线程范围内的共享变量
2022-03-10 00:41:05
![](https://img.aspxhome.com/file/2023/2/67102_0s.jpg)
Java之操作Redis案例讲解
2023-06-18 03:48:41
![](https://img.aspxhome.com/file/2023/8/61288_0s.png)
Java深入讲解static操作符
2023-11-27 00:59:16
![](https://img.aspxhome.com/file/2023/5/60105_0s.png)
SpringBoot项目实战之加载和读取资源文件
2023-10-07 06:00:41
springBoot接入阿里云oss的实现步骤
2023-05-28 23:09:04
![](https://img.aspxhome.com/file/2023/2/110872_0s.png)
Eclipse项目有红感叹号的解决方法
2023-02-13 10:54:15
Java实现二分查找算法实例分析
2022-06-01 07:30:32
Android CoordinatorLayout高级用法之自定义Behavior
2022-03-20 05:47:51
![](https://img.aspxhome.com/file/2023/3/91673_0s.gif)
Spring Validation方法实现原理分析
2023-09-04 17:11:55
![](https://img.aspxhome.com/file/2023/7/81257_0s.jpg)
Android控件之SlidingDrawer(滑动式抽屉)详解与实例分享
2023-07-27 09:26:18
![](https://img.aspxhome.com/file/2023/2/85982_0s.jpg)
C++实现日期类的示例详解
2022-07-29 04:19:24
![](https://img.aspxhome.com/file/2023/2/104362_0s.png)