Apache FTPツールを使用して、次のようにファイルをアップロード、ダウンロード、削除します
1.対応するJARパッケージをダウンロードします
commons-net-1.4.1.jar
2。実装コードは次のとおりです。
public class ftputils {// ftpサーバーアドレスpublic string hostname = "192.168.1.249"; // FTPサーバーポート番号はデフォルトで21 Public Integer Port = 21; // ftpログインアカウントパブリック文字列ユーザー名= "root"; // ftpログインパスワードパブリック文字列パスワード= "123"; public ftpclient ftpclient = null; / *** ftp serverを初期化*/ public void initftpclient(){ftpclient = new ftpclient(); ftpclient.setControlencoding( "utf-8"); try {System.out.println( "接続... ftpサーバー:"+this.hostname+":"+this.port); ftpclient.connect(hostname、port); // ftp server ftpclient.login(username、password)を接続します。 // ftpサーバーにログインint replycode = ftpclient.getReplyCode(); //サーバーが正常にログインしているかどうか(!ftpreply.ispositiveCompletion(ReplyCode)){system.out.println( "connect failed ... ftp server:"+this.hostname+":"+this.port); } system.out.println( "connect successfu ... ftp server:"+this.hostname+":"+this.port); } catch(malformedurlexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); }} / ***アップロードファイル* @param pathname ftp service save address* @param filename filename ftpにアップロード* @param riginfilenameアップロードするファイルの名前(絶対アドレス) inputstream inputstream = null; try {System.out.println( "ファイルを起動する"); inputStream = new FileInputStream(new file(originFileName)); initftpclient(); ftpclient.setfiletype(ftpclient.binary_file_type); createdirecroty(pathname); ftpclient.makedirectory(pathname); ftpclient.changeworkingdirectory(pathname); ftpclient.storefile(filename、inputstream); inputstream.close(); ftpclient.logout(); flag = true; system.out.println( "ファイルを正常にアップロード"); } catch(Exception e){System.out.println( "File Failed"); e.printstacktrace(); }最後に{if(ftpclient.isconnected()){try {ftpclient.disconnect(); } catch(ioexception e){e.printstacktrace(); }} if(null!= inputstream){try {inputstream.close(); } catch(ioexception e){e.printstacktrace(); }}} return true; } / ***アップロードファイル* @param pathname ftp serviceアドレス* @param filenameファイル名ftpにアップロード* @param inputstream inputファイルストリーム* @return* / public boolean uploadfile(string filename、inputstream inputstream){boolean flag = fals; try {System.out.println( "ファイルを起動する"); initftpclient(); ftpclient.setfiletype(ftpclient.binary_file_type); createdirecroty(pathname); ftpclient.makedirectory(pathname); ftpclient.changeworkingdirectory(pathname); ftpclient.storefile(filename、inputstream); inputstream.close(); ftpclient.logout(); flag = true; system.out.println( "ファイルを正常にアップロード"); } catch(Exception e){System.out.println( "File Failed"); e.printstacktrace(); }最後に{if(ftpclient.isconnected()){try {ftpclient.disconnect(); } catch(ioexception e){e.printstacktrace(); }} if(null!= inputstream){try {inputstream.close(); } catch(ioexception e){e.printstacktrace(); }} trueを返します。 } //ディレクトリパスの変更public boolean changeworkingdirectory(string directory){boolean flag = true; try {flag = ftpclient.changeworkingdirectory(directory); if(flag){system.out.println( "enter folder" +ディレクトリ + "success!"); } else {system.out.println( "enter folder" +ディレクトリ + "failed!sport folder"); }} catch(ioexception ioe){ioe.printstacktrace(); } flagを返します。 } //マルチレイヤーディレクトリファイルを作成します。 FTPサーバーがある場合、作成されません。いいえがある場合は、public Public Boolean createdirecroty(string remote)を作成します。 string directory = remote + "/"; //リモートディレクトリが存在しない場合、リモートサーバーディレクトリを再帰的に作成します(!directory.equalsignorecase( "/")&&!changeworkingdirectory(new String(directory))){int start = 0; int end = 0; if(directory.startswith( "/")){start = 1; } else {start = 0; } end = directory.indexof( "/"、start);文字列path = "";文字列パス= ""; while(true){string subdirectory = new String(remote.substring(start、end).getBytes( "gbk")、 "ISO-8859-1"); path = path + "/" + subdirectory; if(!estistfile(path)){if(makedirectory(subdirectory)){changeworkingdirectory(subdirectory); } else {system.out.println( "create directory [" + subdirectory + "] failed"); changeworkingdirectory(subdirectory); }} else {changeworkingdirectory(subdirectory); } paths = paths + "/" + subdirectory; start = end + 1; end = directory.indexof( "/"、start); //すべてのディレクトリが作成されたかどうかを確認します(end <= start){break; }}} return success; } // ftpサーバーファイルがパブリックブールの存在するかどうかを決定します(string path)throws ioexception {boolean flag = false; ftpfile [] ftpfilearr = ftpclient.listfiles(path); if(ftpfilearr.length> 0){flag = true; } flagを返します。 } //ディレクトリを作成しますpublic boolean makedirectory(string dir){boolean flag = true; try {flag = ftpclient.makedirectory(dir); if(flag){system.out.println( "folder" + dir + "success!"); } else {system.out.println( "create folder" + dir + "failed!"); }} catch(例外e){e.printstacktrace(); } flagを返します。 } / ** *ファイルをダウンロード * * @param pathname ftp server file directory * * @param filename name * * @param localpathファイルパスダウンロード後 * * * @return * / public boolean downlownownownownownownedfile(string pathname、string filename、string localpath){boolean flag = false; outputStream os = null; try {System.out.println( "Fileをダウンロードを開始"); initftpclient(); // ftpディレクトリを切り替えますftpclient.changeworkingdirectory(pathname); ftpfile [] ftpfiles = ftpclient.listfiles(); for(ftpfile file:ftpfiles){if(filename.equalsignorecase(file.getName())){file localfile = new file(localpath + "/" + file.getName()); os = new fileoutputStream(localfile); ftpclient.retrieveFile(file.getName()、os); os.close(); }} ftpclient.logout(); flag = true; system.out.println( "ファイルを正常にダウンロード"); } catch(Exception e){System.out.println( "File Failed"のダウンロード "); e.printstacktrace(); }最後に{if(ftpclient.isconnected()){try {ftpclient.disconnect(); } catch(ioexception e){e.printstacktrace(); }} if(null!= os){try {os.close(); } catch(ioexception e){e.printstacktrace(); }}} flagを返します。 } / ** *ファイルを削除 * * @param pathname ftpサーバーディレクトリを保存 * * @param filenameファイル名を削除する * * @return * / public boolean deletefile(string pathname、string filename){boolean flag = fals; try {System.out.println( "ファイルを削除を開始"); initftpclient(); // ftpディレクトリを切り替えますftpclient.changeworkingdirectory(pathname); ftpclient.dele(filename); ftpclient.logout(); flag = true; system.out.println( "ファイルを正常に削除"); } catch(Exception e){System.out.println( "ファイルが失敗した"を削除 "); e.printstacktrace(); }最後に{if(ftpclient.isconnected()){try {ftpclient.disconnect(); } catch(ioexception e){e.printstacktrace(); }} return flag; } public static void main(string [] args){ftputils ftp = new ftputils(); //ftp.uploadfile("ftpfile/data "、" 123.docx "、" e://123.docx "); //ftp.downloadfile("ftpfile/data "、" 123.docx "、" f:// "); ftp.deletefile( "ftpfile/data"、 "123.docx"); system.out.println( "ok"); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。