---
title: "java网络图片和二进制字符串间的转换"
author: "sailing04"
date: "2012-01-12T14:53:39Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2012-01-12T14:53:39Z"
id: 1343468
url: "https://sailing04.iteye.com/blog/1343468"
---

摘自：[http://763691.iteye.com/blog/802581](http://763691.iteye.com/blog/802581)

01. /\**
02. * @Title           getImgeHexString
03. * @Description     网络图片转换成二进制字符串
04. * @param URLName   网络图片地址
05. * @param type      图片类型
06. * @return  String  转换结果
07. * @throws
08. \*/
09. public static String getImgeHexString(String URLName,String type) {
10. String res = null;
11. try {
12. int HttpResult = 0; // 服务器返回的状态
13. URL url = new URL(URLName); // 创建URL
14. URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
15. urlconn.connect();
16. HttpURLConnection httpconn = (HttpURLConnection) urlconn;
17. HttpResult = httpconn.getResponseCode();
18. System.out.println(HttpResult);
19. if (HttpResult != HttpURLConnection.HTTP\_OK) // 不等于HTTP\_OK则连接不成功
20. System.out.print("fail");
21. else {
22. BufferedInputStream bis = new BufferedInputStream(urlconn.getInputStream());
23. BufferedImage bm = ImageIO.read(bis);
24. ByteArrayOutputStream bos = new ByteArrayOutputStream();
25. ImageIO.write(bm, type, bos);
26. bos.flush();
27. byte\[] data = bos.toByteArray();
28. res = byte2hex(data);
29. bos.close();
30. }
31. } catch (Exception e) {
32. e.printStackTrace();
33. }
34. return res;
35. }
36. /\**
37. * @title           根据二进制字符串生成图片
38. * @param data      生成图片的二进制字符串
39. * @param fileName  图片名称(完整路径)
40. * @param type      图片类型
41. * @return
42. \*/
43. public static void saveImage(String data, String fileName,String type) {
44. BufferedImage image = new BufferedImage(300, 300,BufferedImage.TYPE\_BYTE\_BINARY);
45. ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
46. try {
47. ImageIO.write(image, type, byteOutputStream);
48. // byte\[] date = byteOutputStream.toByteArray();
49. byte\[] bytes = hex2byte(data);
50. System.out.println("path:" + fileName);
51. RandomAccessFile file = new RandomAccessFile(fileName, "rw");
52. file.write(bytes);
53. file.close();
54. } catch (IOException e) {
55. e.printStackTrace();
56. }
57. }
58. /\**
59. * 反格式化byte
60. \*
61. * @param s
62. * @return
63. \*/
64. public static byte\[] hex2byte(String s) {
65. byte\[] src = s.toLowerCase().getBytes();
66. byte\[] ret = new byte\[src.length / 2];
67. for (int i = 0; i &lt; src.length; i += 2) {
68. byte hi = src\[i];
69. byte low = src\[i + 1];
70. hi = (byte) ((hi &gt;= 'a' && hi &lt;= 'f') ? 0x0a + (hi - 'a')
71. : hi - '0');
72. low = (byte) ((low &gt;= 'a' && low &lt;= 'f') ? 0x0a + (low - 'a')
73. : low - '0');
74. ret\[i / 2] = (byte) (hi &lt;&lt; 4 | low);
75. }
76. return ret;
77. }
78. /\**
79. * 格式化byte
80. \*
81. * @param b
82. * @return
83. \*/
84. public static String byte2hex(byte\[] b) {
85. char\[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
86. 'B', 'C', 'D', 'E', 'F' };
87. char\[] out = new char\[b.length * 2];
88. for (int i = 0; i &lt; b.length; i++) {
89. byte c = b\[i];
90. out\[i * 2] = Digit\[(c &gt;&gt;&gt; 4) & 0X0F];
91. out\[i * 2 + 1] = Digit\[c & 0X0F];
92. }
93. return new String(out);
94. }