JAVA文件转换为Base64

博主发表的文章,有的是自己原创,有的是这些年本人从网上积累的,方便大家学习。 2016-12-13 13:50:06 JAVA转换Base64 easyui open遮罩层盖住上传框 博主发表的文章,有的是自己原创,有的是这些年本人从网上积累的,方便大家学习。

博主发表的文章,有的是自己原创,有的是这些年本人从网上积累的,方便大家学习。

[[178773]]

  1. importjava.io.File;
  2. importjava.io.FileInputStream;
  3. importjava.io.FileOutputStream;
  4. importsun.misc.BASE64Decoder;
  5. importsun.misc.BASE64Encoder;
  6. publicclassFileToBase64{
  7. /**
  8. *<p>将文件转成base64字符串</p>
  9. *@parampath文件路径
  10. *@return
  11. *@throwsException
  12. */
  13. publicstaticStringencodeBase64File(Stringpath)throwsException{
  14. Filefile=newFile(path);
  15. FileInputStreaminputFile=newFileInputStream(file);
  16. byte[]buffer=newbyte[(int)file.length()];
  17. inputFile.read(buffer);
  18. inputFile.close();
  19. returnnewBASE64Encoder().encode(buffer);
  20. }
  21. /**
  22. *<p>将base64字符解码保存文件</p>
  23. *@parambase64Code
  24. *@paramtargetPath
  25. *@throwsException
  26. */
  27. publicstaticvoiddecoderBase64File(Stringbase64Code,StringtargetPath)throwsException{
  28. byte[]buffer=newBASE64Decoder().decodeBuffer(base64Code);
  29. FileOutputStreamout=newFileOutputStream(targetPath);
  30. out.write(buffer);
  31. out.close();
  32. }
  33. /**
  34. *<p>将base64字符保存文本文件</p>
  35. *@parambase64Code
  36. *@paramtargetPath
  37. *@throwsException
  38. */
  39. publicstaticvoidtoFile(Stringbase64Code,StringtargetPath)throwsException{
  40. byte[]buffer=base64Code.getBytes();
  41. FileOutputStreamout=newFileOutputStream(targetPath);
  42. out.write(buffer);
  43. out.close();
  44. }
  45. publicstaticvoidmain(String[]args){
  46. try{
  47. Stringbase64Code=encodeBase64File("/Users/Crazy/Pictures/zyb2.jpg");
  48. System.out.println(base64Code);
  49. decoderBase64File(base64Code,"/Users/Crazy/Desktop/zyb.png");
  50. toFile(base64Code,"/Users/Crazy/Desktop/zyb.txt");
  51. }catch(Exceptione){
  52. e.printStackTrace();
  53. }
  54. }
  55. }

【本文是清一色专栏作者张勇波的原创文章,转载请通过清一色获取作者授权】

©本文为清一色官方代发,观点仅代表作者本人,与清一色无关。清一色对文中陈述、观点判断保持中立,不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证。本文不作为投资理财建议,请读者仅作参考,并请自行承担全部责任。文中部分文字/图片/视频/音频等来源于网络,如侵犯到著作权人的权利,请与我们联系(微信/QQ:1074760229)。转载请注明出处:清一色财经

(0)
打赏 微信扫码打赏 微信扫码打赏 支付宝扫码打赏 支付宝扫码打赏
清一色的头像清一色管理团队
上一篇 2023年5月4日 20:20
下一篇 2023年5月4日 20:20

相关推荐

发表评论

登录后才能评论

联系我们

在线咨询:1643011589-QQbutton

手机:13798586780

QQ/微信:1074760229

QQ群:551893940

工作时间:工作日9:00-18:00,节假日休息

关注微信