public class GetByteByNetUrl { /** * 根據(jù)地址獲得數(shù)據(jù)的字節(jié)流 * @param strUrl 網(wǎng)絡(luò)連接地址 * @return */ public static byte[] getImageFromNetByUrl(String strUrl){ try { URL url = new URL(strUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream();//通過輸入流獲取圖片數(shù)據(jù) byte[] btImg = readInputStream(inStream);//得到圖片的二進制數(shù)據(jù) return btImg; } catch (Exception e) { e.PRintStackTrace(); } return null; } /** * 從輸入流中獲取數(shù)據(jù) * @param inStream 輸入流 * @return * @throws Exception */ 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(); }
新聞熱點
疑難解答