Edtftpj is a very powerful FTP component, including Java,. net, and JavaScript.
The Java version is chargedEdtftpj/Pro and free open-sourceEdtftpj/free.
Used hereEdtftpj/free.
Edtftpj/free provides a stable, powerful, and easy-to-use class library, which makes it very easy to transmit files using the FTP protocol.
Edtftpj/free has the following functions:
1) fast and stable file transfer through FTP
2) active and passive modes, binary transmission and ASCII transmission are supported.
3) Resume transmission after interruption
4) supports flexible reading and writing from the FTP server using FTP streams
Edtftpj/freeHttp://www.enterprisedt.com/products/edtftpj/download.html
Download and decompressAdd edtftpj. jar to the classpath path.
1. Write an FTP tool class to obtain the FTP client.
Package CN. luxh. utils; Import Java. Io. ioexception; Import Com.w.isedt.net. FTP. ftpconnectmode; Import Com.w.isedt.net. FTP. ftpexception; Import Com.w.isedt.net. FTP. ftptransfertype; Import Com.w.isedt.net. FTP. filetransferclient; /** * FTP util * @ Author Luxh */ Public Class Ftputil { /** ** @ Param Host FTP server address * @ Param Username FTP account * @ Param Password FTP password * @ Return * @ Throws Ftpexception * @ Throws Ioexception */ Public Static Filetransferclient getfiletransferclient (string host, string username, string password) Throws Ftpexception, ioexception { // Create an FTP Client // Ftpclient is not officially recommended Filetransferclient FTP = New Filetransferclient (); // Set up an FTP server FTP. setremotehost (host ); // Set FTP User Name FTP. setusername (username ); // Set FTP Password FTP. setpassword (password ); // Solve Chinese file name garbled FTP. getadvancedsettings (). setcontrolencoding ("GBK" ); // Passive mode. The data connection is initiated by the client. FTP. getadvancedftpsettings (). setconnectmode (ftpconnectmode. PASV ); // Files written in HTML and text must be uploaded in ASCII mode. files uploaded in binary mode will be damaged, leading to file execution errors. // FTP. setcontenttype (ftptransfertype. ASCII ); // Binary mode is used to transfer executable files, compressed files, and image files. FTP. setcontenttype (ftptransfertype. Binary ); // Connect to the FTP server FTP. Connect (); Return FTP ;} /** * Disconnect from the FTP server * @ Param FTP * @ Throws Ftpexception * @ Throws Ioexception */ Public Static Void Disconnectfromftpserver (filetransferclient FTP) Throws Ftpexception, ioexception { If (FTP! = Null && FTP. isconnected () {ftp. Disconnect ();}}}
2. perform a simple test on uploading, downloading, and deleting files.
Package CN. luxh. test; Import Java. Io. ioexception; Import Org. JUnit. test; Import Com.w.isedt.net. FTP. ftpexception; Import Com.w.isedt.net. FTP. filetransferclient; Import CN. luxh. utils. ftputil; Public Class Ftputiltest { /** FTP Server */ Private Static Final String ftp_host = "127.0.0.1" ; /** FTP account */ Private Static Final String ftp_username = "root" ; /** FTP Password */ Private Static Final String ftp_password = "root" ; /** * Upload a file * @ Throws Ioexception * @ Throws Ftpexception */ @ Test Public Void Testuploadfile () Throws Ftpexception, ioexception {filetransferclient FTP =Ftputil. getfiletransferclient (ftp_host, ftp_username, ftp_password); string localfilename = "D:/test/Chinese .txt" ; String remotefilename = "/Test/Chinese .txt" ; Ftp. uploadfile (localfilename, remotefilename); ftputil. disconnectfromftpserver (FTP );} /** * Download an object * @ Throws Ftpexception * @ Throws Ioexception */ @ Test Public Void Testdownloadfile () Throws Ftpexception, ioexception {filetransferclient FTP = Ftputil. getfiletransferclient (ftp_host, ftp_username, ftp_password); ftp. downloadfile ( "D:/test/Chinese .txt", "/test/Chinese .txt" ); Ftputil. disconnectfromftpserver (FTP );} /** * Delete an object * @ Throws Ftpexception * @ Throws Ioexception */ @ Test Public Void Testdeletefile () Throws Ftpexception, ioexception {filetransferclient FTP = Ftputil. getfiletransferclient (ftp_host, ftp_username, ftp_password); ftp. deletefile ( "/Test/Chinese .txt" ); Ftputil. disconnectfromftpserver (FTP );}}