java 图片与base64之间的互相转换

这篇文章实现的功能是,本地或者线上的图片转换成base64和base64转换成图片。

好了不多说了,直接上代码!

 
  1. import java.io.ByteArrayOutputStream;

  2. import java.io.FileInputStream;

  3. import java.io.FileOutputStream;

  4. import java.io.IOException;

  5. import java.io.InputStream;

  6. import java.io.OutputStream;

  7. import java.net.HttpURLConnection;

  8. import java.net.URL;

  9.  
  10. import com.steward.utils.StringUtil;

  11.  
  12. import sun.misc.BASE64Decoder;

  13. import sun.misc.BASE64Encoder;

  14.  
  15. @SuppressWarnings("restriction")

  16. public class Base64Utils {

  17.  
  18. public static void main(String[] args) throws Exception {

  19.  
  20. //本地图片地址

  21. String url = "C:/Users/Administrator/Desktop/628947887489084892.jpg";

  22. //在线图片地址

  23. String string = "http://bpic.588ku.com//element_origin_min_pic/17/03/03/7bf4480888f35addcf2ce942701c728a.jpg";

  24.  
  25. String str = Base64Utils.ImageToBase64ByLocal(url);

  26.  
  27. String ste = Base64Utils.ImageToBase64ByOnline(string);

  28.  
  29. System.out.println(str);

  30.  
  31. Base64Utils.Base64ToImage(str,"C:/Users/Administrator/Desktop/test1.jpg");

  32.  
  33. Base64Utils.Base64ToImage(ste, "C:/Users/Administrator/Desktop/test2.jpg");

  34. }

  35.  
  36. /**

  37. * 本地图片转换成base64字符串

  38. * @param imgFile 图片本地路径

  39. * @return

  40. *

  41. * @author ZHANGJL

  42. * @dateTime 2018-02-23 14:40:46

  43. */

  44. public static String ImageToBase64ByLocal(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理

  45.  
  46.  
  47. InputStream in = null;

  48. byte[] data = null;

  49.  
  50. // 读取图片字节数组

  51. try {

  52. in = new FileInputStream(imgFile);

  53.  
  54. data = new byte[in.available()];

  55. in.read(data);

  56. in.close();

  57. } catch (IOException e) {

  58. e.printStackTrace();

  59. }

  60. // 对字节数组Base64编码

  61. BASE64Encoder encoder = new BASE64Encoder();

  62.  
  63. return encoder.encode(data);// 返回Base64编码过的字节数组字符串

  64. }

  65.  
  66.  
  67.  
  68. /**

  69. * 在线图片转换成base64字符串

  70. *

  71. * @param imgURL 图片线上路径

  72. * @return

  73. *

  74. * @author ZHANGJL

  75. * @dateTime 2018-02-23 14:43:18

  76. */

  77. public static String ImageToBase64ByOnline(String imgURL) {

  78. ByteArrayOutputStream data = new ByteArrayOutputStream();

  79. try {

  80. // 创建URL

  81. URL url = new URL(imgURL);

  82. byte[] by = new byte[1024];

  83. // 创建链接

  84. HttpURLConnection conn = (HttpURLConnection) url.openConnection();

  85. conn.setRequestMethod("GET");

  86. conn.setConnectTimeout(5000);

  87. InputStream is = conn.getInputStream();

  88. // 将内容读取内存中

  89. int len = -1;

  90. while ((len = is.read(by)) != -1) {

  91. data.write(by, 0, len);

  92. }

  93. // 关闭流

  94. is.close();

  95. } catch (IOException e) {

  96. e.printStackTrace();

  97. }

  98. // 对字节数组Base64编码

  99. BASE64Encoder encoder = new BASE64Encoder();

  100. return encoder.encode(data.toByteArray());

  101. }

  102.  
  103.  
  104. /**

  105. * base64字符串转换成图片

  106. * @param imgStr base64字符串

  107. * @param imgFilePath 图片存放路径

  108. * @return

  109. *

  110. * @author ZHANGJL

  111. * @dateTime 2018-02-23 14:42:17

  112. */

  113. public static boolean Base64ToImage(String imgStr,String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片

  114.  
  115. if (StringUtil.isEmpty(imgStr)) // 图像数据为空

  116. return false;

  117.  
  118. BASE64Decoder decoder = new BASE64Decoder();

  119. try {

  120. // Base64解码

  121.  

    imgStr=imgStr.substring(23);//去掉b
  122. byte[] b = decoder.decodeBuffer(imgStr);

  123. for (int i = 0; i < b.length; ++i) {

  124. if (b[i] < 0) {// 调整异常数据

  125. b[i] += 256;

  126. }

  127. }

  128.  
  129. OutputStream out = new FileOutputStream(imgFilePath);

  130. out.write(b);

  131. out.flush();

  132. out.close();

  133.  
  134. return true;

  135. } catch (Exception e) {

  136. return false;

  137. }

  138.  
  139. }

  140.  
  141.  
  142. }

 

 

其中的

 
  1. if (StringUtil.isEmpty(imgStr)) // 图像数据为空

  2. return false;

只是用来判断传入的数据是否为空,里面的判断也简单    

 
  1. /**

  2. * 验证字符串是否为空

  3. *

  4. * @param input

  5. * @return

  6. */

  7. public static boolean isEmpty(String input) {

  8. return input == null || input.equals("") || input.matches(EMPTY_REGEX);

  9. }


 

好了,具体的步骤就这样了!! 希望能对你有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值