Apache FTPに基づいて、ファイルをアップロードするときに次の問題を考慮する必要があります(例は継続関数です)。
(1)FTPサーバーにディレクトリの変更があるかどうか、存在しない場合は、ディレクトリを作成する必要があります。
(2)アップロードされたファイルがすでに存在するかどうかを判断します。それが存在する場合は、アップロードまたは継続的な送信の前に削除する必要があります。
1.列挙クラスをアップロードまたはダウンロードします。
パッケージcom.scengine.wtms.utils.ftp; public Enum uploadstatus {file_exits(0)、create_directory_success(1)、create_directory_fail(2)、upload_from_break_success(3)、upload_from_faild(4)、download_from_break_succes upload_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をインポートします。 Import org.apache.commons.net.printcommandlistener; org.apache.commons.net.ftp.ftpをインポートします。 Import org.apache.commons.net.ftp.ftpclient; org.apache.commons.net.ftp.ftpfileをインポートします。 import org.apache.commons.net.ftp.ftpreply; public class continueftp {private ftpclient ftpclient = new ftpclient(); / ***オブジェクトの構成設定プロセスで使用されたコマンドをコンソールに出力*/ public contionftp(){this.ftpclient.addprotocolcommandlistener(new PrintCommandListener(new PrintWriter(System.out))); } / ** * * JavaプログラミングのFTPサーバーに接続するために使用 * * @param hostname * hostname * @param port * @param username * @paramパスワード *パスワード * @returnコネクションが成功したかどうか * * @throws ioexception * / public boolean connect(int、int ert、string username、string esername、string esername、string estring passwerd ftpclient.connect(hostname、port); if(ftpreply.ispositiveCompletion(ftpclient.getReplyCode())){if(ftpclient.login(username、password)){return true; }} disconnect(); falseを返します。 } / ** * * FTPサーバーからファイルをダウンロード * * @paramリモート *リモートファイルパス * * @paramローカル *ローカルファイルパス * * @return成功したかどうか * * @throws ioexception * / @suppresswarnings( "resource")public boolean download(string、string local)throws ioexception {ftpclient.enterlocalpassivememode。 ftpclient.setfiletype(ftp.binary_file_type);ブール結果;ファイルf = new File(local); ftpfile [] files = ftpclient.listfiles(remote); if(files.length!= 1){system.out.println( "リモートファイルは一意ではない"); falseを返します。 } long lremotesize = files [0] .getSize(); if(f.exists()){outputstream out = new fileoutputStream(f、true); system.out.println( "ローカルファイルサイズは次のとおりです。" + f.length()); if(f.length()> = lRemotesize){System.out.println( "ローカルファイルサイズはリモートファイルサイズより大きく、ダウンロード中止"); falseを返します。 } ftpclient.setRestArtOffset(f.Length()); result = ftpclient.retrievefile(remote、out); out.close(); } else {outputStream out = new fileoutputStream(f); result = ftpclient.retrievefile(remote、out); out.close(); } return result; }/** * *ファイルをFTPサーバーにアップロードし、ブレークポイント連続伝送 * * @paramローカル *ローカムパス * * @paramリモート *リモートファイルパスを使用します。 ioexception */ @suppresswarnings( "resource")public uploadstatus upload(string local、string remote)throws ioexception {// passivemodeを設定してftpclient.enterlocalpassivemode(); // ftpclient.setfiletype(ftp.binary_file_type); uploadStatusの結果。 //リモートディレクトリの処理文字列remotefileName = remote; if(remote.contains( "/")){remotefileName = remote.substring(remote.lastindexof( "/") + 1); string directory = remote.substring(0、remote.lastindexof( "/") + 1); if(!directory.equalsignorecase( "/")&&!ftpclient.changeworkingdirectory(directory)){//リモートディレクトリが存在しない場合は、リモートサーバーディレクトリを再帰的に作成します。 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( "create create create of create Directory"); uploadstatus.create_directory_failを返します。 }} start = end + 1; end = directory.indexof( "/"、start); //すべてのディレクトリが作成されたかどうかを確認します(end <= start){break; }}}} //ファイルがリモートftpfile [] files = ftpclient.listfiles(remotefileName)に存在するかどうかを確認します。 if(files.length == 1){long remotesize = files [0] .getSize();ファイルf = new File(local); long localsize = f.length(); if(remotesize == localsize){return uploadstatus.file_exits; } else if(remotesize> localsize){uploadstatus.remote_bigger_localを返します。 } //ファイル内のポインターを移動して、Breakpoint inputstreamの中断を実現してみてください。 if(is.skip(remotesize)== remotesize){ftpclient.setRestArtOffset(remotesize); if(ftpclient.storefile(remote、is)){return uploadstatus.upload_from_break_success; }} // BreakPointの中断が成功しない場合、サーバー上のファイルを削除して再度アップロードする場合(!ftpclient.deletefile(remotefilename)){return uploadstatus.delete_remote_faild; } is = new fileInputStream(f); if(ftpclient.storefile(remote、is)){result = uploadstatus.upload_new_file_success; } else {result = uploadstatus.upload_new_file_failed; } is.close(); } else {inputstream is = new fileInputStream(local); if(ftpclient.storefile(remotefilename、is)){result = uploadstatus.upload_new_file_success; } else {result = uploadstatus.upload_new_file_failed; } is.close(); } return result; } / ** * *リモートサーバーから切断 * * @Throws ioException * / public void disconnect()throws ioexception {if(ftpclient.isconnected()){ftpclient.disconnect(); }} public static void main(string [] args){continueftp myftp = new contionftp(); try {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をもっとサポートすることを願っています。