Apache FTP هي أداة عميل تحميل FTP المستخدمة على نطاق واسع. من السهل تشغيله ، وله رمز بسيط وبنية واضحة. إنها أولوية لبرنامج إدارة عميل ملف FTP. تتضمن عمليات FTP: تحميل ملف FTP (ناقل الحركة المستمر) ، تنزيل ملف FTP ، إعادة تسمية ملفات FTP ، وحذف ملف FTP. أظهرت هذه العمليات بالكامل طريقة إدارة تطبيقات FTP. لذلك استخدمت دائمًا هذه الطريقة لتنفيذ إدارة خوادم ملفات FTP ؛ مرفق مع رمز أداة FTP.
1. فئة تعداد حالة تشغيل ملف FTP
حزمة com.scengine.wtms.utils.ftp ؛ التعداد العام ftpStatus {file_exits (0) ، create_directory_success (1) ، create_directory_fail (2) ، epload_from_break_success (3) ، upload_from_berk_faild (4) ، download_from_break_scess (5) Upload_new_file_success (7) ، Upload_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) ؛ حالة int الخاصة ؛ public int getStatus () {return status ؛ } public void setStatus (int status) {this.status = status ؛ } ftpstatus (int status) {this.status = status ؛ }} 2. رمز أداة تشغيل ملف FTP
حزمة com.scengine.wtms.utils.ftp ؛ استيراد java.io.file ؛ استيراد java.io.fileInputStream ؛ استيراد java.io.fileOutputStream ؛ استيراد java.io.ioException ؛ استيراد java.io.inputstream ؛ استيراد java.io.outputStream ؛ استيراد java.io.outputStream ؛ استيراد java.io.printwriter ؛ استيراد javax.servlet.http.httpservletresponse ؛ استيراد org.apache.commons.net.printCommandListener ؛ استيراد org.apache.commons.net.ftp.ftp ؛ استيراد org.apache.commons.net.ftp.ftpclient ؛ استيراد org.apache.commons.net.ftp.ftpfile ؛ استيراد org.apache.commons.net.ftp.ftpreply ؛ استيراد com.scengine.wtms.utils.log ؛ الفئة العامة ftputils {private ftpclient ftpclient = new ftpclient () ؛ / *** إعدادات إنشاء الكائنات إخراج الأوامر المستخدمة في العملية إلى وحدة التحكم*/ public ftputils () {this.ftpclient.addprotocolcommandlistener (New PrintCommandListener (new printwriter (system.out))) ؛ } / ** * * يستخدم للاتصال بخادم FTP في Java Programming * * param hostname * hostName * * param port * port * * param username * username * * param password * password * usporturn usern ، usexpishing ، ftpclient.connect (اسم المضيف ، المنفذ) ؛ if (ftpreply.ispositivecompletion (ftpClient.getReplyCode ()))) {if (ftpclient.login (اسم المستخدم ، كلمة المرور)) {return true ؛ }} disconnect () ؛ العودة كاذبة } / ** * حذف ملف ftp عن بعد * * param Remote * مسار الملف البعيد * return * throws ioException * / public ftpstatus delete (سلسلة عن بُعد) رمي ioException {ftpclient.enterlocalpassivemode () ؛ ftpclient.setfiletype (ftp.binary_file_type) ؛ نتيجة FTPSTATUS = فارغة ؛ ftpfile [] files = ftpclient.listfiles (Remote) ؛ if (files.length == 1) {boolean status = ftpclient.deletefile (Remote) ؛ النتيجة = الحالة؟ ftpstatus.delete_remote_success: ftpstatus.delete_remote_faild ؛ } آخر {result = ftpstatus.not_exist_file ؛ } log.getLogger (this.getClass ()). info ("معرف حذف ملف خادم FTP:"+result) ؛ نتيجة العودة } / ** * أعد تسمية ملف FTP عن بُعد * * param الاسم * اسم الملف البعيد الجديد (يجب أن يكون المسار-مستدعي أن يكون ضمن نفس المسار) * * param عن بُعد * مسار الملف البعيد * * * return إذا كان ناجحًا * * throws ioexception * / public ftpstatus إعادة تسمية السلسلة ، string remote) throws ioexception ftpclient.setfiletype (ftp.binary_file_type) ؛ نتيجة FTPSTATUS = فارغة ؛ ftpfile [] files = ftpclient.listfiles (Remote) ؛ if (files.length == 1) {boolean status = ftpclient.rename (Remote ، name) ؛ النتيجة = الحالة؟ ftpstatus.remote_rename_success: ftpstatus.remote_rename_faild ؛ } آخر {result = ftpstatus.not_exist_file ؛ } log.getLogger (this.getClass ()). info ("معرف تحديث FILENAME FILENAME FTP:"+result) ؛ نتيجة العودة ستر الملف المنبثق Save Path Select Window Response.setContentType ("Application/Octet-tream") ؛ استجابة. استجابة. ftpclient.enterlocalPassiveMode () ؛ ftpclient.setfiletype (ftp.binary_file_type) ؛ نتيجة FTPSTATUS ؛ outputStream Out = response.getOutputStream () ؛ حالة منطقية = ftpClient.RetRieveFile (عن بعد ، خارج) ؛ النتيجة = الحالة؟ ftpstatus.download_from_break_success: ftpstatus.download_from_break_faild ؛ log.getLogger (this.getClass ()). info ("معرف تنزيل ملف خادم FTP:"+result) ؛ out.close () ؛ نتيجة العودة } / ** * تنزيل الملف من FTP Server * * param Remote * مسار الملف البعيد * * param محلي * مسار الملف المحلي * * return ما إذا كان ناجحًا * * throws ioException * / suppressWarnings ("Resource") public ftpstat download (string remote) throws ioxpection {ftpclient ftpclient.setfiletype (ftp.binary_file_type) ؛ نتيجة FTPSTATUS ؛ ملف f = ملف جديد (محلي) ؛ ftpfile [] files = ftpclient.listfiles (Remote) ؛ if (files.length! = 1) {log.getLogger (this.getClass ()). info ("الملف البعيد غير فريد") ؛ إرجاع ftpstatus.file_not_unique ؛ } longotesize long = files [0] .getSize () ؛ if (f.exists ()) {outputStream Out = newOutputStream (f ، true) ؛ log.getLogger (this.getClass ()). info ("حجم الملف المحلي هو:" + f.length ()) ؛ if (f.length ()> = lremotesize) {log.getLogger (this.getClass ()). info ("حجم الملف المحلي أكبر من حجم الملف البعيد ، تنزيل الإجهاض") ؛ إرجاع ftpstatus.remote_smaller_local ؛ } ftpClient.setRestArtoffset (f.length ()) ؛ حالة منطقية = ftpClient.RetRieveFile (عن بعد ، خارج) ؛ النتيجة = الحالة؟ ftpstatus.download_from_break_success: ftpstatus.download_from_break_faild ؛ out.close () ؛ } آخر {outputStream Out = newOutputStream (f) ؛ حالة منطقية = ftpClient.RetRieveFile (عن بعد ، خارج) ؛ النتيجة = الحالة؟ ftpstatus.download_from_break_success: ftpstatus.download_from_break_faild ؛ out.close () ؛ } نتيجة الإرجاع ؛ }/** * تحميل ملف إلى خادم FTP ، دعم نقطة توقف مستمر * * param محلي * اسم الملف المحلي ، مسار المطلق * * param عن بعد * مسار الملف البعيد ، استخدم/home/directory1/subdirectory/file.ex * throws ioException */ suppressWarnings ("Resource") Public FtpStatus Upload (String Local ، String Remote) يلقي ioException {// set passivemode لنقل ftpclient.enterlocalpassivemode () ؛ // تعيين لنقل ftpclient.setFiletype (ftp.binary_file_type) ؛ نتيجة FTPSTATUS ؛ // معالجة سلسلة الدلائل عن بُعد RemoteFilename = Remote ؛ if (remote.contains ("/")) {remoteFilename = remote.substring (remote.lastindexof ("/") + 1) ؛ دليل السلسلة = remote.substring (0 ، remote.lastindexof ("/") + 1) ؛ if (! directory.equalsignorecase ("/") &&! int end = 0 ؛ if (directory.startswith ("/"))) {start = 1 ؛ } آخر {start = 0 ؛ } end = directory.indexof ("/" ، start) ؛ بينما (صحيح) {String subdirectory = remote.subString (start ، end) ؛ if (! ftpClient.ChangeWorkingDirectory (subdirectory)) {if (ftpclient.makedirectory (subdirectory)) {ftpClient.ChangeWorkingDirectory (subdirectory) ؛ } آخر {log.getLogger (this.getClass ()). info ("فشل إنشاء دليل") ؛ إرجاع ftpstatus.create_directory_fail ؛ }} start = end + 1 ؛ end = directory.indexof ("/" ، start) ؛ // تحقق مما إذا كانت جميع الدلائل قد تم إنشاؤها إذا (end <= start) {break ؛ }}}}} // تحقق مما إذا كان هناك ملف ftpfile عن بعد = ftpclient.listfiles (RemoteFilename) ؛ if (files.length == 1) {long remotesize = files [0] .getSize () ؛ ملف f = ملف جديد (محلي) ؛ long alcalsize = f.length () ؛ if (remotesize == lalalSize) {return ftpstatus.file_exits ؛ } آخر إذا (remotesize> adoldsize) {return ftpstatus.remote_bigger_local ؛ } // حاول نقل المؤشر في الملف لتحقيق انقطاع إدخال نقطة الإيقاف هو = جديد fileInputStream (f) ؛ if (is.skip (Remotesize) == Remotesize) {ftpClient.setRestArtAffset (Remotesize) ؛ if (ftpclient.storefile (عن بعد ، هو)) {return ftpstatus.upload_from_break_success ؛ }} // إذا استمرت نقطة التوقف في المرور دون جدوى ، فقم بحذف الملف على الخادم وتحميله مرة أخرى إذا (! ftpclient.deletefile (RemoteFilename)) {return ftpstatus.delete_remote_faild ؛ } هو = جديد fileInputStream (f) ؛ if (ftpclient.storefile (عن بعد ، هو)) {result = ftpstatus.upload_new_file_success ؛ } آخر {result = ftpstatus.upload_new_file_failed ؛ } is.close () ؛ } آخر {inputStream هو = جديد fileInputStream (محلي) ؛ if (ftpclient.storefile (RemoteFilename ، IS)) {result = ftpstatus.upload_new_file_success ؛ } آخر {result = ftpstatus.upload_new_file_failed ؛ } is.close () ؛ } نتيجة الإرجاع ؛ } / ** * * افصل من الخادم البعيد * * throws ioException * / public void disconnect () remrows ioException {if (ftpclient.isconnected ()) {ftpclient.disconnect () ؛ }} public static void main (string [] args) {ftputils myftp = new ftputils () ؛ حاول {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 ("FTP Upload File استثناء:" + E.GetMessage ()) ؛ }}}ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.