FTP檔案處理

2021-08-19 09:54:42 字數 1544 閱讀 8331

org.apache.commons.net.ftp.ftpclient
apache的ftpclient包有許多方便的函式操作;在使用時候需要注意:

中文名稱檔案編碼(ftp檔案中文名稱的編碼是「iso-8859-1」);

new string(filename.getbytes("gbk"),"iso-8859-1")
/** 本地字元編碼 */

private static string local_charset = "gbk";

// ftp協議裡面,規定檔名編碼為iso-8859-1

private static string server_charset = "iso-8859-1";

轉換到ftp伺服器時的路徑問題(路徑需從根目錄開始配置 /;否則無法轉移到ftp伺服器目錄);

設定檔案型別 二進位制(以便操作)

ftp.setfiletype(ftp.binary_file_type);
// 獲得檔案大小

int size = (int) fs[0].getsize();

byte bytes = new byte[size];

bytearrayoutputstream os = new bytearrayoutputstream();

//寫入輸出流

ftp.retrievefile(new string(name.getbytes(local_charset),server_charset),os);

bytes = os.tobytearray();

os.flush();

os.close();

name = new string(name.getbytes(local_charset),server_charset);

// 設定response的header

response.addheader("content-disposition", "attachment;filename=" + name);

response.addheader("content-length", "" + ff.getsize());

outputstream toclient = new bufferedoutputstream(response.getoutputstream());

toclient.write(bytes);

解決ftp.listfiles() 為空:

1.ftp.listfils("path");若path中有中文 將編碼改為「iso-8859-1」;

2.在獲取檔案列表之前 新增

ftp.enterlocalpassivemode();
ftpclient.enterlocalactivemode();    //主動模式

// ftpclient.enterlocalpassivemode(); 被動模式 

可參考:

ftp傳送檔案

ftp ftp open 10.173.255.58 connected to 10.173.255.58 10.173.255.58 220 vsftpd 2.2.2 name 10.173.255.58 gbase 331 please specify the password.password...

FTP檔案上傳

是網上找的例子,原文找不到了。public class ftputils2 return 判斷是否登入成功 public boolean ftplogin else ftp伺服器連線回答 int reply this.ftpclient.getreplycode if ftpreply.isposi...

ftp 檔案共享

ftp 檔案傳輸協議 軟體包 vsftpd ftp埠 控制埠 21 tcp 資料埠 20 tcp 配置檔案 etc vsftpd vsftpd.conf 有主動模式和被動模式 注釋 1 控制埠是用來建立連線的,不能傳資料 2 主動模式是伺服器主動送達,客戶端需要提供位址,不安全 3 被動模式是客戶端...