码迷,mamicode.com
首页 > 其他好文 > 详细

高鹏工具类

时间:2014-08-22 13:02:58      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:工具类

package com.haier.adThird.util;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GPUtil {
	/**
	 * 读取Stream到磁盘上
	 * 
	 * @param inputStream
	 * @param filePath
	 * @throws Exception
	 */
	public static void readStreamToDisk(InputStream inputStream, String filePath)
			throws Exception {
		byte[] bs = new byte[1024 * 2];
		int len;
		OutputStream os = new FileOutputStream(filePath);
		while ((len = inputStream.read(bs)) != -1) {
			os.write(bs, 0, len);
		}
		os.close();
		inputStream.close();
	}

	/**
	 * 读取URL获取连接
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static HttpURLConnection readURL(String url) throws Exception {
		URL picUrl = new URL(url);
		HttpURLConnection httpURLConnection = (HttpURLConnection) picUrl
				.openConnection();
		httpURLConnection.setConnectTimeout(3000);
		httpURLConnection.setDoInput(true);// 从服务器获取数据
		return httpURLConnection;
	}

	/**
	 * 读取URL获取连接(输入流)
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static InputStream readURL_InputStream(String url) throws Exception {
		HttpURLConnection httpURLConnection = readURL(url);
		return httpURLConnection.getInputStream();
	}

}

高鹏工具类

标签:工具类

原文地址:http://blog.csdn.net/gaopeng0071/article/details/38752881

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