base64编解码文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
* @Author:novy
* @Date:12:05 2022/7/6
* @Version 1.0
**/
public class B64code {
public static void main(String[] args) throws Exception {
//解码还原成this.class
// String str = "base64字符串";
// decoderBase64File(str, "F:\\this.class");



//编码到here.txt
String base64Code = encodeBase64File("F:\\this.class");
String Str2 = base64Code.replaceAll("(\\r\\n|\\n|\\\\n|\\s)", "");
System.out.println(Str2.trim());
try (FileOutputStream fileOutputStream = new FileOutputStream("F:\\this.txt")) {
byte[] bytes = Str2.getBytes();
fileOutputStream.write(bytes);
}
}
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);
}
public static void decoderBase64File(String base64Code,String targetPath) throws Exception {
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
}



声明:
本文章用于学习交流,严禁用于非法操作,出现后果一切自行承担,阅读此文章表示你已同意本声明。

Disclaimer:
This article is for study and communication. It is strictly forbidden to use it for illegal operations. All consequences shall be borne by yourself. Reading this article means that you have agreed to this statement.