无法使用Apache commons-net-3.1在FTP上上传大文件

时间:2012-03-22 10:54:45

标签: java ftp-client apache-commons-net

我正在尝试使用FTPClient的storeFile(远程,本地)方法上传一个大文件(超过8 MB),但结果为false。它上传了一些额外的字节。以下是带输出的代码:

public class Main {
public static void main(String[] args) {
FTPClient client = new FTPClient();
FileInputStream fis = null;

try {

client.connect("208.106.181.143");
client.setFileTransferMode(client.BINARY_FILE_TYPE);
client.login("abc", "java");
int reply = client.getReplyCode();
System.out.println("Received Reply from FTP Connection:" + reply);

if(FTPReply.isPositiveCompletion(reply)){
    System.out.println("Connected Success");
}

client.changeWorkingDirectory("/"+"Everbest"+"/");
client.makeDirectory("ETPSupplyChain5.3-EvbstSP3");
client.changeWorkingDirectory("/"+"Everbest"+"/"+"ETPSupplyChain5.3-EvbstSP3"+"/");
FTPFile[] names = client.listFiles();
 String filename = "E:\\Nitin\\D-Drive\\Installer.rar";

fis = new FileInputStream(filename);

boolean result = client.storeFile("Installer.rar", fis);
 int replyAfterupload = client.getReplyCode();
System.out.println("Received Reply from FTP Connection replyAfterupload:" + replyAfterupload);
System.out.println("result:"+result);
for (FTPFile name : names) {
    System.out.println("Name = " + name);
  }

client.logout();

fis.close();



client.disconnect();
} catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 }
}




o/p:
Received Reply from FTP Connection:230
Connected Success
32   /Everbest/ETPSupplyChain5.3-EvbstSP3
Received Reply from FTP Connection replyAfterupload:150
result:false

1 个答案:

答案 0 :(得分:0)

首先,8 Mb文件不是一个大文件。我设法使用这个库上传大于100Mb的文件。

使用参数setFileTypestoreFile方法之前调用BINARY_FILE_TYPE方法。它告诉服务器你的文件不应该存储为ASCII。

最后,尝试在FileInputStream方法之后关闭storeFile。并捕获close方法的结果。