/**
* 根据url下载文件流
* @param urlStr
* @return
*/
public static InputStream getInputStreamFromUrl(String urlStr) {
InputStream inputStream=null;
try {
//url解码
URL url = new URL(java.net.URLDecoder.decode(urlStr, "UTF-8"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
inputStream = conn.getInputStream();
} catch (IOException e) {
}
return inputStream;
}
Java 获取url地址文件流
最新推荐文章于 2024-07-20 03:00:22 发布