本文介紹了將文件 FTP 到服務(wù)器,但結(jié)果以零字節(jié)大小到達的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試使用 FTPClient 將文件上傳到數(shù)據(jù)庫服務(wù)器.顯示文件傳輸成功,但文件為空(大小為0字節(jié)).
I am tring to upload a file to a database server using FTPClient. It shows the file is transfered succesfully, but the file is empty (size 0 bytes).
以下是我用來構(gòu)建的源代碼.誰能解決這個問題?
Below is the source code that I have used to build. Can anyone resolve this issue?
package Examples;
import org.apache.commons.net.ftp.*;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("server");
client.login("userid", "password");
System.out.print("Message : " + client.getReplyString());
client.changeWorkingDirectory("/loaddata");
System.out.println("Working Directory" + client.printWorkingDirectory());
client.setDefaultPort(22);
int f1 = client.getDefaultPort();
boolean f2 = client.setFileType(FTPClient.BINARY_FILE_TYPE);
System.out.println("File transfer port no " + f1);
System.out.println("FTP server reply ." + client.getReplyString());
String localfile = "c:/Touch.txt";
fis = new FileInputStream(localfile);
int lastSlash = localfile.lastIndexOf('/');
String filename = localfile.substring(lastSlash+1);
System.out.println("file : "+fis);
client.setFileTransferMode(2);
System.out.println("Flag reply ." + client.getReplyString());
boolean flag = client.storeFile(filename,fis);
System.out.println("Flag reply ." + client.getReplyString());
if (flag) {
System.out.println("Successfully uploaded the file");
} else {
System.out.println("Not able to upload the file");
}
fis.close();
client.logout();
System.out.println("Logout ." + client.getReplyString());
} catch (Exception e) {
System.out.println("Exception " + e);
} finally {
if (client.isConnected()) {
try {
client.disconnect();
System.out.println("Server Disconnected." + client.getReplyString());
} catch (IOException ioe) {
// do nothing
}
}
}
}
}
推薦答案
我刪除了client.changeWorkingDirectory("/loaddata");否則保持原樣并獲得成功.線路會不會有問題?比我再次使用 commons 3.1
I removed the client.changeWorkingDirectory("/loaddata"); otherwise left it all intact and got success. Could the line be a problem? Than again I used commons 3.1
這篇關(guān)于將文件 FTP 到服務(wù)器,但結(jié)果以零字節(jié)大小到達的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!