私が過去に行っていたプロジェクト、FTPサーバーにファイルをアップロードし、今ではレビューします。比較的簡単で、一度に理解できます。
環境:最初に、最初にFTPサーバーをインストールします。 IISを使用してWin8でローカルに構成しました。 Baiduでインストールドキュメントを見つけることができます。
1.プロジェクトディレクトリにFTP構成ファイルを作成すると、ディレクトリは以下に示すとおりです。
01 ftpconfig.properties:
ftpip = 10.73.222.29
ftpport = 21
ftpuser = wp
ftppwd = 04143114wp
ftpremotepath = d://共有
02 ftpconfig.propertiesで特定のコンテンツを読み取るクラス:
パッケージcom.java.core.util; Import java.io.ioexception; Import java.io.inputstream; Import java.util.properties;/*** @author wangpei* @version reaced:2017年5月6日午後9時42分40 pm私有地のプロパティ。 public readftpproperties(){is = this.getClass()。getResourceasStream( "/ftpconfig.properties"); try {properties.load(is); } catch(ioException e){system.out.println( "構成ファイルは存在しません.."); e.printstacktrace(); }最後に{if(null!= is){try {is.close(); } catch(ioException e){system.out.println( "閉じたストリームが失敗した.."); e.printstacktrace(); }}}} public string getIp(){// FTPサーバーのIPアドレスを取得します。 } public String getPort(){// FTPサーバーのreturn Properties.getProperty( "ftpport"); } public string getUser(){// ftpログインユーザー名を取得しますreturn properties.getProperty( "ftpuser"); } public String getPwd(){// FTPサーバーのreturn Properties.getProperty( "ftppwd")のログインパスワードを取得します; } public string getRemotepath(){// FTPサーバーにファイルが保存されているディレクトリを取得します。 }}03ファイルアップロードとダウンロードのインターフェイスクラス
パッケージcom.java.web.service; Import java.io.inputstream; Import org.apache.commons.net.ftp.ftpclient; Import com.java.core.core.readftpproperties; interface ftpservice { / * * ftpにログイン * / public boolean loginftp(ftpclient client、readftpproperties rfp); / * * ftpからログアウト */ public boolean logout(ftpclientクライアント); /// * * FTPの名前は入力ストリーム */ public boolean uploadfile(ftpclientクライアント、文字列remotepath、string filenewname、inputstream inputstream、readftpporties rfp); / * *ディレクトリremotepathからファイルファイル名をダウンロード */ public inputstream downfileByftp(ftpclientクライアント、文字列remotepath、string filename); /** ftpのディレクトリパス名でファイルを削除*/ public boolean delfile(ftpclient client、string pathname);}04ファイルアップロードとダウンロードのインターフェイス実装クラス
パッケージcom.java.web.service.serviceimpl; Import java.io.ioexception; Import java.io.inputStream; Import java.io.unsupportedencodingincection; import java.net.socketexception; Import org.apache.commons.net.ftp.ftp.fttp.ptp.cmons.net.ftp.ftp.ftp.ftp.fttp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp.ftp. org.apache.commons.net.ftp.ftpfile; Import com.java.core.util.readftpproperties; Import com.java.web.service.ftpservice;/*** @author wangpei* @version create boolean loginftp(ftpclientクライアント、readftpproperties rfp){string ftpip = rfp.getip(); string ftpport = rfp.getport(); string ftpuser = rfp.getuser();文字列ftppwd = rfp.getpwd(); // string fgtpremotepath = rfp.getRemotepath();ブールb = false; try {client.connect(ftpip、integer.parseint(ftpport)); } catch(numberformatexception e){system.out.println( "ftpに接続できない"); falseを返します。 } catch(socketexception e){system.out.println( "ftpに接続できない"); falseを返します。 } catch(ioException e){system.out.println( "ftpに接続できない"); falseを返します。 } catch(ioException e){system.out.println( "ftpに接続できない"); falseを返します。 } client.setControlencoding( "uft-8"); try {b = client.login(ftpuser、ftppwd); } catch(ioException e){system.out.println( "login ftp error"); logout(client); // logout/disconnect ftp server link return false; } burten b; } public boolean logout(ftpclient client){boolean b = false; try {b = client.logout(); // logout client.disconnect(); // disconnect} catch(ioexception e){return false; } burten b; } public boolean uploadFile(ftpclientクライアント、string remotepath、string filenewname、inputstream inputstream、readftpproperties rfp){boolean b = false; try {client.setFileType(ftpclient.binary_file_type); client.enterlocalpassivemode(); if(remotepath!= null &&! ""。equals(remotepath.trim()){string [] paths = remotepath.split( "/"); for(string onepath:paths){if(onepath == null || "" .equals(onepath.trim())){継続; } onePath = new String(OnePath.GetBytes( "UTF-8")、 "ISO-8859-1"); System.out.println( "OnePath =" + OnePath); if(!client.changeworkingdirectory(onepath)){client.makedirectory(onepath); // ftp server directory client.changeworkingdirectory(onepath); // ftp serverディレクトリの変更} }}} b = client.storefile(new String(filenewname.getBytes( "utf-8")、 "ISO-8859-1")、inputstream); } catch(unsupportedencodingexception e){return false; } catch(ioException e){false; } burten b; } public inputstream downfilebyftp(ftpclient ftpclient、string remotepath、string filename){ftpfile [] fs; inputstream is = null; try {//パッシブモードftpclient.enterlocalpassivemode()を設定してください。 // ftpclient.setfiletype(ftp.binary_file_type); //編集フォーマットftpclient.setControlencoding( "utf-8"); Remotepath = Remotepath.Substring(0、Remotepath.lastIndexof(filename)); fs = ftpclient.listfiles(remotepath); //(ftpfile ff:fs)の再帰ターゲットディレクトリ{if(ff.getname()。equals(filename)){//ターゲットファイルを見つける= ftpclient.retrievefileStream(new filename)。 "ISO-8859-1"));壊す; }}} catch(ioexception e){e.printstacktrace(); } return is; } public boolean delfile(ftpclient ftpclient、string pathname){boolean b = false; try {b = ftpclient.deletefile(pathname); bを返します。 } catch(例外e){falseを返します。 }最後に{logout(ftpclient); // exit/disconnect ftp server link}}}}コードは理解しやすく、一度読むことで理解する必要があります。ここでは特に分析しません。主にコードのコメントを見てください。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。