程序如下:
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();
}
}

解决方案 »

  1.   

    e:\XunLei 可能是被迅雷软件设置了保护,你可以尝试访问其他普通的文件夹正常么,如果其他的文件夹正常就是 访问权限的问题了 ,应该需要类似超级管理员权限才行,XunLei弄的什么私密空间是可以设置密码的
      

  2.   

    fos=new FileOutputStream("e://myxunlei/");参数不可以是目录,必须是文件名
      

  3.   

    FileInputStream(String name) throws FileNotFoundException 
    的文档中有:
      

  4.   

    URL imageURL = new URL("https://farm7.static.flickr.com/6104/6284324915_c7b1332fc6_o_d.png");String file = imageURL.getFile().replaceAll("^.*/","");File out = new File("E:/myxunlei/",file);
    FileOutputStream outstrem = new FileOutputStream(out);
      

  5.   

    你说的"https://farm7.static.flickr.com/6104/6284324915_c7b1332fc6_o_d.png"
    是什么意思???
    你还用了正则表达式????
    大侠!解释一下
    小弟不懂
      

  6.   

    imageURL 只是一个图片链接的例子。
    getFile 返回除去域名剩下的 “/6104/6284324915_c7b1332fc6_o_d.png”正则是去掉前面的路径,只保留后面的文件名。
      

  7.   

    不想用正则,可以 lastIndexOf("/"), 然后 substring