`
jueyue
  • 浏览: 193770 次
社区版块
存档分类
最新评论

地图转发使用代理访问地图服务器

 
阅读更多

好久没有写博客,今天凑机会写一个,这个是公司的需求,因为大部分业务都是内网访问的,而且服务器也基本上是不能连接外网的,但是我们的实时交通这个快大家也都是知道的,没有外网是不可能做到的,所以我们想了一个折中的办法,找一台代理服务器来代理,只要进行转发就可以了,话不多说上代码

	// 处理GET请求
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("----------------来请求了一次------------");
		String level = request.getParameter("level");
		String x = request.getParameter("x");
		String y = request.getParameter("y");
		String cache = request.getParameter("cache");
		ImageInputStream insr = getRealTimeTraffic(level,x,y,cache);
		BufferedOutputStream  out = new BufferedOutputStream(response.getOutputStream());
		response.setContentType("png");   
		byte c[] = new byte[1024];
		int len = 0;
		while((len = insr.read(c))>0){
			out.write(c, 0, len);


		}
		out.flush();
		out.close();
		System.gc();
	}


/**
	 * 从外网获取数据流
	 * @param level
	 * @param x
	 * @param y
	 * @param cache
	 * @return
	 */
	private ImageInputStream getRealTimeTraffic(String level,String x,String y,String cache){
		String url = String
				.format("****网站?v=1.0" +
						"&t=1&zoom=%s&x=%s&y=%s&cache=%s",level,x,y,cache);
		URL myURL = null;
		URLConnection httpsConn = null;
		try {
			myURL = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			httpsConn = (URLConnection) myURL.openConnection();
			if (httpsConn != null) {
				ImageInputStream insr =  ImageIO.createImageInputStream(
						httpsConn.getInputStream());
				return insr;
			}
		}catch (IOException e) {
				e.printStackTrace();
		}
		return null;
	}

 这样就可以在一定范围解决问题了

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics