Java 使用apache commons将多个文件FTP到本地目录中
我正在尝试使用apache commons将目录中的所有文件下载到本地计算机,如下所示:Java 使用apache commons将多个文件FTP到本地目录中,java,apache,ftp-client,Java,Apache,Ftp Client,我正在尝试使用apache commons将目录中的所有文件下载到本地计算机,如下所示: import java.io.FileOutputStream; import org.apache.commons.net.ftp.FTPClient; import java.io.IOException; import java.net.SocketException; import org.apache.commons.net.ftp.FTPFile; public class FTPExampl
import java.io.FileOutputStream;
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPFile;
public class FTPExample {
public static void main(String[] args) throws SocketException, IOException {
FTPClient client = new FTPClient();
client.connect("MyHostName");
client.enterLocalPassiveMode();
client.login("username", "password");
FTPFile[] files = client.listFiles("/App/");
for (FTPFile file : files) {
System.out.println(file.getName());
FileOutputStream fos = new FileOutputStream("Ftp Files/" + file.getName());
client.retrieveFile(file.getName(),fos);
}
}
}
我能够列出目录中的文件,但我在尝试下载文件时遇到FilenotFound异常。请帮忙。
我的错误是:
Exception in thread "main" java.io.FileNotFoundException: Ftp Files\01 (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at ftpexample.FTPExample.main(FTPExample.java:30)
Java Result: 1
线程“main”java.io.FileNotFoundException:Ftp Files\01中的异常(系统找不到指定的路径)
在java.io.FileOutputStream.open(本机方法)
位于java.io.FileOutputStream。(FileOutputStream.java:212)
位于java.io.FileOutputStream。(FileOutputStream.java:104)
位于ftpexample.ftpexample.main(ftpexample.java:30)
Java结果:1
编辑:我需要文件以原始文件名存储在文件夹Ftp File/中。感谢您的帮助。我在这里找到了问题的答案。我就是这样做的:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTPExample {
public static void main(String[] args) {
try {
//new ftp client
FTPClient ftp = new FTPClient();
//try to connect
ftp.connect("MyHhostName");
//login to server
if (!ftp.login("username", "password")) {
ftp.logout();
}
int reply = ftp.getReplyCode();
//FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
//enter passive mode
ftp.enterLocalPassiveMode();
//get system name
System.out.println("Remote system is " + ftp.getSystemType());
//change current directory
ftp.changeWorkingDirectory("/App/PMIGENV/BACK/Finacle/FC/app/CDCI_LOGS/log/UBSADMIN");
System.out.println("Current directory is " + ftp.printWorkingDirectory());
//get list of filenames
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
//loop thru files
for (FTPFile file : ftpFiles) {
if (!file.isFile()) {
continue;
}
System.out.println("File is " + file.getName());
//get output stream
OutputStream output;
output = new FileOutputStream("FtpFiles" + "/" + file.getName());
//get the file from the remote system
ftp.retrieveFile(file.getName(), output);
//close output stream
output.close();
//delete the file
// ftp.deleteFile(file.getName());
}
}
ftp.logout();
ftp.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
感谢那些试图帮忙的人。我在这里找到了问题的答案。我就是这样做的:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTPExample {
public static void main(String[] args) {
try {
//new ftp client
FTPClient ftp = new FTPClient();
//try to connect
ftp.connect("MyHhostName");
//login to server
if (!ftp.login("username", "password")) {
ftp.logout();
}
int reply = ftp.getReplyCode();
//FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
//enter passive mode
ftp.enterLocalPassiveMode();
//get system name
System.out.println("Remote system is " + ftp.getSystemType());
//change current directory
ftp.changeWorkingDirectory("/App/PMIGENV/BACK/Finacle/FC/app/CDCI_LOGS/log/UBSADMIN");
System.out.println("Current directory is " + ftp.printWorkingDirectory());
//get list of filenames
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
//loop thru files
for (FTPFile file : ftpFiles) {
if (!file.isFile()) {
continue;
}
System.out.println("File is " + file.getName());
//get output stream
OutputStream output;
output = new FileOutputStream("FtpFiles" + "/" + file.getName());
//get the file from the remote system
ftp.retrieveFile(file.getName(), output);
//close output stream
output.close();
//delete the file
// ftp.deleteFile(file.getName());
}
}
ftp.logout();
ftp.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
这是工作代码。我已经试了很长时间了,但现在效果很好。 以前它下载的文件大小为0Kb
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPDownload {
public void ftpDownload() throws IOException
{
String host="your_host_name";
String uname="your_user_name";
String pass="your_password";
String remoteDIR="/public_html/tmp";
String localDIR="D://FTP";
//Object for FTPClient class
FTPClient ftp=new FTPClient();
ftp.connect(host);
boolean login=ftp.login(uname, pass);
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(remoteDIR);
FTPFile[] files=ftp.listFiles();
try{
if(login){
System.out.println("Your Are Logged In "+ftp.getStatus());
System.out.println("Working Directory is "+ftp.printWorkingDirectory());
System.out.println("Local Directory is "+localDIR);
System.out.println("Total Files Are "+files.length);
if(files != null && files.length >0 )
{
for(FTPFile fl:files)
{
if(!fl.isFile())
{
continue;
}
System.out.println(fl.getName());
OutputStream out;
out=new FileOutputStream(localDIR+"/"+fl.getName());
ftp.retrieveFile(fl.getName(), out);
out.close();
}
}
}
else
{
System.out.println("Sorry");
}
ftp.logout();
ftp.disconnect();
}
catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) throws IOException {
FTPDownload ft=new FTPDownload();
ft.ftpDownload();
}
}
这是工作代码。我已经试了很长时间了,但现在效果很好。 以前它下载的文件大小为0Kb
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FTPDownload {
public void ftpDownload() throws IOException
{
String host="your_host_name";
String uname="your_user_name";
String pass="your_password";
String remoteDIR="/public_html/tmp";
String localDIR="D://FTP";
//Object for FTPClient class
FTPClient ftp=new FTPClient();
ftp.connect(host);
boolean login=ftp.login(uname, pass);
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(remoteDIR);
FTPFile[] files=ftp.listFiles();
try{
if(login){
System.out.println("Your Are Logged In "+ftp.getStatus());
System.out.println("Working Directory is "+ftp.printWorkingDirectory());
System.out.println("Local Directory is "+localDIR);
System.out.println("Total Files Are "+files.length);
if(files != null && files.length >0 )
{
for(FTPFile fl:files)
{
if(!fl.isFile())
{
continue;
}
System.out.println(fl.getName());
OutputStream out;
out=new FileOutputStream(localDIR+"/"+fl.getName());
ftp.retrieveFile(fl.getName(), out);
out.close();
}
}
}
else
{
System.out.println("Sorry");
}
ftp.logout();
ftp.disconnect();
}
catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) throws IOException {
FTPDownload ft=new FTPDownload();
ft.ftpDownload();
}
}
本地计算机上的文件夹似乎不存在。不,它在那里相信我。本地计算机上的文件夹似乎不存在。不,它在那里相信我。你不认为现在这样做有点太晚了吗?我知道,但我只是想分享我所做的一切,我想它可能会帮助像我这样真正想解决同样问题的人。你不觉得现在做这个有点太晚了吗?我知道,但我只是想分享我所做的一切,我想它可能会帮助像我这样真正想解决同样问题的人。