問題描述
我想用 Apache Commons Net 實現一個 FTP 客戶端,僅用于上傳數據.FTP 服務器的連接和登錄工作正常.但是上傳不正常.這些文件與原件相比有點大.并且文件已損壞.我嘗試了圖像、視頻和文本文件.只有文本文件沒問題.
I want to implement a FTP Client with Apache Commons Net only for uploading data. The Connection and Login to FTP-Server works fine. But the upload does not work right. The files are a little to big as the originals. And the files are damaged. I tried an image, a video and a textfile. Only the textfile is alright.
現在我在調試時看到了
boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
給我 false
.所以不能設置.為什么?(也許這不是問題?)
gives me false
. So it can not be set. Why?
(Maybe this is not the problem?)
這是我的其余代碼
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();}
推薦答案
更改如下:
boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
應該是:
boolean xxx=client.setFileType(FTP.BINARY_FILE_TYPE);
您將 FileTransferModes 與 FileTypes 混淆了.
You have confused FileTransferModes with FileTypes.
可用的文件類型有:
- FTP.ASCII_FILE_TYPE(默認)
- FTP.BINARY_FILE_TYPE
- FTP.EBCDIC_FILE_TYPE
- FTP.LOCAL_FILE_TYPE
可用的 FileTransferMode 有:
The available FileTransferModes are:
- FTP.STREAM_TRANSFER_MODE(默認)
- FTP.BLOCK_TRANSFER_MODE
- FTP.COMPRESSED_TRANSFER_MODE
我想如果 apache 為這些常量類型引入了枚舉,那么可以避免這種問題,但是該庫將無法用于 pre-java-5 運行時.
我想知道 java 1.4 兼容性到底有多大問題.
I suppose if apache introduced enums for these constant types, then this kind of problem could be avoided, but then the library would not be available to pre-java-5 runtimes.
I wonder how much of an issue java 1.4 compatibility really is.
這篇關于Apache Commons FTP 問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!