通过 FTP 将文件发送到服务器,但结果到达时字节大小为零
我正在尝试使用 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
}
}
}
}
}
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).
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
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我删除了
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
看看其他一些 FTPClient 问题,我认为原因是 Apache Commons-NET 中的 bug版本 3.0 中的库(FTPClient 是其中的一个组件)。
安装较新的版本(3.0.1修复了错误)。
Looking at some other FTPClient-questions, I think the reason is a bug in the Apache Commons-NET library (of which the FTPClient is a component) in version 3.0.
Install a newer version (3.0.1 fixes the bug).