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.ftp.ftpclient; นำเข้า org.apache.commons.net.ftp.ftpfile; นำเข้า org.apache.commons.net.ftp.ftpreply;/*** ใช้ไฟล์ ftpclient ftpclient การเข้ารหัสสตริงแบบคงที่ส่วนตัว = system.getProperty ("file.encoding"); /** * คำอธิบาย: อัปโหลดไฟล์ไปยังเซิร์ฟเวอร์ ftp * * @ @version1.0 * @param url * ftp server hostname * @param พอร์ต * พอร์ตเซิร์ฟเวอร์ ftp * @param ชื่อผู้ใช้ * ftp login accent * @param password * ftp passwate * @param path * * * * * * * * * * * * * * * * * * * * * * FTP Server * @param อินพุต * สตรีมอินพุตไฟล์ท้องถิ่น * @return return True ประสบความสำเร็จไม่เช่นนั้นจะส่งคืน false */ public boolean boolean uploadfile (url สตริง, พอร์ต int, ชื่อผู้ใช้สตริง, รหัสผ่านสตริง, สายสตริง, สตริงไฟล์, อินพุตอินพุต) ลอง {int ตอบกลับ; // หากคุณใช้พอร์ตเริ่มต้นคุณสามารถเชื่อมต่อโดยตรงกับ FTP Server FTPClient.Connect (URL); // ftp.connect (url, พอร์ต); // เชื่อมต่อเซิร์ฟเวอร์ ftp // เข้าสู่ระบบไปยัง ftpclient.login (ชื่อผู้ใช้, รหัสผ่าน); ftpclient.setControlencoding (การเข้ารหัส); // ตรวจสอบว่าการเชื่อมต่อนั้นสำเร็จตอบกลับ = ftpClient.getReplyCode (); if (! ftpreply.ispositiveCompletion (ตอบกลับ)) {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 ()) {ลอง {ftpClient.disconnect (); } catch (ioexception ioe) {}}} ผลการส่งคืน; }/*** อัปโหลดไฟล์โลคัลไปยังเซิร์ฟเวอร์ ftp**/โมฆะสาธารณะ testuploadFromDisk () {ลอง {fileInputStream ใน = ใหม่ FileInputStream (ไฟล์ใหม่ ("d: /test02/list.txt")); Boolean Flag = UploadFile ("10.0.0.102", 21, "admin", "123456", "/", "lis.txt", ใน); System.out.println (Flag); } catch (filenotfoundException e) {e.printStackTrace (); }} /** * คำอธิบาย: ดาวน์โหลดไฟล์จากเซิร์ฟเวอร์ ftp * * @ @version1.0 * @param url * ftp server hostname * @param พอร์ต * พอร์ตเซิร์ฟเวอร์ ftp * @param username * บัญชีล็อกอิน ftp * @param passwate หลังจากดาวน์โหลด * @return */ public Static boolean downfile (url สตริง, พอร์ต int, string username, รหัสผ่านสตริง, สตริง remotepath, ชื่อไฟล์สตริง, สตริง localpath) {boolean result = false; ลอง {int ตอบกลับ; ftpclient.setControlencoding (การเข้ารหัส); / * * เพื่ออัปโหลดและดาวน์โหลดไฟล์ภาษาจีนบางแห่งแนะนำให้ใช้สองประโยคต่อไปนี้แทน * สตริงใหม่ (remotepath.getBytes (การเข้ารหัส), "ISO-8859-1") การแปลงรหัส * หลังจากการทดสอบไม่สามารถผ่านได้ */// ftpClientConfig conf = ใหม่ ftpClientConfig (ftpClientConfig.syst_nt); // conf.setserverlanguagecode ("zh"); ftpclient.connect (url, พอร์ต); // หากใช้พอร์ตเริ่มต้นคุณสามารถเชื่อมต่อโดยตรงกับเซิร์ฟเวอร์ FTP โดย ftpclient.login (ชื่อผู้ใช้รหัสผ่าน); // เข้าสู่ระบบ // ตั้งค่าประเภทการถ่ายโอนไฟล์เป็น binary ftpclient.setFileType (ftpClient.binary_file_type); // รับรหัสตอบกลับเข้าสู่ระบบ FTP ตอบ = ftpClient.getReplyCode (); // ตรวจสอบว่าการเข้าสู่ระบบนั้นสำเร็จถ้า (! ftpreply.ispositiveCompletion (ตอบกลับ)) {ftpClient.disconnect (); System.err.println ("เซิร์ฟเวอร์ FTP ปฏิเสธการเชื่อมต่อ"); ผลการกลับมา; } // ถ่ายโอนไปยังไดเรกทอรีเซิร์ฟเวอร์ FTP ไปยังไดเรกทอรีที่ระบุ ftpClient.changeWorkingDirectory (สตริงใหม่ (remotepath.getBytes (การเข้ารหัส), "ISO-8859-1")); // รับรายการไฟล์ ftpfile [] fs = ftpClient.listfiles (); สำหรับ (ftpfile ff: fs) {ถ้า (ff.getName (). เท่ากับ (ชื่อไฟล์)) {ไฟล์ localfile = ไฟล์ใหม่ (localPath + "/" + ff.getName ()); OutputStream คือ = ใหม่ fileOutputStream (localFile); ftpClient.RetrieveFile (ff.getName (), IS); is.close (); }} ftpClient.logout (); ผลลัพธ์ = จริง; } catch (ioexception e) {e.printstacktrace (); } ในที่สุด {if (ftpClient.isconnected ()) {ลอง {ftpClient.disconnect (); } catch (ioexception ioe) {}}} ผลการส่งคืน; }/** * ดาวน์โหลดไฟล์บนเซิร์ฟเวอร์ FTP ไปยัง Local * */Public Void TestDownFile () {ลอง {Boolean Flag = DownFile ("10.0.0.102", 21, "Admin", "123456", "/", "ip.txt", "e:/"); System.out.println (Flag); } catch (exception e) {e.printstacktrace (); }} โมฆะคงที่สาธารณะหลัก (สตริง [] args) {ftpapche fa = new ftpapche (); fa.testdownfile (); fa.testuploadfromdisk (); -2. อัปโหลดและดาวน์โหลดโฟลเดอร์ FTP (โปรดทราบว่าโฟลเดอร์ทั้งหมดเป็นโฟลเดอร์ทั้งหมด)
แพ็คเกจ ftp; นำเข้า java.io.bufferedInputStream; นำเข้า java.io.bufferedOutputStream; นำเข้า java.io.file; นำเข้า java.io.fileinputstream นำเข้า java.io.filenotfoundException; org.apache.commons.net.ftp.ftpclient; นำเข้า org.apache.commons.net.ftp.ftpclientconfig; นำเข้า org.apache.commons.net.ftp.ftpfile; นำเข้า org.apache.commons.net.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp. ftpclient ftpclient; แถบสตริงส่วนตัว Intport ส่วนตัว ผู้ใช้สตริงส่วนตัว รหัสผ่านสตริงส่วนตัว Logger Logger แบบคงที่ส่วนตัว = logger.getLogger (ftptest_04.class.getName ()); / * * * ftp constructor */ public ftptest_04 (แถบสตริง, intport int, ผู้ใช้สตริง, รหัสผ่านสตริง) {this.strip = strip; this.intport = intport; this.user = ผู้ใช้; this.password = รหัสผ่าน; this.ftpClient = new ftpClient (); } / *** @return พิจารณาว่าการเข้าสู่ระบบนั้นประสบความสำเร็จ** / บูลีนสาธารณะ ftplogin () {บูลีน islogin = false; ftpClientConfig ftpClientConfig = ใหม่ ftpClientConfig (); ftpClientConfig.setServertimezoneId (Timezone.getDefault (). getId ()); this.ftpclient.setControlencoding ("GBK"); this.ftpclient.configure (ftpClientConfig); ลอง {ถ้า (this.intport> 0) {this.ftpclient.connect (this.strip, this.intport); } else {this.ftpclient.connect (this.strip); } // การเชื่อมต่อเซิร์ฟเวอร์ FTP คำตอบ int ตอบกลับ = this.ftpClient.getReplyCode (); if (! ftpreply.ispositiveCompletion (ตอบกลับ)) {this.ftpclient.disconnect (); logger.error ("เข้าสู่ระบบไปยังบริการ FTP ล้มเหลว!"); return islogin; } this.ftpclient.login (this.user, this.password); // ตั้งค่าโปรโตคอลการส่งข้อมูล this.ftpclient.enterLocalPassiveMode (); this.ftpclient.setFileType (ftpClient.binary_file_type); logger.info ("ขอแสดงความยินดี" + this.user + "ลงชื่อเข้าใช้เซิร์ฟเวอร์ FTP ได้สำเร็จ"); islogin = true; } catch (exception e) {e.printstacktrace (); logger.error (this.user + "เข้าสู่ระบบไปยังบริการ FTP ล้มเหลว!" + e.getMessage ()); } this.ftpclient.setBuffersize (1024 * 2); this.ftpclient.setDatatimeout (30 * 1000); return islogin; }/ *** @exit ปิดการเชื่อมโยงเซิร์ฟเวอร์**/ โมฆะสาธารณะ ftplogout () {ถ้า (null! = this.ftpClient && this.ftpClient.isconnected ()) {ลอง {boolean reuslt = this.ftpClient.logout (); }} catch (ioexception e) {e.printstacktrace (); logger.warn ("ยกเว้นการออกจากเซิร์ฟเวอร์ FTP!" + e.getMessage ()); } ในที่สุด {ลอง {this.ftpclient.disconnect (); // ปิดการเชื่อมต่อกับเซิร์ฟเวอร์ ftp} catch (ioexception e) {e.printstacktrace (); logger.warn ("ยกเว้นการปิดการเชื่อมต่อของเซิร์ฟเวอร์ FTP!"); }}}}}/**** อัปโหลดไฟล์ ftp* @param localfile ไฟล์ท้องถิ่น* @param romotuploadepath อัปโหลดเส้นทางเซิร์ฟเวอร์- ควรลงท้ายด้วย/**/บูลีนสาธารณะ uploadfile (ไฟล์ localfile, สตริง romotuploadepath) ความสำเร็จของบูลีน = FALSE; ลอง {this.ftpclient.changeworkingDirectory (romotuploadepath); // เปลี่ยนเส้นทางการทำงานของโปรแกรม = ใหม่ 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 (stream! = null) {ลอง {stream.close (); } catch (ioexception e) {e.printstacktrace (); }}} ส่งคืนความสำเร็จ; } / **** ดาวน์โหลดไฟล์* @param remotefilename ชื่อไฟล์ที่จะดาวน์โหลด* @param localdires ดาวน์โหลดไปยังเส้นทางท้องถิ่น* @param remotownownloadpath เส้นทางที่ระยะไกล fileName ตั้งอยู่** / public boolean downloadfile (String remotefilename bufferedOutputStream outstream = null; ความสำเร็จของบูลีน = FALSE; ลอง {this.ftpclient.changeworkingDirectory (remotedOwnloadPath); Outstream = ใหม่ bufferedOutputStream (ใหม่ fileOutputStream (strFilePath)); logger.info (remotefilename + "เริ่มดาวน์โหลด ... "); ความสำเร็จ = this.ftpClient.retrieveFile (RemoteFileName, Outstream); if (success == true) {logger.info (RemoteFileName + "ดาวน์โหลดได้สำเร็จเป็น" + strfilepath); กลับมาประสบความสำเร็จ }} catch (exception e) {e.printstacktrace (); logger.error (remotefilename + "ดาวน์โหลดล้มเหลว"); } ในที่สุด {ถ้า (null! = Outstream) {ลอง {outstream.flush (); Outstream.close (); } catch (ioexception e) {e.printstacktrace (); }}} if (success == false) {logger.error (remotefilename + "ดาวน์โหลดล้มเหลว !!!"); } คืนความสำเร็จ; }/**** @upload โฟลเดอร์* @param localDirectory* โฟลเดอร์ท้องถิ่น* @param remotedirectorypath* เส้นทางเซิร์ฟเวอร์ ftp ลงท้ายด้วยไดเรกทอรี "/"**/บูลีนสาธารณะ uploadDirectory (สตริง localDirectory ลอง {remotedirectoryPath = remotedirectorypath + src.getName () + "/"; บูลีน 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 (remotedirectorypath + "การสร้างไดเรกทอรีล้มเหลว"); } ไฟล์ [] allfile = src.listfiles (); สำหรับ (int currentfile = 0; currentfile <allfile.length; currentfile ++) {ถ้า (! allfile [currentfile] .isdirectory ()) {string srcname = allfile [currentfile] .getPath (). toString (); uploadfile (ไฟล์ใหม่ (srcname), remotedirectorypath); }} สำหรับ (int currentfile = 0; currentfile <allfile.length; currentfile ++) {ถ้า (allfile [currentfile] .isdirectory ()) {// uploaddirectory (allfile [currentfile] .getPath () }} ส่งคืนจริง; } / **** โฟลเดอร์ @download* @param localDirectoryPath ที่อยู่ท้องถิ่น* @param remotedirectory โฟลเดอร์ระยะไกล** / public boolean downloadDirectory (สตริง localDirectoryPath, สตริง remoteRectory) {ลอง {String filename = ไฟล์ใหม่ (remotedirectory) localDirectoryPath = localDirectoryPath + ชื่อไฟล์ + "//"; ไฟล์ใหม่ (LocalDirectoryPath) .MkDirs (); ftpfile [] allfile = this.ftpclient.listfiles (remotedirectory); สำหรับ (int currentfile = 0; currentfile <allfile.length; currentfile ++) {ถ้า (! allfile [currentfile] .isdirectory ()) {downloadfile (allfile [currentfile] .getName (), localDirectoryPath }} สำหรับ (int currentfile = 0; currentfile <allfile.length; currentfile ++) {ถ้า (allfile [currentfile] .isdirectory ()) {String strremotedirectorypath = remotedirectory+ "/" Allfile [currentFile] DownloadDirectory (LocalDirectoryPath, StrremotedirectoryPath); }}} catch (ioexception e) {e.printstacktrace (); logger.info ("ดาวน์โหลดโฟลเดอร์ล้มเหลว"); กลับเท็จ; } return true; } // ftpClient set และรับฟังก์ชั่นสาธารณะ ftpClient getftpClient () {return ftpClient; } โมฆะสาธารณะ setftpClient (ftpClient ftpClient) {this.ftpClient = ftpClient; } โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น IOException {ftptest_04 ftp = new ftptest_04 ("10.0.0.102", 21, "ผู้ดูแลระบบ", "123456"); ftp.ftplogin (); System.out.println ("1"); // อัปโหลดโฟลเดอร์บูลีน UploadFlag = ftp.uploadDirectory ("D: // test02", "/"); // ถ้าเป็นผู้ดูแลระบบ/ไฟล์ทั้งหมดจะถูกส่งผ่าน หากเป็นเพียงแค่/จะส่งผ่านไปยังโฟลเดอร์ System.out.println ("UploadFlag:" + UploadFlag); // ดาวน์โหลดโฟลเดอร์ ftp.downloaddirectory ("d: // tm", "/"); ftp.ftplogout (); -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่าเนื้อหาของบทความนี้จะช่วยในการศึกษาหรือทำงานของทุกคน ฉันหวังว่าจะสนับสนุน Wulin.com เพิ่มเติม!