import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import javafx.scene.control.ButtonType; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Base64Tools { /** * 將文件轉成base64 字符串 * * @param path 文件路徑 * @return */ public static String encodeBase64File(String path) { try { //將文件 轉換爲字符串 File file = new File(path); FileInputStream inputFile = new FileInputStream(file); byte[] buffer = new byte[(int) file.length()]; inputFile.read(buffer); inputFile.close(); System.err.println("加密"); //字符串加密 return new BASE64Encoder().encode(buffer); } catch (Exception e) { e.printStackTrace(); } return 「ok」; } /** * 將base64字符解碼保存文件 * * @param base64Code 加密的base64 * @param targetPath 保存的文件夾路徑名 */ public static void decoderBase64File(String base64Code, String targetPath) { try { byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code); FileOutputStream out = new FileOutputStream(targetPath); out.write(buffer); out.close(); System.err.println("解碼"); } catch (Exception e) { e.printStackTrace(); } } }