For details, please see below:
In terms of application of files for interactive data, using the FTP server is a good choice. This article uses Apache Jakarta Commons Net (Commons-Net-3.3.JAR) based on Filezilla Server server to implement the file upload/download/deletion of files on the FTP server.
For the detailed construction process of the Filezilla Server server, please refer to the Filezilla Server installation configuration tutorial. Some friends said before that uploading large files (more than a few hundred m) to the FTP server will reproduce the problem that cannot be renamed, but I personally test the upload of 2G files to the Filezilla Server. Friends can use it with confidence This code.
Favftputil.javapackage com.favccxx.favsoft.util; Import java.io.file; Import java.io.fileinputstream; Import java.fileoutstream; Import java.io.ioException; Import java.Io.inputstream; Import java.io .OutputStream; Import org.apache.Commons.net.ftpClient; Import org.apache.commons.net.ftpfile; Import Org.apache.commons.netp.ftp Reply; Public Class Favftputil { /*** Upload files (available for Action/Controller layer)* @Param Hostname FTP server address* @Param Port FTP server port number* @param username FTP account number* @Param Password login password* @param AME FTP server saves directory* @ Param Filename The file name after uploading to the FTP server* @param InputStream input file stream* @Return*/ Public Static Boolean UploadFile (String Hostname, Int Port, String UserName, Strin G Password, String PathName, String Filename, InputStream InputStream) {Boolean) Flag = false; ftpclient ftpclient = new ftpClient (); ftpClient.setControleencoding ("UTF-8"); Try {// ; // Log in to ftp server ftpclient.login (username, password); // Whether to log in to the ftp server int replycode = ftpclient.getRePlycode (); if (! ftpreply.ispositiveCompleTion) {Return Flag;} ftpclient .setFileType (ftpclient.binary_file_type); ftpclient.makedirectory (pathname); ftpclient.changeworkingDirectory (pathname); ftpclient.storefile (filename, inputStream); inputStream.close (); ftpclient.logout (); } Catch (Exception E) {e.printstacktrace ();} Finally {if (if ftpclient.isconnect ()) {try {ftpClient.disconnect ();} Catch (IOEXCEPTION E) {e.printstacktrace ();}}} /*** upload file (can be paired with The file is renamed) * @Param Hostname FTP server Address* @param Port FTP server port number* @param username FTP login account* @param password FTP login password* @param pathname FTP server The file name after uploading to FTP server* @ @ Param Originfilename name (absolute address) * @Return */ Public Static Boolean UploadFileFromPRoduction (String Port, String Username, String Word, String PathName, String Filename, String Originfilename) {Boolean Flag = False; TRY { InputStream InputStream = New FileInputStream (New File (Originfilename)); Flag = UploadFile (Hostname, Port, Username, Pathname, FILENAME, Inpu tStream);} Catch (Exception E) {e.printstacktrace ();} Return Flag;} /*** Upload files (renamed no files)* @Param Hostname FTP server Address* @Param Port FTP server port number* @param username FTP login account number* @Param password login password* @param me FTP The server saves the directory * @param Originfilename name (absolute address) * @Redurn */ Public Static Boolean uploadFileFromPRODUCTION (String Port, String , String Password, String PathName, String Originfilename) {Boolean Flag = false; try {string filename = new file (Originfilename) .getName (); inputStream InputStream = New FileInputStream (New File (Originfilename)); Le (Hostname, Port, Username, Password, PathName, FILENAME, InputStream);} Catch ( Exception E) {e.printstacktrace ();} Return Flag;} /*** Delete file* @param Hostname FTP server Address* @Param Port FTP server number* @Param Username FTP login account number * @param Password FTP login password * @Param Pathname FTP server Save the directory* @param Filename The file name to delete* @Return*/ Public Static Boolean Deletefile (String Hostname, INT PORT, String Username, String Password, String PathName, String Filename) {Boolean Flag = False ; Ftpclient ftpclient = new ftpclient (); try {// connecting the ftpclient.connect (hostname, port); // Login the ftpclient.login (username, password); // Verify whether the FTP server is logged in to successfully int Replycode = ftpclient.getreplycode (); if (! ftpreply.ispositiveComplting (Replycode)) {Return Flag;} // Switch ftp directory.changeworctionDirectory (PATHNAM e); ftpClient.dele (fileename); ftpclient.logout (); flag = true; } Catch (Exception E) {e.printstacktrace ();} Finally {if (ftpclient.isconnect ()) {try {ftpClient.logout ();} Catch (IOEXCEPTION E) {}} } Return Flag;} /** * Download file* @Param Hostname FTP server Address* @Param Port FTP server port number* @param username FTP login account* @Param Password FTP Login password* @Param Pathname FTP server file directory* @param FILENAME file name* @param localpath The downloaded file path * @Return */ Public Static Boolean Downloadfile (String Hostname, Int Port, String Username, String Pathname, String FILENAME, Strin G LOCALPATH) {boolean flag = false; ftpclient ftpclient = new ftpclient (); TRY {// connect FTPClient.connect (hostname, port); // Log in to ftpclient.login (username, password); // Verify whether the FTP server logs in successfully int replycode = ftpclient.g etreplycode (); if (!! Ftpreply.ispositiveCompleTion (REPLYCODE) {Return Flag;} // Switch ftpClient.changeworkingDirectory (pathname); FTPFILE [] ftpClien T.ListFiles (); for (FTPFile File: FTPFILES) {if (Filename.equalSignorecase (File .getName ()))) {file localFile = new file (localpath + "/" + file.getName ()); outputStream OS = New FileoutStream (LOCALFILE); iLe (file.getName (), OS); OS. close ();}} ftpclient.logout (); Flag = TRUE;} Catch (Exception E) {e.printstacktrace ();} Finally {if (ftpclient.isconnected ()) {Try {Try ftpclient.logout ();} Catch (IOEXCEPTION E) {}}} Return Flag;}} favftputilTest.javapackage com.favccxx.favsoft.util; Import Junit.testcase; Publ IC Class Favftptest Extends Testcase {Public Void Testfavftputil () {String hostname = "127.0. 0.1 "; int port = 21; string username =" Business "; string password =" Business "; String pathname =" Business/eBook "; String Filename =" Big.rar "; String Originfilename = "C: // Users/ /Downloads/downloads.rar "; Favftputil.uploadFileFromPRoduction (Hostname, Port, Username, PATHNAME, FILENAME, Originfilename); // Ring localPath = "d:/"; // Favftputil.downloadFile (hostname, port, username , Password, PathName, FILENAME, LOCALPATH);}}
The above is the complete code of downloading the download file through FTP. I hope it will be helpful to everyone.