ImageBase64Converter.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.huimv.process.utils;
  2. import sun.misc.BASE64Decoder;
  3. import sun.misc.BASE64Encoder;
  4. import java.io.*;
  5. /**
  6. * @Project : huimv.shiwan
  7. * @Package : com.huimv.biosafety.uface.controller
  8. * @Description : TODO
  9. * @Version : 1.0
  10. * @Author : ZhuoNing
  11. * @Create : 2020-12-25
  12. **/
  13. public class ImageBase64Converter {
  14. /**
  15. * 本地文件(图片、excel等)转换成Base64字符串
  16. *
  17. * @param imgPath
  18. */
  19. public static String convertFileToBase64(String imgPath) {
  20. byte[] data = null;
  21. // 读取图片字节数组
  22. try {
  23. InputStream in = new FileInputStream(imgPath);
  24. System.out.println("文件大小(字节)="+in.available());
  25. data = new byte[in.available()];
  26. in.read(data);
  27. in.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. // 对字节数组进行Base64编码,得到Base64编码的字符串
  32. BASE64Encoder encoder = new BASE64Encoder();
  33. String base64Str = encoder.encode(data);
  34. return base64Str;
  35. }
  36. /**
  37. * 将base64字符串,生成文件
  38. */
  39. public static File convertBase64ToFile(String fileBase64String, String filePath, String fileName) {
  40. BufferedOutputStream bos = null;
  41. FileOutputStream fos = null;
  42. File file = null;
  43. try {
  44. File dir = new File(filePath);
  45. if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
  46. dir.mkdirs();
  47. }
  48. BASE64Decoder decoder = new BASE64Decoder();
  49. byte[] bfile = decoder.decodeBuffer(fileBase64String);
  50. file = new File(filePath + File.separator + fileName);
  51. fos = new FileOutputStream(file);
  52. bos = new BufferedOutputStream(fos);
  53. bos.write(bfile);
  54. return file;
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. return null;
  58. } finally {
  59. if (bos != null) {
  60. try {
  61. bos.close();
  62. } catch (IOException e1) {
  63. e1.printStackTrace();
  64. }
  65. }
  66. if (fos != null) {
  67. try {
  68. fos.close();
  69. } catch (IOException e1) {
  70. e1.printStackTrace();
  71. }
  72. }
  73. }
  74. }
  75. public static void main(String[] args) {
  76. long start = System.currentTimeMillis();
  77. String imgBase64Str= ImageBase64Converter.convertFileToBase64("D:\\pic\\dream.jpg");
  78. System.out.println("本地图片转换Base64:" + imgBase64Str);
  79. System.out.println("Base64字符串length="+imgBase64Str.length());
  80. ImageBase64Converter.convertBase64ToFile(imgBase64Str,"D:\\pic\\out","test.jpg");
  81. System.out.println("duration:"+(System.currentTimeMillis()-start));
  82. // start=System.currentTimeMillis();
  83. // String fileBase64Str= ImageBase64Converter.convertFileToBase64("D:\\Pictures\\科技\\PayOrderList200109075516581.xlsx");
  84. //// System.out.println("本地excel转换Base64:" + fileBase64Str);
  85. // System.out.println("Base64字符串length="+fileBase64Str.length());
  86. // ImageBase64Converter.convertBase64ToFile(fileBase64Str,"D:\\Pictures\\科技","test.xlsx");
  87. // System.out.println("duration:"+(System.currentTimeMillis()-start));
  88. }
  89. }