问一个简单的问题!
我写了一个下载网页的程序,我运行的时候下载的中文是乱码,是不是因为big-endian和little-endian的问题,我要调整为网页原来的样子应该怎么做,难道要把每个汉字字符都手动调整吗?
import java.net.*;
import java.io.*;
public class SourceViewer2 {
public static void main (String[] args) {
if (args.length > 0) {
try {
URL u = new URL(args[0]);
URLConnection uc = u.openConnection();
InputStream raw = uc.getInputStream();
InputStream buffer = new BufferedInputStream(raw);
Reader r = new InputStreamReader(buffer);
int c;
while ((c = r.read()) != -1) {
System.out.print((char) c);
}
}
catch (MalformedURLException ex) {
System.err.println(args[0] + " is not a parseable URL");
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
}
C:\Documents and Settings\Fantasy\Desktop\temp>javac Test.java
C:\Documents and Settings\Fantasy\Desktop\temp>java Test http://localhost:8090/i
ndex.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>::我顶你个肺-银行管理系统::</title>
<link href="CSS/mycss.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="377" height="34" border="0" align="center" cellpadding="0" cellspa
cing="0">
<tr>
<th class="bigtitle">我顶你个肺-24小时自助银行</th>
</tr>
</table>
<br>
<form name="form1" method="post" action="">
<table width="469" height="84" border="0" align="center" cellpadding="0" cellspa
cing="0">
<tr>
<th width="178" align="right">帐号:</th>
<td width="291">
<input type="text" name="textfield"> </td>
</tr>
<tr>
<th align="right">密码:</th>
<td><input type="text" name="textfield2"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="确定">
<input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
C:\Documents and Settings\Fantasy\Desktop\temp>
------------------------------------为什么我没发现乱码------------
楼主可以在程序中的适当位置加上这样的代码:
(URLConnection)uc.setRequestProporty("Accept-Language","X"),其中X是你想要以何种语言接收文件(各种语言字符的代号,在IE的工具中可以找到),比如说,如果你想将一个阿拉伯文的google站点的首页下载下来,而你又没写上这句(默认的是中文),那收到的就是乱码了。