Apache FTP adalah alat klien unggahan FTP yang banyak digunakan. Mudah dioperasikan, memiliki kode sederhana dan struktur yang jelas. Ini adalah prioritas untuk perangkat lunak manajemen klien file FTP. Operasi FTP meliputi: Unggah file FTP (Transmisi kontinu Breakpoint), unduhan file FTP, penggantian nama file FTP, dan penghapusan file FTP. Operasi ini telah sepenuhnya menunjukkan metode manajemen aplikasi FTP. Jadi saya selalu menggunakan metode ini untuk mengimplementasikan manajemen server file FTP; dilampirkan dengan kode alat FTP.
1. Kelas Enumerasi Status Operasi File FTP
paket com.scengine.wtms.utils.ftp; public enum FTPStatus { File_Exits(0), Create_Directory_Success(1), Create_Directory_Fail(2), Upload_From_Break_Success(3), Upload_From_Break_Faild(4), Download_From_Break_Success(5), Download_From_Break_Faild(6), Unggah_new_file_success (7), unggah_new_file_failed (8), delete_remote_success (9), Delete_remote_faild (10), remote_bigger_local (11), remote_smaller_local (12), not_exist_file (13), remote_rename_success (14), remote_rename_faild (15), file_not_unique (16); status int pribadi; public int getStatus () {status pengembalian; } public void setstatus (status int) {this.status = status; } FtpStatus (status int) {this.status = status; }} 2. Kode Alat Operasi File FTP
paket com.scengine.wtms.utils.ftp; impor java.io.file; impor java.io.fileInputStream; impor java.io.fileoutputStream; impor java.io.ioException; impor java.io.inputstream; impor java.io.outputStream; impor java.io.outputStream; impor java.io.printwriter; impor javax.servlet.http.httpservletResponse; impor org.apache.commons.net.printcommandlistener; impor org.apache.commons.net.ftp.ftp; impor org.apache.commons.net.ftp.ftpclient; impor org.apache.commons.net.ftp.ftpfile; impor org.apache.commons.net.ftp.ftpreply; impor com.scengine.wtms.utils.log; kelas publik ftputils {private ftpClient ftpClient = ftpclient baru (); / *** Pengaturan Konstruksi Objek Output Perintah yang digunakan dalam proses ke konsol*/ public ftputils () {this.ftpclient.addprotocolCommandlistener (printCommandlistener baru (printwriter baru (System.out))); } / ** * * Digunakan untuk terhubung ke server FTP dalam pemrograman Java * * @param hostname * nama host * * @param port * port * * @param nama pengguna * nama pengguna * * @param kata sandi * @return apakah koneksi berhasil * * @throws ioException * / Public Boolean Connect (string host host, string host, string host, port, string string, string string {port {throws IoException * / Public boolean (string string {string string, string {port, string {port {throws IoException * / Public boolean (string string {port {throws) ftpClient.Connect (nama host, port); if (ftpreply.ispositiveCompletion (ftpclient.getReplycode ())) {if (ftpclient.login (nama pengguna, kata sandi)) {return true; }} disconnect (); mengembalikan false; } / ** * Hapus file ftp jarak jauh * * @param remote * Jalur file jarak jauh * @return * @throws ioException * / public ftpstatus delete (string remote) melempar ioException {ftpclient.enterLocalPassiveMode (); ftpClient.setFileType (ftp.binary_file_type); Hasil ftpstatus = null; Ftpfile [] file = ftpClient.listFiles (remote); if (file.length == 1) {status boolean = ftpclient.deleteFile (remote); hasil = status? Ftpstatus.delete_remote_success: ftpstatus.delete_remote_faild; } else {result = ftpstatus.not_exist_file; } Log.getLogger (this.getClass ()). Info ("Pengidentifikasi Penghapusan File Server FTP:"+Hasil); hasil pengembalian; } /** * Rename the remote FTP file* * @param name * New remote file name (path-must be guaranteed to be under the same path) * * @param remote * Remote file path* * @return Whether it is successful* * @throws IOException */ public FTPStatus rename(String name,String remote) throws IOException { ftpClient.enterLocalPassiveMode (); ftpClient.setFileType (ftp.binary_file_type); Hasil ftpstatus = null; Ftpfile [] file = ftpClient.listFiles (remote); if (file.length == 1) {status boolean = ftpclient.rename (remote, name); hasil = status? Ftpstatus.remote_rename_success: ftpstatus.remote_rename_faild; } else {result = ftpstatus.not_exist_file; } Log.getLogger (this.getClass ()). Info ("FTP Server FileName Update Identifier:"+hasil); hasil pengembalian; } /** * * Download file from the FTP server* * @param fileName * Download file name (including suffix name) * * @param remote * Remote file path* * @param local * Local file path* * @return Whether it is successful* * @throws IOException */ public FTPStatus download(String fileName,String remote,HttpServletResponse response) throws IOException { // Open File Pop-up Stream Output Simpan Path Window Response.setContentType ("Application/Octet-Stream"); Response.setContentType ("Aplikasi/Octet-Stream; Charset = UTF-8"); response.setHeader ("Disposisi Konten", "lampiran; fileName =" +nama file); ftpClient.enterLocalPassiveMode (); ftpClient.setFileType (ftp.binary_file_type); Hasil ftpstatus; OutputStream out = response.getoutputStream (); status boolean = ftpclient.retrievefile (remote, out); result = status? ftpstatus.download_from_break_success: ftpstatus.download_from_break_faild; Log.getLogger (this.getClass ()). Info ("Pengidentifikasi Unduh File Server FTP:"+Hasil); out.close (); hasil pengembalian; } / ** * Unduh file dari server ftp * * @param remote * Jalur file jarak jauh * * @param local * path file lokal * * @return apakah itu berhasil * * @throws ioException * / @suppresswarnings ("sumber daya") public ftpstatus (string remote, string lokal) ioExcepion {ftpSclientus public. ftpClient.setFileType (ftp.binary_file_type); Hasil ftpstatus; File f = file baru (lokal); Ftpfile [] file = ftpClient.listFiles (remote); if (file.length! = 1) {log.getLogger (this.getClass ()). Info ("File jarak jauh tidak unik"); return ftpstatus.file_not_unique; } long lremotesize = file [0] .getSize (); if (f.exists ()) {outputStream out = new fileoutputStream (f, true); Log.getLogger (this.getClass ()). Info ("Ukuran file lokal adalah:" + f.length ()); if (f.length ()> = lremotesize) {log.getLogger (this.getClass ()). Info ("Ukuran file lokal lebih besar dari ukuran file jarak jauh, unduh dibatalkan"); return ftpstatus.remote_smaller_local; } ftpClient.SetRestArtArtOffset (f.length ()); status boolean = ftpclient.retrievefile (remote, out); result = status? ftpstatus.download_from_break_success: ftpstatus.download_from_break_faild; out.close (); } else {outputStream out = FileOutputStream baru (f); status boolean = ftpclient.retrievefile (remote, out); result = status? ftpstatus.download_from_break_success: ftpstatus.download_from_break_faild; out.close (); } hasil pengembalian; }/** * Unggah file ke server ftp, mendukung transmisi kontinu breakpoint * * @param lokal * nama file lokal, path absolute * * @param jarak jauh * jalur file jarak jauh, menggunakan/home/directory1/subdirectory/file.ext * sesuai dengan jalur yang ditentukan pada linux, non-level directored, non-level non-level non-level, non-level non-level, non-level directored, multi-level non-level, Non-level nute-level, Non-level nute-level, Multi-level nuteRECTORY @Multi-lever Hasil * * @Throws IoException */ @suppressWarnings ("Resource") Public FTPStatus Upload (String Local, String Remote) Melempar IoException {// Set PassiveMode untuk mengirimkan ftpclient.enterLocalPassiveMode (); // diatur untuk mengirimkan ftpclient.setFileType (ftp.binary_file_type); Hasil ftpstatus; // menangani direktori jarak jauh string remoteFileName = remote; if (remote.contains ("/")) {remoteFileName = remote.substring (remote.LastIndexof ("/") + 1); String Directory = Remote.substring (0, Remote.LastIndexOf ("/") + 1); if (! Directory.equalsignorecase ("/") &&! ftpClient.ChangeworkingDirectory (direktori)) {// Jika direktori jarak jauh tidak ada, secara rekursif membuat direktori server jarak jauh int start = 0; int end = 0; if (directory.startswith ("/"))) {start = 1; } else {start = 0; } end = directory.indexof ("/", start); while (true) {string subdirectory = remote.substring (start, end); if (! ftpClient.ChangeworkingDirectory (subdirectory)) {if (ftpClient.makedirectory (subdirectory)) {ftpClient.ChangeworkingDirectory (subdirectory); } else {log.getLogger (this.getClass ()). Info ("Buat direktori gagal"); return ftpstatus.create_directory_fail; }} start = end + 1; end = directory.indexof ("/", start); // Periksa apakah semua direktori telah dibuat IF (end <= start) {break; }}}}} // Periksa apakah ada file remote ftpfile [] file = ftpclient.listFiles (remoteFileName); if (file.length == 1) {long remotesize = file [0] .getSize (); File f = file baru (lokal); Long LocalSize = f.length (); if (remoteSize == localSize) {return ftpstatus.file_exits; } else if (remoteSize> localSize) {return ftpstatus.remote_bigger_local; } // Coba pindahkan pointer dalam file untuk mewujudkan gangguan inputStream breakpoint adalah = FileInputStream baru (f); if (is.skip (remotesize) == remotesize) {ftpClient.setrestArtArtOffset (remoteSize); if (ftpclient.storefile (jarak jauh, is)) {return ftpstatus.upload_from_break_success; }} // Jika breakpoint terus berlalu dengan tidak berhasil, hapus file di server dan unggah lagi jika (! Ftpclient.deleteFile (remotefileName)) {return ftpstatus.delete_remote_faild; } adalah = FileInputStream baru (f); if (ftpclient.storefile (jarak jauh, is)) {result = ftpstatus.upload_new_file_success; } else {result = ftpstatus.upload_new_file_failed; } is.close (); } else {inputStream adalah = FileInputStream baru (local); if (ftpclient.storefile (remoteFileName, is)) {result = ftpstatus.upload_new_file_success; } else {result = ftpstatus.upload_new_file_failed; } is.close (); } hasil pengembalian; } / ** * * Putuskan dari server jarak jauh * * @throws IoException * / public void disconnect () melempar ioException {if (ftpClient.isconnected ()) {ftpClient.Disconnect (); }} public static void main (string [] args) {ftputils myftp = new ftputils (); coba {myftp.connect ("192.168.1.200", 21, "Duser", "htpduserxp32"); Log.getLogger (ftputils.class) .info (myftp.upload ("c: //users//administrator//desktop//swing.drawer.jar", "/jars/swing.drawer.jar"))); myftp.disconnect (); } catch (ioException e) {log.getLogger (ftputils.class) .info ("Pengecualian file unggah ftp:" + e.getMessage ()); }}}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.