本文介绍了FTP将文件传输到服务器,但结果以零字节大小到达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用FTPClient将文件上传到数据库服务器。它显示文件已成功传输,但文件为空(大小为0字节)。
以下是我用来构建的源代码。任何人都可以解决这个问题吗?
package示例;
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;
尝试{
client.connect(server);
client.login(userid,password);
System.out.print(Message:+ client.getReplyString());
client.changeWorkingDirectory(/ loaddata);
System.out.println(工作目录+ client.printWorkingDirectory());
client.setDefaultPort(22);
int f1 = client.getDefaultPort();
布尔值f2 = client.setFileType(FTPClient.BINARY_FILE_TYPE);
System.out.println(File transfer port no+ f1);
System.out.println(FTP服务器回复。+ client.getReplyString());
字符串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(成功上传文件);
} else {
System.out.println(无法上传文件);
}
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 not
}
}
}
}
}
解决方案
我删除了
client.changeWorkingDirectory(/ loaddata);
,否则保持原样并取得成功。线路是否会成为问题?
再次使用了commons 3.1
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
}
}
}
}
}
解决方案 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
这篇关于FTP将文件传输到服务器,但结果以零字节大小到达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!