不要reader ,writer
InputStream r = url.openStream();
OutputStream w = new FileOutputStream("c:\\temp\\a.html");
InputStream r = url.openStream();
OutputStream w = new FileOutputStream("c:\\temp\\a.html");
调试欢乐多
reader ,writer是字符流方式读写数据,在这过程中会产生一些信息变异,如同VC跟java交互时产生的字符乱码是一样的。
直接通过字节流来操作数据,可以避免这种问题(最多作一次字符集转换操作)
import java.net.*;public class DownloadPage {
public static void main(String[] args) {
DownloadPage dp = new DownloadPage();
dp.download("http://hk.search.yahoo.com/search?p=stupid+yahoo&fl=0&vc=&x=wrt&meta=rst=hk&ei=BIG5&n=10");
} public void download(String searchStr) {
try {
URL url = new URL(searchStr);
// Reader r = new BufferedReader(new InputStreamReader(url.openStream(), "BIG5"));
InputStream r = url.openStream();
OutputStream w = new FileOutputStream("c:\\temp\\a.html");// Writer w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\temp\\a.html"), "UTF-8"));
byte[] buffer = new byte[4096];
int len;
while((len = r.read(buffer)) != -1)
w.write(buffer, 0, len);
r.close();
w.flush();
w.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}結果也是不能顯示中文, 我也嚐試開一個 socket, 用 http 1.1, 也跟足了所有header, cookie, 但也是不能成功, 唉... 謝謝以上各位幫助!