java對ftp的相關操作

2021-08-21 04:49:10 字數 3880 閱讀 9972

2.4

.jar commons-net-

2.2.jar

publicclass ftputil

publicftputil setconfig(stringhost,

int port,stringusername,stringpassword)

/**

* 連線ftp伺服器 */

privateftputil connectserver()else

// 2、設定連線屬性

client.setcontrolencoding(

"gbk");

// 設定以二進位制方式傳輸

client.setfiletype(ftpclient.binary_file_type);

client.enterlocalpassivemode();

}}catch(socketexception e)catch(ioexception e)

returnthis;

}/**

*/ publiclistdownload()

logger.info(

"個檔案");

for(ftpfile file : ftpfiles)

if(files ==null)

is = client.retrievefilestream(file.getname());

if(is ==null)

downloadfile =newfile(localdir + file.getname());

fileoutputstream fos = fileutils.openoutputstream(downloadfile);

ioutils.

copy(is, fos);

client.completependingcommand();

ioutils.closequietly(is);

ioutils.closequietly(fos);

files.

add(downloadfile);

}logger.info(

returnfiles;

}catch(ioexception e)

}publiclistdownload(stringremotedir,stringlocaldir)

publiclistdownload(stringremotedir,stringregex,stringlocaldir)

publiclistdownload(stringdownloadfilename)

/**

* 上傳檔案

*  /

publicvoidupload(listfiles)

logger.info(

"準備上傳" + files.

size() +

"個檔案");

for(file file : files)

ioutils.

copy(newfileinputstream(file), os);

ioutils.closequietly(os);

}logger.info(

"檔案上傳成功,上傳檔案路徑:" + remotedir);

}catch(ioexception e)

}publicvoidupload(listfiles,stringremotedir)

publicvoidupload(file file)

/**

*  判斷檔案在ftp上是否存在 */

publicboolean isfileexist(stringfilename)

// 3、檢查檔案是否存在

for(ftpfile file : ftpfiles)

}}catch(exception e)

returnresult;

}/**

* 模糊搜尋獲取檔案全稱 */

publicstringgetfullname(stringname)

// 3、檢查檔案是否存在

for(ftpfile file : ftpfiles)

}}catch(exception e)

returnresult;

}/**

* 關閉連線 */

publicvoidcloseconnect()catch(ioexception e)

}publicstringgetremotedir()

publicvoidsetremotedir(stringremotedir)

publicstringgetlocalpath()

publicvoidsetlocalpath(stringlocalpath)

publicstringgetdownloadfilename()

publicvoidsetdownloadfilename(stringdownloadfilename)

publicstringtostring()

}測試方法:

publicstaticvoidmain(stringargs)throwsioexception

java連線FTP相關開發

近期要做連線ftp的 開發,老的賬號密碼忘了。所以只能在這裡記載記載,謝謝 直接貼 吧,基本注釋寫的都挺清晰的 配置位址路徑 public class constants工具類方法 public class ftpuntil public ftpuntil string ftpip,int port...

FTPHelper 封裝FTP的相關操作

using system using system.io using system.net using system.text namespace whir.software.datasynctools.library.helper public ftpresult download string ...

java對Data的操作

date now new date 2018,5,1 system.out.println now.getdate date有個建構函式可以輸入年,月,日,當日 0時,其實輸出的是上個月的最後一天 dateformat format new dateformat yyyy mm dd date no...