Berdasarkan Apache FTP, masalah -masalah berikut perlu dipertimbangkan saat mengunggah file (contohnya adalah fungsi kelanjutan):
(1) Apakah server FTP memiliki perubahan direktori, jika tidak ada, Anda perlu membuat direktori.
(2) Tentukan apakah file yang diunggah sudah ada. Jika ada, apakah perlu dihapus sebelum mengunggah atau lanjutan transmisi.
1. Unggah atau unduh kelas enumerasi:
paket com.scengine.wtms.utils.ftp; Public Enum unggahstatus {file_exits (0), create_directory_success (1), create_directory_fail (2), unggah_from_break_success (3), unggah_from_break_faild (4), download_from_break_success (5) Unggah_new_file_success (7), unggah_new_file_failed (8), delete_remote_success (9), delete_remote_faild (10), remote_bigger_local (11), remote_smaller_locall (12); status int pribadi; public int getStatus () {status pengembalian; } public void setstatus (status int) {this.status = status; } Unggahtatus (status int) {this.status = status; }}2. Kode Alat:
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.printwriter; 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; kelas publik Continueftp {private ftpClient ftpClient = ftpClient baru (); / *** Pengaturan Konstruksi Objek Output Perintah yang digunakan dalam proses ke konsol*/ continueftp publik () {this.ftpclient.addprotocolCommandlistener (printCommandlistener baru (printwriter baru (System.out))); } /** * * Used to connect to the FTP server in java programming* * @param hostname * Hostname * @param port * Port * @param username * Username * @param password * Password * @return Whether the connection is successful* * @throws IOException */ public boolean connect(String hostname, int port, String username, String password) throws IOException { ftpClient.Connect (nama host, port); if (ftpreply.ispositiveCompletion (ftpclient.getReplycode ())) {if (ftpclient.login (nama pengguna, kata sandi)) {return true; }} disconnect (); mengembalikan false; } /** * * Download file from the FTP server* * @param remote * Remote file path* * @param local * Local file path* * @return Whether it is successful* * @throws IOException */ @SuppressWarnings("resource") public boolean download(String remote, String local) throws IOException { ftpClient.enterLocalPassiveMode(); ftpClient.setFileType (ftp.binary_file_type); hasil boolean; File f = file baru (lokal); Ftpfile [] file = ftpClient.listFiles (remote); if (file.length! = 1) {System.out.println ("File jarak jauh tidak unik"); mengembalikan false; } long lremotesize = file [0] .getSize (); if (f.exists ()) {outputStream out = new fileoutputStream (f, true); System.out.println ("Ukuran file lokal adalah:" + f.length ()); if (f.length ()> = lremotesize) {System.out.println ("Ukuran file lokal lebih besar dari ukuran file jarak jauh, unduh dibatalkan"); mengembalikan false; } ftpClient.SetRestArtArtOffset (f.length ()); hasil = ftpclient.retrievefile (jarak jauh, keluar); out.close (); } else {outputStream out = FileOutputStream baru (f); hasil = ftpclient.retrievefile (jarak jauh, keluar); out.close (); } hasil pengembalian; }/** * * Mengunggah file ke server FTP, mendukung transmisi kontinu breakpoint * * @param lokal * nama file lokal, jalur absolut * * @param jarak jauh * jalur file jarak jauh, menggunakan/home/directory1/subdirectory/file. @throws IoException */ @suppressWarnings ("Resource") Upload Publicstatus (String Local, String Remote) melempar ioException {// atur passiveMode untuk mengirimkan ftpclient.enterLocalPassiveMode (); // atur ftpclient.setFileType (ftp.binary_file_type); Unggah hasil; // Pemrosesan Direktori Remote 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, buat direktori server jarak jauh int rekursif 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 {System.out.println ("Buat Gagal membuat direktori"); return unggahstatus.create_directory_fail; }} start = end + 1; end = directory.indexof ("/", start); // Periksa apakah semua direktori telah dibuat IF (end <= start) {break; }}}} // Periksa apakah file ada di file ftpfile [] remote = 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 unggahstatus.file_exits; } lain if (remoteSize> localSize) {return unggahstatus.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 (remote, is)) {return unggahstatus.upload_from_break_success; }} // Jika gangguan breakpoint tidak berhasil, hapus file di server dan unggah lagi jika (! Ftpclient.deleteFile (remoteFileName)) {return unggahstatus.delete_remote_faild; } adalah = FileInputStream baru (f); if (ftpclient.storefile (jarak jauh, is)) {result = unggahstatus.upload_new_file_success; } else {result = unggahstatus.upload_new_file_failed; } is.close (); } else {inputStream adalah = FileInputStream baru (local); if (ftpclient.storefile (remoteFileName, is)) {result = unggahstatus.upload_new_file_success; } else {result = unggahstatus.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) {continueftp myftp = new continueftp (); coba {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 ("Kesalahan yang menghubungkan ke 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.