飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

java下载网上的图片到本地

时间:2021-11-10  作者:匿名  
public class TestImgJsoup {

	

	/**
	 * @param urlPath
	 *            图片路径
	 * @throws Exception
	 */
	public void getImages(String urlPath, String filePath, String fileName) {
		try {
			URL url = new URL(urlPath);// :获取的路径
			// :http协议连接对象
			HttpURLConnection conn = (HttpURLConnection) 域名Connection();
			域名equestMethod("GET");
			域名eadTimeout(6 * 10000);
			if (域名esponseCode() == 200) {
				InputStream inputStream = 域名nputStream();
				byte[] data = readStream(inputStream);
				if (域名th > (1024 * 10)) {
					FileOutputStream outputStream = new FileOutputStream(
							filePath + fileName);
					域名e(data);
					域名e();
				}
			}

		} catch (Exception e) {
			域名tStackTrace();
			域名tln("下载图片出错" + 域名essage() + "    "
					+ 域名ause());
		}
	}

	/**
	 * 读取url中数据,并以字节的形式返回
	 * 
	 * @param inputStream
	 * @return
	 * @throws Exception
	 */
	public byte[] readStream(InputStream inputStream) throws Exception {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[2048];
		int len = -1;
		while ((len = 域名(buffer)) != -1) {
			域名e(buffer, 0, len);
		}
		域名e();
		域名e();
		return 域名teArray();
	}
	
	
	public static void main(String[] args) {
		
		String urlPath="http://域名/images/域名";
		TestImgJsoup testImgJsoup= new TestImgJsoup();
		域名mages(urlPath, "C:\\\\", "域名");
		//保存图片 
	
	}
}
标签:图片
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。