FTP هو اختصار اللغة الإنجليزية لبروتوكول نقل الملفات (بروتوكول نقل الملفات) ، ويسمى الاختصار الصيني "بروتوكول النقل المكتوب". تستخدم لنقل ملفات التحكم ثنائية الاتجاه على الإنترنت. في الوقت نفسه ، إنه أيضًا تطبيق. هناك تطبيقات FTP مختلفة تعتمد على أنظمة تشغيل مختلفة ، وكل هذه التطبيقات تلتزم بنفس البروتوكول لنقل الملفات. في استخدام FTP ، غالبًا ما يواجه المستخدمون مفهومين: "تنزيل" و "تحميل". تعني ملفات "تنزيل" نسخ الملفات من المضيف عن بُعد إلى جهاز الكمبيوتر الخاص بك ؛ تعني ملفات "التحميل" نسخ الملفات من جهاز الكمبيوتر الخاص بك إلى المضيف البعيد. في لغة الإنترنت ، يمكن للمستخدمين تحميل (تنزيل) الملفات إلى (من) المضيفين عن بُعد من خلال برامج العميل.
أولاً ، تم تنزيل Serv-U لإعداد جهاز الكمبيوتر الخاص بك كخادم ملفات FTP لسهولة التشغيل. يتم استخدام الرمز التالي في خادم FTP ويجب كتابة البيانات ذات الصلة لاتصال FTP في الكود لإكماله.
1. تحميل وتنزيل ملفات FTP (لاحظ أنه يتم تحميل وتنزيل الملفات الفردية)
استيراد java.io.file ؛ استيراد java.io.fileInputStream ؛ استيراد java.io.filenotfoundexception ؛ استيراد java.io.fileOutputStream ؛ استيراد java.io.ioException ؛ استيراد java.io.inputstream org.apache.commons.net.ftpclient ؛ import org.apache.commons.net.ftp.ftpfile ؛ import org.apache.commons.net.ftp.ftpreply ؛/*** expload ftp upload and file download*/public class ftpapche ترميز سلسلة ثابتة خاصة = system.getProperty ("file.encoding") ؛ /** * الوصف: تحميل الملفات إلى خادم FTP * * @ @version1.0 * param url * ftp server hostname * param port * ftp server port * @param username * ftp login accoun FTP Server * @Param Inport * دفق إدخال الملف المحلي * RETURN RETRE TRUE بنجاح ، وإلا فإنه سيُرجع FALSE */ Public Static Boolean UploadFile (url url string ، منفذ int ، اسم مستخدم السلسلة ، كلمة مرور السلسلة ، مسار السلسلة ، اسم ملف السلسلة ، إدخال inputStream) {boolean result = false ؛ جرب {int reply ؛ // إذا كنت تستخدم المنفذ الافتراضي ، فيمكنك الاتصال مباشرة بخادم FTP FTPClient.connect (url) ؛ // ftp.connect (url ، port) ؛ // قم بتوصيل خادم FTP // تسجيل الدخول إلى ftpclient.login (اسم المستخدم ، كلمة المرور) ؛ ftpclient.setControlencoding (الترميز) ؛ // تحقق من أن الاتصال هو الرد الناجح = ftpClient.getReplyCode () ؛ if (! ftpreply.ispositivecOption (الرد)) {system.out.println ("فشل الاتصال") ؛ ftpclient.disconnect () ؛ نتيجة العودة } // نقل دليل العمل إلى تغيير منطقي الدليل المحدد = ftpClient.ChangeWorkingDirectory (path) ؛ ftpclient.setfiletype (ftp.binary_file_type) ؛ if (تغيير) {result = ftpclient.storefile (سلسلة جديدة (filename.getBytes (الترميز) ، "ISO-8859-1") ، الإدخال) ؛ if (النتيجة) {system.out.println ("تم تحميله بنجاح!") ؛ }} input.close () ؛ ftpclient.logout () ؛ } catch (ioException e) {E.PrintStackTrace () ؛ } أخيرًا {if (ftpclient.isconnected ()) {try {ftpclient.disconnect () ؛ } catch (ioException ioe) {}}} نتيجة الإرجاع ؛ }/*** قم بتحميل الملفات المحلية إلى خادم FTP**/public void testUploadFromDisk () {try {fileInputStream in = new FileInputStream (ملف جديد ("D: /Test02/List.txt")) ؛ flag boolean = uploadfile ("10.0.0.102" ، 21 ، "admin" ، "123456" ، "/" ، "lis.txt" ، in) ؛ system.out.println (flag) ؛ } catch (fileNotFoundException e) {e.printStackTrace () ؛ } ستر محلي بعد التنزيل * @RETURN */ Public Static Boolean DownFile (url url string ، منفذ int ، اسم المستخدم ، كلمة مرور السلسلة ، سلسلة Remotepath ، اسم ملف السلسلة ، String LocalPath) {boolean result = false ؛ جرب {int reply ؛ ftpclient.setControlencoding (الترميز) ؛ / * * من أجل تحميل الملفات الصينية وتنزيلها ، توصي بعض الأماكن باستخدام الجملتين التاليتين بدلاً من * سلسلة جديدة (remotepath.getBytes (الترميز) ، "ISO-8859-1"). * بعد الاختبار ، لا يمكن تمريره. */// ftpclientconfig conf = new ftpClientConfig (ftpClientConfig.syst_nt) ؛ // conf.SetServerLanguageCode ("ZH") ؛ ftpclient.connect (url ، port) ؛ // إذا تم استخدام المنفذ الافتراضي ، يمكنك الاتصال مباشرة بخادم FTP بواسطة ftpclient.login (اسم المستخدم ، كلمة المرور) ؛ // تسجيل الدخول // تعيين نوع نقل الملف إلى ftpclient.setfiletype ثنائي (ftpclient.binary_file_type) ؛ // احصل على رمز استجابة تسجيل الدخول FTP = ftpclient.getReplyCode () ؛ // تحقق من أن تسجيل الدخول ناجح إذا (! ftpreply.ispositivecompletion (الرد)) {ftpclient.disconnect () ؛ System.err.println ("رفض خادم FTP الاتصال.") ؛ نتيجة العودة }. // احصل على قائمة الملفات ftpfile [] fs = ftpclient.listfiles () ؛ لـ (ftpfile ff: fs) {if (ff.getName (). equals (filename)) {file localfile = new file (localPath + "/" + ff.getName ()) ؛ OutputStream هو = fileOutputStream جديد (LocalFile) ؛ ftpClient.RetRieveFile (ff.getName () ، IS) ؛ is.close () ؛ }} ftpclient.logout () ؛ النتيجة = صواب ؛ } catch (ioException e) {E.PrintStackTrace () ؛ } أخيرًا {if (ftpclient.isconnected ()) {try {ftpclient.disconnect () ؛ } catch (ioException ioe) {}}} نتيجة الإرجاع ؛ }/** * قم بتنزيل الملف على خادم FTP إلى المحلي *//public void testDownFile () {try {boolean flag = downfile ("10.0.0.102" ، 21 ، "admin" ، "123456" ، "/" ، "ip.txt" ، "e:/") ؛ system.out.println (flag) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }} public static void main (string [] args) {ftpapche fa = new ftpapche () ؛ fa.testDownFile () ؛ fa.testuploadfromDisk () ؛ }}2. تحميل وتنزيل مجلدات FTP (لاحظ أن المجلد بأكمله هو المجلد بأكمله)
package ftp ؛ import java.io.bufferedInputStream ؛ import java.io.bufferedoutputstream ؛ import java.io.file ؛ import java.io.fileInputStream ؛ import java.io.io.filenotfoundexception ؛ import java.fileouttream ؛ import java.ioe. org.apache.commons.net.ftpclient ؛ import org.apache.commons.net.ftp.ftpclientconfig ؛ import org.apache.commons.net.ftp.ftpfile ؛ import org.acmomon.net.ftp.ftpreply ؛ express.log.logger ؛ ftpclient ftpclient. قطاع السلسلة الخاص Private intport ؛ مستخدم سلسلة خاصة ؛ كلمة مرور السلسلة الخاصة ؛ logger static private = logger.getLogger (ftptest_04.class.getName ()) ؛ / * * * ftp constructor */ public ftptest_04 (String stric ، intport ، string user ، string password) {this.strip = strip ؛ this.intport = inTport ؛ this.user = المستخدم ؛ this.password = كلمة المرور ؛ this.ftpClient = جديد ftpclient () ؛ } / *** return تحديد ما إذا كان تسجيل الدخول ناجحًا** / public boolean ftplogin () {boolean islogin = false ؛ ftpClientConfig ftpClientConfig = جديد ftpClientConfig () ؛ ftpClientConfig.SetServertimeZoneid (timezone.getDefault (). getId ()) ؛ this.ftpclient.setControlenCoding ("GBK") ؛ this.ftpclient.configure (ftpClientConfig) ؛ حاول {if (this.intport> 0) {this.ftpclient.connect (this.strip ، this.intport) ؛ } آخر {this.ftpclient.connect (this.strip) ؛ }. if (! ftpreply.ispositivecOption (الرد)) {this.ftpclient.disconnect () ؛ logger.error ("فشل تسجيل الدخول إلى خدمة FTP!") ؛ إرجاع Islogin. } this.ftpclient.login (this.user ، this.password) ؛ // قم بتعيين بروتوكول الإرسال this.ftpclient.enterlocalPassiveMode () ؛ this.ftpclient.setfiletype (ftpclient.binary_file_type) ؛ logger.info ("تهانينا" + this.user + "قام بنجاح بتسجيل الدخول إلى خادم FTP") ؛ Islogin = صحيح ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ LOGGER.ERROR (this.user + "Login to the FTP فشل!" + E.GetMessage ()) ؛ } this.ftpclient.setBuffersize (1024 * 2) ؛ this.ftpclient.setDatatimeout (30 * 1000) ؛ إرجاع Islogin. }/ *** exit ارتباط الخادم**/ public void ftplogout () {if (null! = this.ftpclient && this.ftpclient.isconnection ()) {try {boolean reuslt = that.ftpclient.logout () ؛ // exit ftp server if (reuslt) }} catch (ioException e) {E.PrintStackTrace () ؛ logger.warn ("استثناء من الخروج من خادم FTP!" + E.GetMessage ()) ؛ } أخيرًا {try {this.ftpclient.disconnect () ؛ // أغلق الاتصال بخادم ftp} catch (ioException e) {eprintstacktrace () ؛ logger.warn ("استثناء من إغلاق اتصال خادم FTP!") ؛ }}}}}/**** قم بتحميل ملف ftp* param localfile file local* param romotuploadepath path- يجب أن ينتهي مع/*/public boolean uploadfile (file localfile ، string romotuadepath) {bufferedInputStream instrand ؛ نجاح منطقي = خطأ ؛ جرب {this.ftpclient.ChangeWorkingDirectory (romotuploadepath) ؛ // تغيير مسار العمل instream = جديد bufferedInputStream (جديد fileInputStream (localfile)) ؛ logger.info (localfile.getName () + "بدء تحميل ...") ؛ النجاح = this.ftpclient.storefile (localfile.getName () ، instream) ؛ if (success == true) {logger.info (localfile.getName () + "تم تحميله بنجاح") ؛ العودة النجاح }} catch (fileNotfoundException e) {e.printStackTrace () ؛ logger.error (localfile + "غير موجود") ؛ } catch (ioException e) {E.PrintStackTrace () ؛ } أخيرًا {if (instream! = null) {try {instream.close () ؛ } catch (ioException e) {E.PrintStackTrace () ؛ }}} نجاح الإرجاع ؛ } / **** تنزيل ملف* param remoteFilename اسم الملف ليتم تنزيله* param localdires تنزيل إلى المسار المحلي* param remoteDownload the path حيث يوجد RemoteFiLename** / public boolen downloadFile (string remotefilename ، string localdires ، string remoteDownloadPath) BufferedOutputStream OutStream = NULL ؛ نجاح منطقي = خطأ ؛ حاول {this.ftpClient.ChangeWorkingDirectory (RemotedOwnloadPath) ؛ Outstream = جديد bufferedoutputstream (FileOutputStream جديد (strfilepath)) ؛ logger.info (RemoteFilename + "ابدأ التنزيل ...") ؛ النجاح = this.ftpClient.RetRieveFile (RemoteFilename ، OutStream) ؛ if (success == true) {logger.info (RemoteFilename + "تم تنزيله بنجاح إلى" + strfilepath) ؛ العودة النجاح }} catch (استثناء e) {E.PrintStackTrace () ؛ logger.error (RemoteFilename + "Download FAILLED") ؛ } أخيرًا {if (null! = OutStream) {try {overstream.flush () ؛ Outstream.close () ؛ } catch (ioException e) {E.PrintStackTrace () ؛ }}} if (success == false) {logger.error (remoteFilename + "Download failt !!!") ؛ } نجاح العودة ؛ }/**** upload Folder* param localdirectory* المجلد المحلي* param remotedirectorypath* ينتهي مسار خادم FTP مع الدليل "/"*//public boolean uploaddirectory (String localdiroctory ، remotedirectorypath) حاول {remotedirectorypath = remotedirectorypath + src.getName () + "/" ؛ Boolean makedirflag = this.ftpclient.makedirectory (Remotedirectorypath) ؛ System.out.println ("LocalDirectory:" + localDirectory) ؛ System.out.println ("Remotedirectorypath:" + RemotedirectoryPath) ؛ System.out.println ("src.getName ():" + src.getName ()) ؛ System.out.println ("Remotedirectorypath:" + RemotedirectoryPath) ؛ System.out.println ("MakedIrflag:" + MakedirFlag) ؛ // ftpclient.listDirectories () ؛ } catch (ioException e) {E.PrintStackTrace () ؛ logger.info (فشل إنشاء الدليل عن quotedirectorypath + ") ؛ } file [] allfile = src.listfiles () ؛ لـ (int currentfile = 0 ؛ currentfile <allfile.length ؛ currentfile ++) {if (! allfile [currentfile] .isdirectory ()) {String srcName = allfile [currentfile] .getPath (). toString () ؛ UploadFile (ملف جديد (srcname) ، remotedirectorypath) ؛ }} لـ (int currentfile = 0 ؛ currentfile <allfile.length ؛ currentfile ++) {if (allfile [currentfile] .isdirectory ()) {// exploaddirectory المتكرر (allfile [currentfile] .getPath (). }} إرجاع صحيح ؛ } / **** مجلد Townload* param localdirectorypath العنوان المحلي* param remotedirectory المجلد البعيد** / / public boolean downloadDirectory (String localDirectoryPath ، String Remotedirectory) {try {String fileDame = file new (file remotedirectory) .getName () ؛ localDirectoryPath = localDirectoryPath + filename + "//" ؛ ملف جديد (localDirectoryPath) .mkdirs () ؛ ftpfile [] allfile = that.ftpclient.listfiles (Remotedirectory) ؛ لـ (int currentfile = 0 ؛ currentfile <allfile.length ؛ currentfile ++) {if (! allfile [currentfile] .isdirectory ()) {downloadFile (allfile [currentfile] .getName () ، localdirectorypath ، demootedirectory) ؛ }} لـ (int currentfile = 0 ؛ currentfile <allfile.length ؛ currentfile ++) {if (allfile [currentfile] .isdirectory ()) {String strremotedirectorypath = remotedirectory+ "/"+ allfile [currentfile] .getName () ؛ DownloadDirectory (LocalDirectoryPath ، strremotedirectorypath) ؛ }}} catch (ioException e) {E.PrintStackTrace () ؛ logger.info ("فشل تنزيل المجلد") ؛ العودة كاذبة } إعادة صواب ؛ } // ftpclient set واحصل على وظائف FTPCLIENT GETFTPCLIENT () {return ftpclient ؛ } public void setftpclient (ftpclient ftpclient) {this.ftpclient = ftpClient ؛ } static void main (string [] args) يلقي ioException {ftptest_04 ftp = new ftptest_04 ("10.0.0.102" ، 21 ، "admin" ، "123456") ؛ ftp.ftplogin () ؛ System.out.println ("1") ؛ // تحميل مجلد Boolean UploadFlag = ftp.uploaddirectory ("d: // test02" ، "/") ؛ // إذا كان المسؤول/، فسيتم تمرير جميع الملفات. إذا كان الأمر مجرد/، فسيتم تمريره على نظام المجلد. // تنزيل مجلد ftp.downloadDirectory ("d: // tm" ، "/") ؛ ftp.ftplogout () ؛ }}ما سبق هو كل محتوى هذه المقالة. آمل أن يكون محتوى هذه المقالة من بعض المساعدة في دراسة أو عمل الجميع. آمل أيضًا دعم wulin.com أكثر!