apache commons-codec base64将文件转为字符串

来源:互联网 发布:苹果软件商店图标 编辑:程序博客网 时间:2024/06/05 02:46
  1. public String getFileByteString(File file) throws Exception{  
  2.     Base64 b64 = new Base64();  
  3.     FileInputStream fis = new FileInputStream(file);  
  4.     System.out.print(file.length());  
  5.     byte[] buffer = new byte[(int)file.length()];  
  6.     System.out.print(buffer.length);  
  7.     fis.read(buffer);  
  8.     fis.close();  
  9.               
  10.     return b64.encodeToString(buffer);  
  11. }  
  12. /**
  13. * 把读到的文件串转存
  14. */
  15. public void getFileByString(String string, String target) throws Exception{  
  16.     Base64 b64 = new Base64();  
  17.     byte[] buffer = b64.decode(string);  
  18.     FileOutputStream fos = new FileOutputStream(target);  
  19.     fos.write(buffer);  
  20.     fos.close();  
  21. }  
0 0
原创粉丝点击