java 读取url链接地址的内容

准备内容index.jsp
写道
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<script type="text/javascript" src="${ pageContext.request.contextPath}/jslib/jq/jquery1.6.1.js"></script>
<script type="text/javascript" src="${ pageContext.request.contextPath}/jslib/json.js"></script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试路径</title>
</head>
<body>

测试使用 呵呵
哈哈
</body>
</html>

 主要Java代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

 


public class URLTest {
	
	public static void main(String[] args) {
		String infourl = http://localhost:8081/mytest;
		System.setProperty("sun.net.client.defaultConnectTimeout", "60000");
		System.setProperty("sun.net.client.defaultReadTimeout", "60000");
		
		StringBuffer content = new StringBuffer(200);
		BufferedReader reader = null;
		URLConnection connection = null;
		try {
			URL url = new URL(infourl);
			connection = url.openConnection();
			InputStream inputstream = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(inputstream,
					"utf-8"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				content.append(line);
				System.out.println(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
				 
					e.printStackTrace();
				}
			}
		}
		//System.out.println(content.toString());
		//return content.toString();
	}

}

 

 

你可能感兴趣的:(java)