http://zhidao.baidu.com/question/78956126.html
通过一个网址 读取网站的源代码
可是上面的代码会乱码
这样写就不会了
InputStreamReader inp =new InputStreamReader(url.openStream(),"UTF-8");
reader = new BufferedReader(inp);
package com;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import javax.swing.JTextArea;
public class Newdownurl {
static BufferedReader reader;
static JTextArea tPage;
static URL url;
static String wenben;
public static void main(String[] args) {
tPage = new JTextArea();
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
String datetime = tempDate.format(new java.util.Date());
System.out.println(datetime);
String url = "http://test.tz2100.cn:8000/"; //网站路径
wenben="d:/tomcat_demo/webapps/cms_test///test/"+datetime+".shtml"; //存储路径
readPage(url);
}
public static void readPage(String uu) {
String line;
try {
url = new URL(uu);
InputStreamReader inp =new InputStreamReader(url.openStream(),"UTF-8");
reader = new BufferedReader(inp);
while ((line = reader.readLine()) != null) {
tPage.append(line + "\n");
}
cunchu();
System.out.println("存储成功11");
} catch (Exception ie) {
tPage.setText("发生输入输出异常 22");
} finally {
try {
if (reader != null)
reader.close();
} catch (Exception e) {
}
}
}
public static void cunchu() {
File f = new File(wenben);
try {
f.createNewFile();
FileWriter out = new FileWriter(f);
out.write(tPage.getText());
out.flush(); // 清空管道
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}