[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. import com.steward.utils.StringUtil;
  10. import sun.misc.BASE64Decoder;
  11. import sun.misc.BASE64Encoder;
  12. @SuppressWarnings( "restriction")
  13. public class Base64Utils {
  14. public static void main(String[] args) throws Exception {
  15. //本地图片地址
  16. String url = "C:/Users/Administrator/Desktop/628947887489084892.jpg";
  17. //在线图片地址
  18. String string = "http://bpic.588ku.com//element_origin_min_pic/17/03/03/7bf4480888f35addcf2ce942701c728a.jpg";
  19. String str = Base64Utils.ImageToBase64ByLocal(url);
  20. String ste = Base64Utils.ImageToBase64ByOnline(string);
  21. System.out.println(str);
  22. Base64Utils.Base64ToImage(str, "C:/Users/Administrator/Desktop/test1.jpg");
  23. Base64Utils.Base64ToImage(ste, "C:/Users/Administrator/Desktop/test2.jpg");
  24. }
  25. /**
  26. * 本地图片转换成base64字符串
  27. * @param imgFile 图片本地路径
  28. * @return
  29. *
  30. * @author ZHANGJL
  31. * @dateTime 2018-02-23 14:40:46
  32. */
  33. public static String ImageToBase64ByLocal(String imgFile) { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
  34. InputStream in = null;
  35. byte[] data = null;
  36. // 读取图片字节数组
  37. try {
  38. in = new FileInputStream(imgFile);
  39. data = new byte[in.available()];
  40. in.read(data);
  41. in.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. // 对字节数组Base64编码
  46. BASE64Encoder encoder = new BASE64Encoder();
  47. return encoder.encode(data); // 返回Base64编码过的字节数组字符串
  48. }
  49. /**
  50. * 在线图片转换成base64字符串
  51. *
  52. * @param imgURL 图片线上路径
  53. * @return
  54. *
  55. * @author ZHANGJL
  56. * @dateTime 2018-02-23 14:43:18
  57. */
  58. public static String ImageToBase64ByOnline(String imgURL) {
  59. ByteArrayOutputStream data = new ByteArrayOutputStream();
  60. try {
  61. // 创建URL
  62. URL url = new URL(imgURL);
  63. byte[] by = new byte[ 1024];
  64. // 创建链接
  65. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  66. conn.setRequestMethod( "GET");
  67. conn.setConnectTimeout( 5000);
  68. InputStream is = conn.getInputStream();
  69. // 将内容读取内存中
  70. int len = - 1;
  71. while ((len = is.read(by)) != - 1) {
  72. data.write(by, 0, len);
  73. }
  74. // 关闭流
  75. is.close();
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. }
  79. // 对字节数组Base64编码
  80. BASE64Encoder encoder = new BASE64Encoder();
  81. return encoder.encode(data.toByteArray());
  82. }
  83. /**
  84. * base64字符串转换成图片
  85. * @param imgStr base64字符串
  86. * @param imgFilePath 图片存放路径
  87. * @return
  88. *
  89. * @author ZHANGJL
  90. * @dateTime 2018-02-23 14:42:17
  91. */
  92. public static boolean Base64ToImage(String imgStr,String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
  93. if (StringUtil.isEmpty(imgStr)) // 图像数据为空
  94. return false;
  95. BASE64Decoder decoder = new BASE64Decoder();
  96. try {
  97. // Base64解码
  98. byte[] b = decoder.decodeBuffer(imgStr);
  99. for ( int i = 0; i < b.length; ++i) {
  100. if (b[i] < 0) { // 调整异常数据
  101. b[i] += 256;
  102. }
  103. }
  104. OutputStream out = new FileOutputStream(imgFilePath);
  105. out.write(b);
  106. out.flush();
  107. out.close();
  108. return true;
  109. } catch (Exception e) {
  110. return false;
  111. }
  112. }
  113. }


其中的

  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. }
  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. import com.steward.utils.StringUtil;
  10. import sun.misc.BASE64Decoder;
  11. import sun.misc.BASE64Encoder;
  12. @SuppressWarnings( "restriction")
  13. public class Base64Utils {
  14. public static void main(String[] args) throws Exception {
  15. //本地图片地址
  16. String url = "C:/Users/Administrator/Desktop/628947887489084892.jpg";
  17. //在线图片地址
  18. String string = "http://bpic.588ku.com//element_origin_min_pic/17/03/03/7bf4480888f35addcf2ce942701c728a.jpg";
  19. String str = Base64Utils.ImageToBase64ByLocal(url);
  20. String ste = Base64Utils.ImageToBase64ByOnline(string);
  21. System.out.println(str);
  22. Base64Utils.Base64ToImage(str, "C:/Users/Administrator/Desktop/test1.jpg");
  23. Base64Utils.Base64ToImage(ste, "C:/Users/Administrator/Desktop/test2.jpg");
  24. }
  25. /**
  26. * 本地图片转换成base64字符串
  27. * @param imgFile 图片本地路径
  28. * @return
  29. *
  30. * @author ZHANGJL
  31. * @dateTime 2018-02-23 14:40:46
  32. */
  33. public static String ImageToBase64ByLocal(String imgFile) { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
  34. InputStream in = null;
  35. byte[] data = null;
  36. // 读取图片字节数组
  37. try {
  38. in = new FileInputStream(imgFile);
  39. data = new byte[in.available()];
  40. in.read(data);
  41. in.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. // 对字节数组Base64编码
  46. BASE64Encoder encoder = new BASE64Encoder();
  47. return encoder.encode(data); // 返回Base64编码过的字节数组字符串
  48. }
  49. /**
  50. * 在线图片转换成base64字符串
  51. *
  52. * @param imgURL 图片线上路径
  53. * @return
  54. *
  55. * @author ZHANGJL
  56. * @dateTime 2018-02-23 14:43:18
  57. */
  58. public static String ImageToBase64ByOnline(String imgURL) {
  59. ByteArrayOutputStream data = new ByteArrayOutputStream();
  60. try {
  61. // 创建URL
  62. URL url = new URL(imgURL);
  63. byte[] by = new byte[ 1024];
  64. // 创建链接
  65. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  66. conn.setRequestMethod( "GET");
  67. conn.setConnectTimeout( 5000);
  68. InputStream is = conn.getInputStream();
  69. // 将内容读取内存中
  70. int len = - 1;
  71. while ((len = is.read(by)) != - 1) {
  72. data.write(by, 0, len);
  73. }
  74. // 关闭流
  75. is.close();
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. }
  79. // 对字节数组Base64编码
  80. BASE64Encoder encoder = new BASE64Encoder();
  81. return encoder.encode(data.toByteArray());
  82. }
  83. /**
  84. * base64字符串转换成图片
  85. * @param imgStr base64字符串
  86. * @param imgFilePath 图片存放路径
  87. * @return
  88. *
  89. * @author ZHANGJL
  90. * @dateTime 2018-02-23 14:42:17
  91. */
  92. public static boolean Base64ToImage(String imgStr,String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
  93. if (StringUtil.isEmpty(imgStr)) // 图像数据为空
  94. return false;
  95. BASE64Decoder decoder = new BASE64Decoder();
  96. try {
  97. // Base64解码
  98. byte[] b = decoder.decodeBuffer(imgStr);
  99. for ( int i = 0; i < b.length; ++i) {
  100. if (b[i] < 0) { // 调整异常数据
  101. b[i] += 256;
  102. }
  103. }
  104. OutputStream out = new FileOutputStream(imgFilePath);
  105. out.write(b);
  106. out.flush();
  107. out.close();
  108. return true;
  109. } catch (Exception e) {
  110. return false;
  111. }
  112. }
  113. }


其中的

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

猜你喜欢

转载自blog.csdn.net/weixin_39041673/article/details/80896917
今日推荐