package com.envision.hr.portal.center.config; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class Base64Converse { public static void main(String[] args) { try { String str = Base64Converse.encodeBase64File("C:\\Users\\Administrator\\Desktop\\工作目录\\obj_2.amr"); String outUrl ="C:\\Users\\Administrator\\Desktop"; Base64Converse.decoderBase64File(str, outUrl); System.out.println("ok"); } catch (Exception e) { } } public static String encodeBase64File(String path) throws Exception { File file = new File(path);; FileInputStream inputStream = new FileInputStream(file); byte[] buffer = new byte[(int) file.length()]; inputStream.read(buffer); inputStream.close(); return new BASE64Encoder().encode(buffer); } /** * 将base64字符解码保存文件 * @param base64Code * @param targetPath * @throws Exception */ public static void decoderBase64File(String base64Code, String targetPath)throws Exception { byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code); FileOutputStream out = new FileOutputStream(targetPath+"\\444.amr"); out.write(buffer); out.close(); } }
把文件转成BASE64 字符串上传,把Base64字符串转成文件下载
最新推荐文章于 2024-03-18 15:40:43 发布