java 编写ftp下载

露珠111 2011-06-08 12:38:51
下载必须要定义好文件名,如何改为下载的时候,弹出“打开”、“保存”、“取消”的对话框,怎么改呢?请教高手指教。
这是我写的:
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();
...全文
132 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Connie_2012 2012-12-05
  • 打赏
  • 举报
回复
我想问哈 要是想下载FTP目录下的所有文件到本地 本地的文件目录和ftp上的一样 怎么样弄啊
露珠111 2011-06-09
  • 打赏
  • 举报
回复
int r = jfc.showSaveDialog(null);
请问qybao,我运行到这句的时候就不动了,一直卡在那儿,什么原因呢?请指教,谢谢!
qybao 2011-06-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 carolzh125 的回复:]
int r = jfc.showSaveDialog(null);
请问qybao,我运行到这句的时候就不动了,一直卡在那儿,什么原因呢?请指教,谢谢!
[/Quote]
运行这句后会弹出一个对话框选择文件,选择文件后点对话框的ok按钮或者直接cancel按钮,程序就会继续了
qybao 2011-06-08
  • 打赏
  • 举报
回复
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();

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧