①1、16进制字符串 转 byte[]
public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); }
2、byte[] 转 图片/xml/ pdf
public static void byteTofile(byte[] data,String path){ if(data.length<3||path.equals("")) return; try{ FileOutputStream imageOutput = new FileOutputStream(new File(path)); imageOutput.write(data, 0, data.length); imageOutput.close(); System.out.println("Make Picture success,Please find image in " + path); } catch(Exception ex) { log.info("byteTofile.Exception():" + ex); ex.printStackTrace(); } }
测试16进制字符串 转 byte[] 转文件
String s1="52abcdef5252"; byte[] stringHex = hexStringToBytes(s1); byteTofile(stringHex, "E:\\wangyc.png");
②3、图片 xml pdf 转byte[]
扫描二维码关注公众号,回复:
1066552 查看本文章
public static byte[] fileTobyte(String path){ byte[] data = null; FileInputStream input = null; try { input = new FileInputStream(new File(path)); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int numBytesRead = 0; while ((numBytesRead = input.read(buf)) != -1) { output.write(buf, 0, numBytesRead); } data = output.toByteArray(); output.close(); input.close(); } catch (FileNotFoundException ex1) { ex1.printStackTrace(); log.info("fileTobyte.FileNotFoundException():"+ex1); } catch (IOException ex1) { ex1.printStackTrace(); log.info("fileTobyte.IOException():"+ex1); } return data; }
4、byte[] 转16进制字符串
public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); }
测试文件 转byte[] 转16进制字符串
byte[] image2byte = fileTobyte("E:\\wangyc.png");
String bytesToHexString = bytesToHexString(image2byte);
System.out.println("bytesToHexString:"+bytesToHexString);
5、③
网络文件 转byte[]
public static byte[] getFileFromNetByUrl(String strUrl){ try { URL url = new URL(strUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream();//通过输入流获取图片数据 byte[] btImg = readInputStream(inStream);//得到图片的二进制数据 return btImg; } catch (Exception e) { e.printStackTrace(); } return null; }
public static byte[] readInputStream(InputStream inStream) throws Exception{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len=inStream.read(buffer)) != -1 ){ outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); }
测试网络文件转byte[] 再转16进制字符串
byte[] image2byte = getFileFromNetByUrl("http://oss-cn-shenzhen.aliyuncs.com/1502694718995.xml");
String bytesToHexString = bytesToHexString(image2byte);
System.out.println("bytesToHexString:"+bytesToHexString);