标签:工具类
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