import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.ConnectException; import java.net.UnknownHostException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; public class FtpUtil { // FTP /** FTPクライアントオブジェクト */ private FTPClient ftp = null; /** FTP転送成功コード */ private static int result_transfer_success = 226; /** FTPファイル名変更成功コード */ private static int result_rename_success = 250; /** OS区切り文字 */ private String SEPARATER = "/"; //ログオブジェクト private Log log = LogFactory.getLog(this.getClass()); /** * FTP接続 * @param String ftpHost FTPホスト名 * @param String ftpUser FTPユーザ名 * @param String ftpPwd FTPパスワード * @param int timeout デフォルトタイムアウト値(単位:ミリ秒) * @throws Exception */ public void connect( String ftpHost, String ftpUser, String ftpPwd, int timeout) throws Exception { String method = "connect()"; log.debug(method + " start"); ftp = new FTPClient(); try { // デフォルトタイムアウト値を指定 ftp.setDefaultTimeout(timeout); // 文字エンコーディングを指定 ftp.setControlEncoding("MS932"); // 接続 ftp.connect(ftpHost); // ログイン if (ftp.login( ftpUser, ftpPwd) == false) { log.error( "ログイン失敗"); throw new Exception( "login失敗"); } // バイナリモードに設定 ftp.setFileType(FTP.BINARY_FILE_TYPE); // パッシブモードでセッションを確立 ftp.enterLocalPassiveMode(); } catch ( ConnectException ce ) { log.fatal( "connect失敗", ce); throw ce; } catch ( UnknownHostException uhe){ log.fatal( "connect失敗", uhe); throw uhe; } catch (Exception e) { log.fatal( "connect失敗", e); throw e; } finally { log.debug(method + " end"); } } /** * FTP(PUT) * @param String localDir 送信元ディレクトリパス(クライアント) * @param String localFile 送信元ファイル名(クライアント) * @param String putDir 送信先ディレクトリ(FTPサーバー) * @param String tempName 一時ファイル名 * @throws Exception */ public void put( String localDir, String localFile, String putDir, String tempName) throws Exception { String method = "put()"; log.debug(method + " start"); // ディレクトリチェック(最後の区切り文字を付加する) localDir = checkDirPath(localDir); putDir = checkDirPath(putDir); // 送信中ファイル名 String putFile = ""; if( tempName == null || tempName.length() == 0 ) { putFile = localFile; } else { putFile = tempName; } FileInputStream is = null; try { // FTPサーバーにファイルアップロード is = new FileInputStream( localDir + localFile); ftp.storeFile( putDir + putFile, is); if( ftp.getReplyCode() != result_transfer_success ) { log.error("ファイルアップロード失敗"); throw new Exception(ftp.getReplyString()); } // 送信中ファイル名が指定されている場合はファイル名の変更 if( ! ( tempName == null || tempName.length() == 0 ) ) { ftp.rename( putDir + putFile, putDir + localFile); if( ftp.getReplyCode() != result_rename_success) { log.error("ファイル名変更失敗" + ftp.getReplyString()); throw new Exception(Integer.toString(ftp.getReplyCode())); } } } catch (Exception e) { log.error( "put失敗", e); // 送信途中のファイルがある場合は削除 if( isFtpFile( putDir, putFile )) { del( putDir, putFile); } throw e; } finally { try { if (is != null) { is.close(); } } catch (IOException ignore) {} log.debug(method + " end"); } } /** * FTP(GET) * @param String getDir 送信元ディレクトリパス(FTPサーバー) * @param String getFile 送信元ファイル名(FTPサーバー) * @param String localDir 受信対象ディレクトリ(クライアント) * @param String tempName 一時ファイル名 * @throws Exception */ public void get( String getDir, String getFile, String localDir, String tempName) throws Exception { String method = "get()"; log.debug(method + " start"); FileOutputStream os = null; // ディレクトリチェック(最後の区切り文字を付加する) localDir = checkDirPath(localDir); getDir = checkDirPath(getDir); // 受信中ファイル名 String localFile = ""; if( tempName == null || tempName.length() == 0 ) { localFile = getFile; } else { localFile = tempName; } try { // FTPサーバからファイルを受信 os = new FileOutputStream( localDir + localFile ); // クライアント側 ftp.retrieveFile( getDir + getFile, os); // サーバー側 if( ftp.getReplyCode() != result_transfer_success ) { log.error("ファイルダウンロード失敗"); throw new Exception(ftp.getReplyString()); } // 受信中ファイル名が指定されている場合はファイル名の変更 if( ! ( tempName == null || tempName.length() == 0 ) ) { os.close(); File file = new File( localDir + localFile); if( ! file.renameTo( new File( localDir + getFile)) ) { throw new Exception( "ファイル名変更失敗"); } } } catch (Exception e) { log.error( ftp.getReplyString(), e); // 受信途中のファイルがある場合は削除 File file = new File( localDir + localFile ); if( file.exists() ) { try { file.delete(); } catch( Exception aiue){ aiue.printStackTrace(); } } throw e; } finally { try { if (os != null) { os.close(); } } catch (IOException ignore) {} log.debug(method + " end"); } } /** * ファイル削除 * @param String delDir 削除ディレクトリ * @param String delName 削除ファイル名 * @throws IOException */ public void del( String delDir, String delFile) throws IOException { String method = "del()"; log.debug(method + " start"); // ディレクトリチェック delDir = checkDirPath(delDir); try { // FTPサーバ上のファイルを削除 ftp.deleteFile(delDir + delFile); } catch (IOException e) { log.error("ファイル削除失敗"); throw e; } finally { log.debug(method + " end"); } } /** * FTP通信切断 * * @throws Exception */ public void disconnect() throws Exception { String method = "disconnect()"; log.debug(method + " start"); try { if ( ftp.isConnected() ) { // ログアウト ftp.logout(); // 切断 ftp.disconnect(); } } catch (IOException e) { log.error("切断失敗"); throw e; } finally { log.debug(method + " end"); } } /** * FTPサーバー上に指定されたファイルが存在するか検査 * @param String searchDir 検索するFTPサーバー上のディレクトリ * @param String searchName 検索するファイル名 * @return boolean true:存在する,false:存在しない */ private boolean isFtpFile( String searchDir, String searchName) { String method = "isFtpFile()"; log.debug(method + " start"); boolean isExist = false; try { String[] flist = ftp.listNames(searchDir); if (flist != null) { for(int i = 0; i < flist.length; i++) { if( searchName.equals(flist[i]) ) { isExist = true; break; } } } } catch (IOException e) { log.error( "ファイル一覧取得失敗", e); } finally { log.debug(method + " end"); } return isExist; } /** * ディレクトリの最後の区切り文字を付加する すでについている場合そのまま返す * @param dirName * @return String 区切り文字を付加したディレクトリ名 */ private String checkDirPath( String dirName ) { String method = "checkDirPath()"; log.debug(method + " start"); if( ! dirName.substring( dirName.length() - SEPARATER.length()).equals(SEPARATER) ) { dirName = dirName + SEPARATER; } log.debug(method + " end"); return dirName; } }