日期:2014-05-20  浏览次数:20788 次

我做的一个类似迅雷的下载程序,但是在写入文件中却报出java.io.FileNotFoundException: e:\myxunlei (拒绝访问。)为什么
程序如下:
package xunlei;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Frmmain implements ActionListener{
JFrame frm;
JLabel ldownload,lsave,lpb;
JScrollPane sptop;
JPanel pnlfoot;
JTextArea ta;
JTextField tfdownload,tfsave;
JButton bdownload;
JProgressBar pb;
public void frm(){
frm=new JFrame("迅雷下载");
ldownload=new JLabel("请输入下载地址:");
lsave=new JLabel("请填入保存地址:");
lpb=new JLabel("进度条__:");
ta=new JTextArea(15,2);
sptop=new JScrollPane(ta);
pnlfoot=new JPanel();
tfdownload=new JTextField();
tfsave=new JTextField();
bdownload=new JButton("下载");
pb=new JProgressBar(0,100);
frm.setLayout(new BorderLayout());
pnlfoot.setLayout(null);
ldownload.setBounds(20, 20, 100, 30);
tfdownload.setBounds(120, 20, 150, 30);
bdownload.setBounds(270, 20, 60, 30);
lsave.setBounds(20, 100, 100, 30);
tfsave.setBounds(120, 100, 150, 30);
lpb.setBounds(20, 150, 100, 30);
pb.setBounds(120, 150, 150, 30);
bdownload.addActionListener(this);
pnlfoot.add(ldownload);
pnlfoot.add(tfdownload);
pnlfoot.add(bdownload);
pnlfoot.add(lsave);
pnlfoot.add(tfsave);
pnlfoot.add(lpb);
pnlfoot.add(pb);
frm.add(sptop,BorderLayout.NORTH);
frm.add(pnlfoot,BorderLayout.CENTER);
frm.setLocation(500,100);
frm.setSize(500, 600);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String surl=tfdownload.getText();
//String path=tfsave.getText();
BufferedInputStream bis=null;
FileOutputStream fos=null;
try{
URL url=new URL(surl);
HttpURLConnection httpConnection=(HttpURLConnection)url.openConnection();
httpConnection.connect();
// String length=httpConnection.getHeaderField("Content-Length");
bis=new BufferedInputStream(httpConnection.getInputStream());
fos=new FileOutputStream("e://myxunlei/");
byte[] b=new byte[1024];
int size=0;
while((size=bis.read(b, 0, 1024))!=-1){
fos.write(b, 0, size);
JOptionPane.showConfirmDialog(null, "下载成功");
}
fos.close();
bis.close();
httpConnection.disconnect();
}catch(Exception e1){
System.out.println(e1);
e1.printStackTrace();
}
}
public static void main(String args[]){
Frmmain fm=new Frmmain();
fm.frm();
}
}


------解决方案--------------------
fos=new FileOutputStream("e://myxunlei/");

参数不可以是目录,必须是文件名
------解决方案--------------------
FileInputStream(String name) throws FileNotFoundException 
的文档中有:
引用 FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.

------解决方案--------------------