java通过ftp上传文件到服务器_java-通过FTP将文件传输到服务器,但结果以零...
我正在嘗試使用FTPClient將文件上傳到數(shù)據(jù)庫(kù)服務(wù)器.它顯示文件已成功傳輸,但是文件為空(大小為0字節(jié)).
以下是我用來(lái)構(gòu)建的源代碼.誰(shuí)能解決這個(gè)問(wèn)題?
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
}
}
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java通过ftp上传文件到服务器_java-通过FTP将文件传输到服务器,但结果以零...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: nginx.redis.mysql_ng
- 下一篇: K9为印度国产坦克?