今作っているアプリケーションの要件に他サーバへ日次でFTP送信するというのがあり、シェルでいいかと思っていたのですが、既に別要件でVFS2を使っていたので、ついでにFTP送信もVFS2でやる事にしました。で、前に作ったクラスに下記を追加。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/** * FTPファイルPut * @param 送信元DIR * @param 送信元ファイル * @param サーバログインユーザー * @param サーバログインパスワード * @param サーバホスト名 * @param 送信先DIR * @param 送信先ファイル名 * @throws IOException * * */ public void FTPputFile( String localpath, String File, String User, String Password, String HostName, String remotePath, String remoteFile) throws IOException { String connURL = getConnectionURL(User, Password, HostName,"ftp"); FileSystemOptions opts = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); StandardFileSystemManager fsManager = new StandardFileSystemManager(); fsManager.init(); try{ FileObject localFileObject = fsManager.resolveFile(localpath + "/" + File,opts); if(!localFileObject.exists()){ throw new IOException("File Not Exists"); } FileObject remoteFileObject = fsManager.resolveFile(connURL + remotePath + "/" + remoteFile,opts); remoteFileObject.copyFrom(localFileObject, Selectors.SELECT_SELF); }catch(IOException e){ throw new IOException(e); }finally{ fsManager.close(); fsManager = null; } } /** * 接続URLを取得する * @param サーバログインユーザー * @param サーバログインパスワード * @param サーバホスト名 * @param sftp ftp 等の文字列 * @return URL * @throws UnsupportedEncodingException * */ public String getConnectionURL(String User, String Password, String HostName, String FileSystem) throws UnsupportedEncodingException { return FileSystem + "://" + URLEncoder.encode(User, "UTF-8") + ":" + URLEncoder.encode(Password, "UTF-8") + "@" + URLEncoder.encode(HostName, "UTF-8"); } |