`

HttpClient远程抓取图片到本地

阅读更多
 /**
	 * 获取外网物理文件
	 * @param filePath 存放在数据库中的文件地址,如/upload/test.jpg
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	private void getFiles(String filePath) throws Exception
	{
		try
		{
			File storeFile = new File(request.getSession().getServletContext().getRealPath("")+filePath);
			if (!storeFile.exists())
			{
				HttpClient client = new HttpClient();
				GetMethod get = new GetMethod(Resources.getProperty("WAN_SITE_URL")+filePath);
				client.executeMethod(get);
				storeFile.createNewFile();
				FileOutputStream output = new FileOutputStream(storeFile);   
				//得到网络资源的字节数组,并写入文件   
				output.write(get.getResponseBody());   
				output.close(); 
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			throw new AGPException("WAN_NET_ERROR");
		}
	}

 

使用HttpClient远程抓取网页内容:http://www.cnblogs.com/modou/articles/1325569.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics