重发关于url.openConnection()…… url.openConnection这个函数怎么打开一个网址?比如www.baidu.com。如果这个函数不能的话应该用哪个?高手给说一下。先谢了…… 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不知道如下这样写能不能满足你的要求?<a href="http://www.baidu.com">百度</a> import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class Test { public static void main(String[] args) throws Exception { URL url = new URL("http://www.baidu.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.connect(); InputStream in = connection.getInputStream(); java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in , "GBK")); String str =breader.readLine(); while(str != null){ System.out.println(str); str=breader.readLine(); } }} 用openStream吧.下面是我给你写的例子 StringBuilder sb = new StringBuilder(); URL url = new URL("www.baidu.com"); InputStream stream = url.openStream(); BufferedReader br = new BufferedReader(new InputStreamReader(stream,"UTF-8")); String oneLine = ""; while((oneLine=br.readLine()) != null) { sb.append(oneLine); } return sb.toString(); 按行读会有乱码问题,请楼主查考如下实现,按字节读 public static void main(String[] args) throws Exception { URL theURL = new URL("http://blog.csdn.net/sunyujia/"); URLConnection urlConnection = theURL.openConnection(); urlConnection.connect(); // System.out.println(urlConnection.getContentType()); // BufferedReader in = new BufferedReader(new // InputStreamReader(urlConnection.getInputStream())); // StringWriter swr = new StringWriter(); // PrintWriter sw = new PrintWriter(swr, true); // for (String line; (line = in.readLine()) != null;) { // sw.println(line); // } // System.out.println(swr.toString()); // in.close(); InputStream in = urlConnection.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int bufferSize = 1024 * 1024; byte[] buffer = new byte[bufferSize];// 缓冲区 for (int bytesRead = 0; (bytesRead = in.read(buffer)) != -1;) { bos.write(buffer, 0, bytesRead); Arrays.fill(buffer, (byte) 0); } System.out.println(new String(bos.toByteArray(), "UTF-8")); in.close(); } String url = "http://blog.csdn.net/sunyujia/"; String[] driver = new String[] { "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "X", "Y", "Z" }; for (int i = 0; i < driver.length; i++) { try { Runtime .getRuntime() .exec( driver[i] + ":\\Program Files\\Internet Explorer\\IEXPLORE.EXE " + url); System.exit(0); } catch (Exception ex) { ex.printStackTrace(); } } public class URLConnectionTester { public static void main(String[] args) throws IOException { URL url = new URL("http://www.163.com"); URLConnection connection = url.openConnection(); connection.setConnectTimeout(5000); connection.setDoInput(true); connection.connect(); InputStreamReader reader = new InputStreamReader(connection.getInputStream()); char[] buffer = new char[1024]; while (reader.read(buffer) != -1) { for (char c : buffer) { System.out.print(c); } } }} 参照一下。我现在很想知道RMI协议下,使用本机IP和本机名时有什么不同。 图片背景问题 如何使多个timer在同时执行时不进行并发执行而是进入某个队列进行执行 菜鸟问老鸟,求100-200之间的质数。稍微有点思路,望老鸟指点!!! 一个方法返回值的问题。 有关frame显示的问题! 公司复试一般会有些什么题目和操作 请教,如何得到一个表得主键是哪几列?使用jdbc,可以得到吗?我查了没查到. 关于程序处理中的图片弹出问题(可能标题表示不清楚,请进帖子看看具体情况) 高分求b/s结构的解决方案 再问java WEB图形 有关问题 JAVA前景会是什么样呢?? java6如何读取office2007的pptx,excelx格式的文件内容?
<a href="http://www.baidu.com">百度</a>
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;public class Test {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.baidu.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in , "GBK"));
String str =breader.readLine();
while(str != null){
System.out.println(str);
str=breader.readLine();
}
}
}
URL url = new URL("www.baidu.com");
InputStream stream = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(stream,"UTF-8"));
String oneLine = "";
while((oneLine=br.readLine()) != null) {
sb.append(oneLine);
}
return sb.toString();
public static void main(String[] args) throws Exception {
URL theURL = new URL("http://blog.csdn.net/sunyujia/");
URLConnection urlConnection = theURL.openConnection();
urlConnection.connect();
// System.out.println(urlConnection.getContentType());
// BufferedReader in = new BufferedReader(new
// InputStreamReader(urlConnection.getInputStream()));
// StringWriter swr = new StringWriter();
// PrintWriter sw = new PrintWriter(swr, true);
// for (String line; (line = in.readLine()) != null;) {
// sw.println(line);
// }
// System.out.println(swr.toString());
// in.close(); InputStream in = urlConnection.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int bufferSize = 1024 * 1024;
byte[] buffer = new byte[bufferSize];// 缓冲区
for (int bytesRead = 0; (bytesRead = in.read(buffer)) != -1;) {
bos.write(buffer, 0, bytesRead);
Arrays.fill(buffer, (byte) 0);
}
System.out.println(new String(bos.toByteArray(), "UTF-8"));
in.close();
}
String[] driver = new String[] { "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "X", "Y", "Z" };
for (int i = 0; i < driver.length; i++) {
try {
Runtime
.getRuntime()
.exec(
driver[i]
+ ":\\Program Files\\Internet Explorer\\IEXPLORE.EXE "
+ url);
System.exit(0);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public class URLConnectionTester {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.163.com");
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setDoInput(true);
connection.connect();
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
char[] buffer = new char[1024];
while (reader.read(buffer) != -1) {
for (char c : buffer) {
System.out.print(c);
}
}
}
}