java 日志页面展示 抓取远程的文件

/**

* 抓取远程的文件

* @param url

* @return

* @throws IOException

*/

public static String getLogContent(String url) {

StringBuffer sb = new StringBuffer();

URL HttpUrl;

HttpURLConnection con = null;

BufferedReader reader = null;

try {

HttpUrl = new URL(url);

con = (HttpURLConnection) HttpUrl.openConnection();

if (con.getResponseCode() != con.HTTP_OK) return "";

reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));

String line = null;

while ((line = reader.readLine()) != null) {

sb.append(line);

sb.append("\n");

}

 

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (reader != null)

reader.close();

if (con != null) {

con.disconnect();

}

} catch (Exception e) {

e.printStackTrace();

}

}

return sb.toString();

}

 

你可能感兴趣的:(java)