
【grant999】:
源码有吗
【xuedouer521】:
FtpClient ftp = new FtpClient(host);
ftp.login(username, password);
ftp.binary();
ftp.cd(path);
while((s=dr.readLine())!=null) {
System.out.println(a(s));
getfile = ftp.get(a(s));
str = ftp.getResponseString ();
System.out.println(str);
FileOutputStream output = new FileOutputStream (new File (a(s)));
while( true )
{
int y = getfile.read();
if( y == -1 ) break;
else
{
output.write ((byte)y);
output.flush();
}
}
getfile.close();
output.close ();
}
public static String a(String s) {
String s1="";
StringTokenizer st=new StringTokenizer(s," ");
s1=st.nextToken();
while(st.hasMoreTokens())
{ s=st.nextToken();
}
if (s1.indexOf("-")!=-1)
s2=s ;
else
s2 = null;
return s1.indexOf("-")==-1?"Folder:"+s:"File:"+s;
}
【xuedouer521】:
z这个代码是一个读文件名 再下载的过程
【myydzhz】:
String server=Server;
String user=User;
String password=Pass;
String path=FtpPath;
try {
FtpClient ftpClient = new FtpClient();
//创建FtpClient对象
ftpClient.openServer(server,this.Port);
//连接FTP服务器
ftpClient.login(user, password);
//登录FTP服务器
if (path.length() != 0) ftpClient.cd(path);
//下载一个文件
ftpClient.binary();
TelnetInputStream is = ftpClient.get(FileName);
File file_out = new File(SavePath+FileName);
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(); //退出FTP服务器
} catch (Exception ex) {
}
【xuedouer521】:
谢谢楼上的 我已经实现下载了
我是希望能把文件下载到本地计算机指定的目录中去 比如下到“D:/”
楼上的和我自己的代码都只能够实现把文件下载到我所建立的这个JAVA项目中的文件夹里 我不想让他那么死 想灵活一些 让他下载入我指定的文件 如何实现呢?
【myydzhz】:
晕,楼主仔细看看SavePath+FileName,就是你指定的目录呀
【xuedouer521】:
运行下来还是下载到了项目目录 没有下到目标文件夹~
getfile = ftp.get(filename3);
str = ftp.getResponseString ();
System.out.println(str);
File file_out = new File("D:/"+filename3);
FileOutputStream os = new FileOutputStream(file_out);
// FileOutputStream output = new FileOutputStream (new File (filename3));
while( true )
{
int y = getfile.read();
if( y == -1 ) break;
else
{
os.write ((byte)y);
os.flush();
//output.flush();
}
}
getfile.close();
os.close ();
楼上帮我看下 我哪里写错了么?
【xuedouer521】:
搞定了 谢谢哦 ~
◆◆
评论读取中....
请登录后再发表评论!
◆◆
修改失败,请稍后尝试
这篇博客讨论了如何使用Java从FTP服务器下载文件并保存到本地计算机的特定目录。博主分享了代码示例,包括使用FtpClient进行连接、登录、切换目录、设置二进制模式以及读写文件的过程。在评论区,用户们交流了代码实现的细节,最终解决了下载到指定目录的问题。

1969

被折叠的 条评论
为什么被折叠?



