問題描述
請解決這個問題.代碼運行正常,沒有任何異常.
Please figure this out. The code runs properly without any exception.
try
{
FTPClient ftp = new FTPClient();
ftp.connect(server);
if(!ftp.login(username, password))
{
ftp.logout();
return false;
}
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return false;
}
InputStream in = new FileInputStream(localfile);
ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE);
ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);
Store = ftp.storeFile(destinationfile, in);
in.close();
ftp.logout();
ftp.disconnect();
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
return Store;
但是 return 語句總是返回 false 并且文件沒有上傳到服務(wù)器上.有人請幫忙.
Butm the return statement always returns false and the file is not uploaded on the server. Someone please help on this.
供您參考,我在辦公網(wǎng)絡(luò)中.--->我們需要添加任何代理嗎?
For your information, I am in an office network. ---> do we need to add any proxies?
File file = new File("C:\Users\sg0214273\Desktop\seagate\seagate.txt");
FileInputStream input = new FileInputStream(file);
client.setFileType(FTP.BINARY_FILE_TYPE);
if (!client.storeFile(file.getName(), input)) {
System.out.println("upload failed!");
}
reply = client.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
System.out.println("upload failed!");
}
Login success...
230 User ******** logged in.
upload failed!-----> is form boolean return value of storefile
upload failed!---------> is from replycode...
Logout from FTP server...
請幫忙.
推薦答案
可以通過調(diào)用FtpClient#getReplyCode().從該頁面(我的重點):
The exact failure message can be found by calling FtpClient#getReplyCode(). From that page (my emphasis):
連接后立即是您需要檢查的唯一實時時間回復(fù)代碼(因為 connect 是 void 類型).該公約為FTPClient 中的所有 FTP 命令方法都是這樣的返回一個布爾值或其他一些值.布爾方法返回如果來自 FTP 服務(wù)器的成功完成回復(fù),則為 true,如果為 false導(dǎo)致錯誤情況或失敗的回復(fù).方法返回一個布爾值以外的值,返回一個包含由 FTP 命令生成的更高級別的數(shù)據(jù),如果回復(fù)則為 null導(dǎo)致錯誤情況或失敗.如果您想訪問導(dǎo)致成功或失敗的確切 FTP 回復(fù)代碼,您必須調(diào)用在成功或失敗后獲取回復(fù)代碼.
Immediately after connecting is the only real time you need to check the reply code (because connect is of type void). 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 服務(wù)器返回碼列表.
To see what a return code means, you can see Wikipedia: List of FTP server return codes.
這篇關(guān)于FtpClient storeFile 總是返回 False的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!