我想用Apache Commons Net實現一個FTP客戶端來上傳數據。 連接和登錄到FTP服務器工作正常。 但上傳不起作用。 這些文件與原件有點大。 並且文件已損壞。 我嘗試了一個圖像,一個視頻和一個文本文件。只有文本文件沒問題。Apache Commons FTP問題
現在我看到在調試
boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
給我false
。所以它不能設置。爲什麼? (也許這是沒有問題嗎?)
這裏,我的代碼的其餘
client=new FTPClient();
try {
int reply;
client.connect(url, port);
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
client.login(user, pw);
boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(300);
client.enterLocalPassiveMode();
if (client.isConnected())
{
try {
File file=new File(<FILE>);
FileInputStream inputStream = new FileInputStream(file);
OutputStream outputStream = client.storeFileStream(file.getName());
byte[] buffer = new byte[4096];
int l;
while((l = inputStream.read(buffer))!=-1)
{
outputStream.write(buffer, 0, l);
}
inputStream.close();
outputStream.flush();
outputStream.close();}
嗨,我試過了,但是這個方法讓我錯誤,所以模式無法設置。 – Igor
你試過這個在調用connect方法後的地方嗎? –
並且在登錄方法之後。 –