VFS2でのFTP送信を追加

今作っているアプリケーションの要件に他サーバへ日次でFTP送信するというのがあり、シェルでいいかと思っていたのですが、既に別要件でVFS2を使っていたので、ついでにFTP送信もVFS2でやる事にしました。で、前に作ったクラスに下記を追加。

    /**
     * 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");
	}
カテゴリー: Java