استنادًا إلى Apache FTP ، يجب مراعاة المشكلات التالية عند تحميل الملفات (المثال هو وظيفة الاستمرار):
(1) ما إذا كان خادم FTP لديه تغيير في الدليل ، إذا لم يكن موجودًا ، فأنت بحاجة إلى إنشاء دليل.
(2) تحديد ما إذا كان الملف الذي تم تحميله موجودًا بالفعل. إذا كانت موجودة ، فهل يجب حذفها قبل التحميل أو الإرسال المستمر.
1. تحميل أو تنزيل فئة التعداد:
حزمة com.scengine.wtms.utils.ftp ؛ التعداد العام eploadStatus {file_exits (0) ، create_directory_success (1) ، create_directory_fail (2) ، upload_from_break_success (3) ، upload_from_break_faild (4) ، download_from_break_sucess (5) expload_new_file_success (7) ، Upload_new_file_failed (8) ، delete_remote_success (9) ، delete_remote_faild (10) ، remote_bigger_local (11) ، remote_smaller_locall (12) ؛ حالة int الخاصة ؛ public int getStatus () {return status ؛ } public void setStatus (int status) {this.status = status ؛ } UploadStatus (int status) {this.status = status ؛ }}2. رمز الأداة:
حزمة 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.printwriter ؛ استيراد 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 ؛ class class {private ftpclient ftpclient = new ftpclient () ؛ / *** إعدادات إنشاء الكائنات إخراج الأوامر المستخدمة في العملية إلى وحدة التحكم*/ public stiteftp () {this.ftpclient.addprotocolcommandlistener (new printCommandListener (new printwriter (system.out))) ؛ } / ** * * * تستخدم للاتصال بخادم FTP في برمجة Java * * param hostname * assname * param port * port * param username * username * param password * password * @usern usern as the that the that eox passion) ftpclient.connect (اسم المضيف ، المنفذ) ؛ if (ftpreply.ispositivecompletion (ftpClient.getReplyCode ()))) {if (ftpclient.login (اسم المستخدم ، كلمة المرور)) {return true ؛ }} disconnect () ؛ العودة كاذبة } / ** * * تنزيل الملف من خادم FTP * * param Remote * مسار الملف عن بعد * * param محلي * مسار الملف المحلي * * return ما إذا كان ناجحًا * * throws ioException * / suppressWarnings ("Resource") boolean public (remote remote ، string local) throws ioexception ftpclient.setfiletype (ftp.binary_file_type) ؛ نتيجة منطقية ملف f = ملف جديد (محلي) ؛ ftpfile [] files = ftpclient.listfiles (Remote) ؛ if (files.length! = 1) {system.out.println ("الملف البعيد غير فريد") ؛ العودة كاذبة } longotesize long = files [0] .getSize () ؛ if (f.exists ()) {outputStream Out = newOutputStream (f ، true) ؛ System.out.println ("حجم الملف المحلي هو:" + f.length ()) ؛ if (f.length ()> = lremotesize) {system.out.println ("حجم الملف المحلي أكبر من حجم الملف البعيد ، تنزيل مجهض") ؛ العودة كاذبة } ftpClient.setRestArtoffset (f.length ()) ؛ النتيجة = ftpClient.RetRieveFile (عن بعد ، خارج) ؛ out.close () ؛ } آخر {outputStream Out = newOutputStream (f) ؛ النتيجة = ftpClient.RetRieveFile (عن بعد ، خارج) ؛ out.close () ؛ } نتيجة الإرجاع ؛ ستر throws ioException */ suppressWarnings ("Resource") Public UploadStatus (String Local ، String Remote) يلقي ioException {// set passivemode لنقل ftpclient.enterlocalpassivemode () ؛ // set ftpclient.setfiletype (ftp.binary_file_type) ؛ نتيجة التحميل ؛ // معالجة سلسلة الدلائل عن بُعد 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) ؛ } آخر {system.out.println ("فشل إنشاء دليل") ؛ إرجاع exploadstatus.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 == adalsize) {return UploadStatus.file_exits ؛ } آخر إذا (remotesize> adoldsize) {return UploadStatus.Remote_Bigger_Local ؛ } // حاول نقل المؤشر في الملف لتحقيق انقطاع إدخال نقطة الإيقاف هو = جديد fileInputStream (f) ؛ if (is.skip (Remotesize) == Remotesize) {ftpClient.setRestArtAffset (Remotesize) ؛ if (ftpclient.storefile (عن بعد ، هو)) {return uploadstatus.upload_from_break_success ؛ }} // إذا لم تنجح انقطاع نقطة الإيقاف ، فقم بحذف الملف على الخادم وتحميله مرة أخرى إذا (! ftpclient.deletefile (RemoteFilename)) {return eploadstats.delete_remote_faild ؛ } هو = جديد fileInputStream (f) ؛ if (ftpclient.storefile (عن بعد ، هو)) {result = uploadstatus.upload_new_file_success ؛ } آخر {result = uploadstatus.upload_new_file_failed ؛ } is.close () ؛ } آخر {inputStream هو = جديد fileInputStream (محلي) ؛ if (ftpClient.storefile (RemoteFilename ، IS)) {result = eploadStatus.upload_new_file_success ؛ } آخر {result = uploadstatus.upload_new_file_failed ؛ } is.close () ؛ } نتيجة الإرجاع ؛ } / ** * * افصل من الخادم البعيد * * throws ioException * / public void disconnect () remrows ioException {if (ftpclient.isconnected ()) {ftpclient.disconnect () ؛ }} public static void main (string [] args) {conterftp myftp = new estelptfp () ؛ حاول {myftp.connect ("192.168.1.200" ، 21 ، "duser" ، "htpduserxp32") ؛ system.out.println (myftp.upload ("c: //users//administrator//desktop//swing.drawer.jar" ، "/jars/swing.drawer.jar")) ؛ myftp.disconnect () ؛ } catch (ioException e) {system.out.println ("خطأ في الاتصال بـ ftp:" + e.getMessage ()) ؛ }}} ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.