当前页面: 开发资料首页 → JSP 专题 → java读取网页链接,下载的源代码如果遇到中文有些中文是乱码,请参阅如下程序
java读取网页链接,下载的源代码如果遇到中文有些中文是乱码,请参阅如下程序
摘要: java读取网页链接,下载的源代码如果遇到中文有些中文是乱码,请参阅如下程序
/**
* @todo 5) 从链接urlPath读取文件内容,存储到filePath文件中
* @param filePath String
* @param urlPath String
* @return String
*/
public String createHtmlFile(String filePath, String urlPath)
{
try
{
Util.log("urlPath="+urlPath);
URL url = new URL(urlPath);
URLConnection urlConnection = url.openConnection();
urlConnection.setAllowUserInteraction(false);
// InputStream urlStream = url.openStream();
InputStream urlStream = urlConnection.getInputStream();//.openStream();
byte b[] = new byte[1024];
int numRead = urlStream.read(b);
String content = new String(b, 0, numRead);
StringBuffer tempHtml = new StringBuffer(); //
while ( (numRead != -1) && (content.length() < MAXSIZE))
{
numRead = urlStream.read(b);
if (numRead != -1)
{
String newContent = new String(b, 0, numRead);
content += newContent;
}
}
tempHtml = tempHtml.append(content);
FileOperation.writeFromBuffer(filePath, tempHtml);
return content;
}
catch (IOException e)
{
e.printStackTrace();
Util.log("ERROR: couldn't open URL ");
return "";
}
}
为什么不用InputStreamReader去读文件呢,可以用指定编码方式去读取文件,设置读取方式为GBK就可以了
同意,采用如下方法就可以了:
/**
* @todo 5) 从链接urlPath读取文件内容,存储到filePath文件中 中文问题已经解决了
* @param filePath String
* @param urlPath String
* @return String
*/
public String createHtmlFile(String filePath, String urlPath)
{
try
{
Util.log("urlPath="+urlPath);
URL url = new URL(urlPath);
URLConnection urlConnection = url.openConnection();
urlConnection.setAllowUserInteraction(false);
InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
//使用openStream得到一输入流并由此构造一个BufferedReader对象
String inputLine;
String content="";
StringBuffer tempHtml = new StringBuffer(); //
//从输入流不断的读数据,直到读完为止
while ((inputLine = in.readLine()) != null)
{
//content=content+inputLine+"/n";
//tempHtml.append(inputLine+"/n");
tempHtml.append(inputLine+"/n");
}
//Util.log("*******************************************************");
//Util.log(content.toString());
//Util.log("*******************************************************");
//tempHtml = tempHtml.append(content);
FileOperation.writeFromBuffer(filePath, tempHtml);
return content;
}
catch (IOException e)
{
e.printStackTrace();
Util.log("ERROR: couldn't open URL ");
return "";
}
}