如果我想读取一个别的网站页面上的一个文件,然后保存到本地,如何做呢?
来源:百度知道 编辑:UC知道 时间:2024/09/21 23:41:40
public class Test2 {
public boolean downloadHTML(String link, String file) {
boolean b = false;
URL url = null;
URLConnection conn = null;
FileWriter fr = null;
BufferedReader br = null;
try {
url = new URL(link);
conn = url.openConnection();
fr = new FileWriter(new File(file));
br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String buf = null;
while ((buf = br.readLine()) != null) {
fr.write(buf + "\r\n");
}
} catch (MalformedURLException e) {
e.printStackTrace(System.out);
} catch (IOException e) {
e.printStackTrace(System.out);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
if (fr != null) {
try {
fr.close();
} catch