本文介绍了在Java中打开URL以获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找机会在java中打开网址。
I´m searching for a opportunity to open a url in java.
URL url = new URL("http://maps.google.at/maps?saddr=4714&daddr=Marchtrenk&hl=de");
InputStream is = url.openConnection().getInputStream();
BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );
String line = null;
while( ( line = reader.readLine() ) != null ) {
System.out.println(line);
}
reader.close();
我找到了这种方式。
I在我的程序中添加它并发生以下错误。
I added it in my program and the following error occurred.
The method openConnection() is undefined for the type URL
(通过url.openConnection())
(by url.openConnection())
什么是我的问题?
我使用带有servlet的tomcat-server,...
I use a tomcat-server with servlets, ...
推荐答案
public class UrlContent{
public static void main(String[] args) {
URL url;
try {
// get URL content
String a="http://localhost:8080/TestWeb/index.jsp";
url = new URL(a);
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这篇关于在Java中打开URL以获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!