[java] 图片与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. public static String ImageToBase64ByLocal(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理

  42.  
  43.  
  44. InputStream in = null;

  45. byte[] data = null;

  46.  
  47. // 读取图片字节数组

  48. try {

  49. in = new FileInputStream(imgFile);

  50.  
  51. data = new byte[in.available()];

  52. in.read(data);

  53. in.close();

  54. } catch (IOException e) {

  55. e.printStackTrace();

  56. }

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

  58. BASE64Encoder encoder = new BASE64Encoder();

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

  61. }

  62.  
  63.  
  64.  
  65. /**

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

  67. *

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

  69. * @return

  70. */

  71. public static String ImageToBase64ByOnline(String imgURL) {

  72. ByteArrayOutputStream data = new ByteArrayOutputStream();

  73. try {

  74. // 创建URL

  75. URL url = new URL(imgURL);

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

  77. // 创建链接

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

  79. conn.setRequestMethod("GET");

  80. conn.setConnectTimeout(5000);

  81. InputStream is = conn.getInputStream();

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

  83. int len = -1;

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

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

  86. }

  87. // 关闭流

  88. is.close();

  89. } catch (IOException e) {

  90. e.printStackTrace();

  91. }

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

  93. BASE64Encoder encoder = new BASE64Encoder();

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

  95. }

  96.  
  97.  
  98. /**

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

  100. * @param imgStr base64字符串

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

  102. */

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

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

  106. return false;

  107.  
  108. BASE64Decoder decoder = new BASE64Decoder();

  109. try {

  110. // Base64解码

  111. byte[] b = decoder.decodeBuffer(imgStr);

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

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

  114. b[i] += 256;

  115. }

  116. }

  117.  
  118. OutputStream out = new FileOutputStream(imgFilePath);

  119. out.write(b);

  120. out.flush();

  121. out.close();

  122.  
  123. return true;

  124. } catch (Exception e) {

  125. return false;

  126. }

  127.  
  128. }

  129.  
  130.  
  131. }

 

 

 

其中的

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

  2. return false;

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

 
  1. /**

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

  3. * @param input

  4. * @return

  5. */

  6. public static boolean isEmpty(String input) {

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

  8. }

  9.  
 
  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. */

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

  41.  
  42.  
  43. InputStream in = null;

  44. byte[] data = null;

  45.  
  46. // 读取图片字节数组

  47. try {

  48. in = new FileInputStream(imgFile);

  49.  
  50. data = new byte[in.available()];

  51. in.read(data);

  52. in.close();

  53. } catch (IOException e) {

  54. e.printStackTrace();

  55. }

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

  57. BASE64Encoder encoder = new BASE64Encoder();

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

  60. }

  61.  
  62.  
  63.  
  64. /**

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

  66. *

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

  68. * @return

  69. */

  70. public static String ImageToBase64ByOnline(String imgURL) {

  71. ByteArrayOutputStream data = new ByteArrayOutputStream();

  72. try {

  73. // 创建URL

  74. URL url = new URL(imgURL);

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

  76. // 创建链接

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

  78. conn.setRequestMethod("GET");

  79. conn.setConnectTimeout(5000);

  80. InputStream is = conn.getInputStream();

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

  82. int len = -1;

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

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

  85. }

  86. // 关闭流

  87. is.close();

  88. } catch (IOException e) {

  89. e.printStackTrace();

  90. }

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

  92. BASE64Encoder encoder = new BASE64Encoder();

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

  94. }

  95.  
  96.  
  97. /**

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

  99. * @param imgStr base64字符串

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

  101. * @return

  102. */

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

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

  106. return false;

  107.  
  108. BASE64Decoder decoder = new BASE64Decoder();

  109. try {

  110. // Base64解码

  111. byte[] b = decoder.decodeBuffer(imgStr);

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

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

  114. b[i] += 256;

  115. }

  116. }

  117.  
  118. OutputStream out = new FileOutputStream(imgFilePath);

  119. out.write(b);

  120. out.flush();

  121. out.close();

  122.  
  123. return true;

  124. } catch (Exception e) {

  125. return false;

  126. }

  127.  
  128. }

  129.  
  130.  
  131. }

 

 

其中的

 
  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. }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值