我想使用FTPClient将文件上传到数据库服务器。它显示文件已成功传输,但文件为空(大小为0字节)。
以下是我用来构建的源代码。任何人都可以解决这个问题吗?
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
}
}
}
}
}
答案 0 :(得分:1)
在查看其他一些FTPClient问题时,我认为原因是版本3.0中的bug in the Apache Commons-NET library(其中FTPClient是一个组件)。
安装较新版本(3.0.1修复了该错误)。
答案 1 :(得分:1)
我删除了 client.changeWorkingDirectory( “/ loaddata”); 否则一切都完好无损并取得成功。这条线可能有问题吗? 我再次使用公共3.1