獲取ftp上指定檔案的InputStream

2021-09-12 23:11:24 字數 1293 閱讀 1492

ftp.retrievefilestream(filename)可以返回ftp檔案的輸入流 ,但輸入流關閉(inputstream.close())之後返回的inputstream物件就為空,可以通過位元組陣列byte儲存後返回出去再new bytearrayinputstream(bytes)方法重新獲得輸入流。

/**

* 獲取ftp上檔案的inputstream

* @param ftpdirname ftp資料夾名稱

* @param ftpfilename ftp檔名稱

* @return

*/public byte getinputstream(string ftpdirname, string ftpfilename)

string dir = new string(ftpdirname.getbytes("gbk"), "iso-8859-1");

if (!ftp.changeworkingdirectory(dir))

// 一定要加上字符集指定,因為獲取檔案時有中文,會出現亂碼而獲取不到。

string filename = new string(ftpfilename.getbytes("gbk"), "iso-8859-1");

ftp.setfiletype(ftpclient.binary_file_type);

// 每次資料連線之前,ftp client告訴ftp server開通乙個埠來傳輸資料,ftp server可能每次開啟不同的埠來傳輸資料,

// 但是在linux上,由於安全限制,可能某些埠沒有開啟,所以就出現阻塞。

ftp.enterlocalpassivemode();

inputstream inputstream = ftp.retrievefilestream(filename);

byte bytes = ioutils.tobytearray(inputstream);

if (inputstream != null)

ftp.getreply();

ftp.logout();

return bytes;

} catch (exception e)

}

呼叫時**如下:

byte inputstream = ftphelper.getinputstream(ecg_path, filename);

inputstream filestream = new bytearrayinputstream(inputstream);

上傳指定檔案到ftp伺服器上

echo offset ftpuser username setftppass password setftpip 192.168 0.2 setupfolder c temp setftpfile temp tempacc txt ftpfile echo ftpuser ftpfile echo...

FTP定時獲取檔案

裝置定時任務crontab e,根據個人要求設定時間可以參考如下 刪除前5天備份檔案 for i 5 i 10 i 命名檔案是根據時間命名,所以迴圈尋找以時間命名檔案,查詢5天檔案 dopredate date y m d d i day rm rf bbb bak predate done 備份目...

獲取指定檔案的屬性

獲取指定檔案的屬性 include include include int stat const char pathname,struct stat buf 若獲取符號鏈結檔案的話,實際上它獲取的是鏈結對應的實際檔案的屬性 int lstat const char pathname,struct s...