1. /**  
  2.  * @Title           getImgeHexString  
  3.  * @Description     网络图片转换成二进制字符串  
  4.  * @param URLName   网络图片地址  
  5.  * @param type      图片类型  
  6.  * @return  String  转换结果  
  7.  * @throws  
  8.  */ 
  9. 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.  
  24.             BufferedImage bm = ImageIO.read(bis);  
  25.             ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  26.             ImageIO.write(bm, type, bos);  
  27.             bos.flush();  
  28.             byte[] data = bos.toByteArray();  
  29.  
  30.             res = byte2hex(data);  
  31.             bos.close();  
  32.         }  
  33.     } catch (Exception e) {  
  34.         e.printStackTrace();  
  35.     }  
  36.     return res;  
  37. }  
  38.  
  39. /**  
  40.  * @title           根据二进制字符串生成图片  
  41.  * @param data      生成图片的二进制字符串  
  42.  * @param fileName  图片名称(完整路径)  
  43.  * @param type      图片类型  
  44.  * @return  
  45.  */ 
  46. public static void saveImage(String data, String fileName,String type) {  
  47.  
  48.     BufferedImage p_w_picpath = new BufferedImage(300300,BufferedImage.TYPE_BYTE_BINARY);  
  49.     ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();  
  50.     try {  
  51.         ImageIO.write(p_w_picpath, type, byteOutputStream);  
  52.         // byte[] date = byteOutputStream.toByteArray();  
  53.         byte[] bytes = hex2byte(data);  
  54.         System.out.println("path:" + fileName);  
  55.         RandomAccessFile file = new RandomAccessFile(fileName, "rw");  
  56.         file.write(bytes);  
  57.         file.close();  
  58.     } catch (IOException e) {  
  59.         e.printStackTrace();  
  60.     }  
  61. }  
  62.  
  63. /**  
  64.  * 反格式化byte  
  65.  *   
  66.  * @param s  
  67.  * @return  
  68.  */ 
  69. public static byte[] hex2byte(String s) {  
  70.     byte[] src = s.toLowerCase().getBytes();  
  71.     byte[] ret = new byte[src.length / 2];  
  72.     for (int i = 0; i < src.length; i += 2) {  
  73.         byte hi = src[i];  
  74.         byte low = src[i + 1];  
  75.         hi = (byte) ((hi >= 'a' && hi <= 'f') ? 0x0a + (hi - 'a')  
  76.                 : hi - '0');  
  77.         low = (byte) ((low >= 'a' && low <= 'f') ? 0x0a + (low - 'a')  
  78.                 : low - '0');  
  79.         ret[i / 2] = (byte) (hi << 4 | low);  
  80.     }  
  81.     return ret;  
  82. }  
  83.  
  84. /**  
  85.  * 格式化byte  
  86.  *   
  87.  * @param b  
  88.  * @return  
  89.  */ 
  90. public static String byte2hex(byte[] b) {  
  91.     char[] Digit = { '0''1''2''3''4''5''6''7''8''9''A',  
  92.             'B''C''D''E''F' };  
  93.     char[] out = new char[b.length * 2];  
  94.     for (int i = 0; i < b.length; i++) {  
  95.         byte c = b[i];  
  96.         out[i * 2] = Digit[(c >>> 4) & 0X0F];  
  97.         out[i * 2 + 1] = Digit[c & 0X0F];  
  98.     }  
  99.  
  100.     return new String(out);