This article describes the mutual conversion function of Java implementing files and base64 streams. Share it for your reference, as follows:
import java.io.FileInputStream;import java.io.FileOutputStream;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/** * The mutual conversion operation between file and base64*/public class testFile {public static void main(String[] args) {testFile t = new testFile();try {String ret = t.encodeBase64File("d://IE and Firefox js or css difference.docx");System.err.println(ret);t.decoderBase64File(ret, "d://ghsTest/retFile.docx", "d://ghsTest/");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * Convert file to base64 string* * @param path file 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); } /** * Decode base64 characters to save the file* * @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(); }}PS: Here are a few more encryption and decryption-related online tools for your reference:
Line encoding conversion tool (utf-8/utf-32/Punycode/Base64):
http://tools.VeVB.COM/transcoding/decode_encode_tool
BASE64 encoding and decoding tools:
http://tools.VeVB.COM/transcoding/base64
Image conversion to Base64 encoding online tool:
http://tools.VeVB.COM/transcoding/img2base64
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools.VeVB.COM/password/hash_md5_sha
For more information about Java related content, please check out the topics of this site: "Summary of Java Mathematical Operation Skills", "Tutorial on Java Data Structures and Algorithms", "Summary of Java Characters and String Operation Skills", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.