我编写了一个独立的程序来将文件上传到 FTP 服务器。代码运行正常,但我在 FTP 上找不到该文件。这是代码
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPDemo {
public static void main(String[] args) {
FTPClient ftp = new FTPClient();
int reply;
try {
ftp.connect("ip address");
ftp.login("username","password");
reply = ftp.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
System.out.println("Connected Success");
}else {
System.out.println("Connection Failed");
ftp.disconnect();
}
FileInputStream fis = null;
String filename = "demo.txt";
fis = new FileInputStream("C:\\demo.txt");
System.out.println("Is file stored: "+ftp.storeFile(filename,fis));
fis.close();
ftp.disconnect();
} catch (SocketException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
文件是否已存储返回 false。可能是什么问题?
最佳答案
让我引用一下 FTPClient documentation :
The convention for all the FTP command methods in FTPClient is such that they either return a boolean value or some other value. The boolean methods return true on a successful completion reply from the FTP server and false on a reply resulting in an error condition or failure. The methods returning a value other than boolean return a value containing the higher level data produced by the FTP command, or null if a reply resulted in an error condition or failure. If you want to access the exact FTP reply code causing a success or failure, you must call getReplyCode after a success or failure.
换句话说,要了解失败的实际原因,您需要调用 ftp.getReplyCode()
并从那里开始工作。
关于java - 文件未成功上传至 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10279885/