码迷,mamicode.com
首页 > Web开发 > 详细

从一个URL下载原始数据,基于byte字节

时间:2015-02-13 16:37:27      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:java

public static byte[] loadRawDataFromURL(String u) throws Exception {
		URL url = new URL(u);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		InputStream is = conn.getInputStream();
		BufferedInputStream bis = new BufferedInputStream(is);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		final int BUFFER_SIZE = 2048;
		final int EOF = -1;

		int c;
		byte[] buf = new byte[BUFFER_SIZE];

		while (true) {
			c = bis.read(buf);
			if (c == EOF)
				break;

			baos.write(buf, 0, c);
		}

		conn.disconnect();
		is.close();

		byte[] data = baos.toByteArray();
		baos.flush();

		return data;
	}

从一个URL下载原始数据,基于byte字节

标签:java

原文地址:http://blog.csdn.net/zhangphil/article/details/43794837

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!