日期:2014-05-17  浏览次数:20952 次

java 编写ftp下载
下载必须要定义好文件名,如何改为下载的时候,弹出“打开”、“保存”、“取消”的对话框,怎么改呢?请教高手指教。
这是我写的:
           ftpClient = new FtpClient();
  ftpClient.openServer(server); 
            ftpClient.login(user, password);           
            ftpClient.binary();
            TelnetInputStream is=ftpClient.get(filename);
         
            File file_out=new File("d:/1.doc");//输出文件名,要改为自定义,怎么改呢?            
            FileOutputStream os=new FileOutputStream(file_out);
            byte[] bytes=new byte[1024];
            int c;
            while ((c=is.read(bytes))!=-1) {
            os.write(bytes,0,c);
            }
            is.close();
            os.close();
            ftpClient.closeServer();

------最佳解决方案--------------------
import javax.swing.*;

ftpClient = new FtpClient();
ftpClient.openServer(server);  
  ftpClient.login(user, password);   
  ftpClient.binary();
  TelnetInputStream is=ftpClient.get(filename);
  
JFileChooser jfc = new JFileChooser("d:\\");
int r = jfc.showSaveDialog(null);
File file = null;
if (r == JFileChooser.APPROVE_OPTION) {
    file = jfc.getSelectedFile();
} else {
    return;
}
  //File file_out=new File("d:/1.doc");//输出文件名,要改为自定义,怎么改呢?   
  FileOutputStream os=new FileOutputStream(file_out);
  byte[] bytes=new byte[1024];
  int c;
  while ((c=is.read(bytes))!=-1) {
  os.write(bytes,0,c);
  }
  is.close();
  os.close();
  ftpClient.closeServer();

------其他解决方案--------------------
int r = jfc.showSaveDialog(null);
请问qybao,我运行到这句的时候就不动了,一直卡在那儿,什么原因呢?请指教,谢谢!

------其他解决方案--------------------
引用:
int r = jfc.showSaveDialog(null);
请问qybao,我运行到这句的时候就不动了,一直卡在那儿,什么原因呢?请指教,谢谢!

运行这句后会弹出一个对话框选择文件,选择文件后点对话框的ok按钮或者直接cancel按钮,程序就会继续了
------其他解决方案--------------------
我想问哈   要是想下载FTP目录下的所有文件到本地  本地的文件目录和ftp上的一样  怎么样弄啊