跪求高人解答,想从网页直接下载网页上的内容到本地硬盘,写了个java程序,但是有问题~
感谢大家,来帮我看看问题出在哪里?万分感谢import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;public class Download { public void downloadFile(String httpUrl, String filePath) {
try {
URL url = null;
try {
url = new URL(httpUrl);
} catch (Exception e) {
System.out.println("this url is error");
}
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
FileWriter fw = new FileWriter(filePath);
BufferedWriter bw = new BufferedWriter(fw);
String s = null;
while((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
}
bw.close();
fw.close();
} catch (Exception e) {
System.out.println("Error!");
}
}写了个main方法,运行后,报了第2个Exception的"Error!"。
谢谢大家给看看问题出在哪~~
感谢大家,来帮我看看问题出在哪里?万分感谢import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;public class Download { public void downloadFile(String httpUrl, String filePath) {
try {
URL url = null;
try {
url = new URL(httpUrl);
} catch (Exception e) {
System.out.println("this url is error");
}
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
FileWriter fw = new FileWriter(filePath);
BufferedWriter bw = new BufferedWriter(fw);
String s = null;
while((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
}
bw.close();
fw.close();
} catch (Exception e) {
System.out.println("Error!");
}
}写了个main方法,运行后,报了第2个Exception的"Error!"。
谢谢大家给看看问题出在哪~~
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;public class Download { public void downloadFile(String httpUrl, String filePath) {
try {
URL url = null;
try {
url = new URL(httpUrl);
} catch (Exception e) {
System.out.println("this url is error");
}
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
FileWriter fw = new FileWriter(filePath);
BufferedWriter bw = new BufferedWriter(fw);
String s = null;
while((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
}
bw.close();
fw.close();
} catch (Exception e) {
System.out.println("Error!");
}
}
public static void main(String[] args)
{
Download download = new Download();
download.downloadFile("http://www.baidu.com", "c:\\1.txt");
}
}
我用你的代码没有出现异常,你把异常结果输出出来看下
htmlparser这个类我在api里找不到么
我就是想用URL这个类写啊~
FILEPATH = ?
这样写一点信息量都没有.你改成:System.out.println("Error:"+e);
将错误信息详细输出来,不就什么都清楚了
用这个代码调用试试看
downloadFile("http://www.baidu.com","C:/1.html");
方法的两个参数,httpUrl,是我我想要下载的网页的地址,filePath是我要保存在本地硬盘的文件路径。
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.io.LineNumberReader;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
public class Download { public void downloadFile(String httpUrl, String filePath) throws IOException {
LineNumberReader in = null;
PrintWriter pw = null;
try {
URL file;
file = new URL(httpUrl);
in = new LineNumberReader(new BufferedReader(new InputStreamReader(file.openStream())));
String line;
String conent;
File aFile = new File(filePath); pw = new PrintWriter(new BufferedWriter(new FileWriter(aFile)));
while ((line = in.readLine()) != null) {
pw.print(in.getLineNumber()+": ");
pw.println(line);
}
pw.flush(); } catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
pw.close();
}
}
public static void main(String[] args) throws IOException
{
Download download = new Download();
download.downloadFile("http://www.csdn.com", "c:\\1.txt");
}
}