001 /*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016 package org.opengion.fukurou.util;
017
018 import java.io.FileInputStream;
019 import java.io.FileOutputStream;
020 import java.io.InputStream;
021 import java.io.OutputStream;
022 import java.io.PrintWriter;
023 import java.io.IOException;
024 import java.util.Map;
025 import java.util.LinkedHashMap ;
026
027 import org.apache.commons.net.ftp.FTP;
028 import org.apache.commons.net.ftp.FTPClient;
029 import org.apache.commons.net.ftp.FTPFile;
030 import org.apache.commons.net.PrintCommandListener;
031 import org.apache.commons.net.ftp.FTPReply;
032
033 /**
034 * FTPConnect.java は、å?通的に使用されã‚?FTP関連の基本機è?を実è£?�—たã?クラスですã?
035 *
036 * これは、org.apache.commons.net.ftp.FTPClient をã?ースに開発されてã�?�¾すã?
037 * こã?クラスの実行には、commons-net-ftp-2.0.jar がå¿?¦�ですã?
038 *
039 * -host=FTPサーバã? -user=ユーザー -passwd=パスワーãƒ?-remoteFile=FTP先ã?ファイルå�?をå¿??設定しますã?
040 * -localFile=ローカルのファイル名ã?、å¿??ではありませんがã?-command=DEL の場合にのみ不要でありã€?
041 * それ以外ã? command の場合ã?、å¿?¦�ですã?
042 *
043 * -mode=[ASCII/BINARY] は、ファイル転送時に、ファイルの種類によってæŒ?®šしますã?
044 * ASCIIモードã?、OS毎に異なる改行コードを変換して転送しますã?BINARYモードã?、そのままの姿でã‚?‚Š取りしますã?
045 *
046 * -command=[GET/PUT/DEL] は、FTPサーバã?に対しての処ç�??方法をæŒ?®šしますã?
047 * GET:FTPサーバã?からローカルにファイル転送しまã�?初期値)ã€?
048 * PUT:ローカルファイルをFTPサーバã?に PUT(STORE、SAVE、UPLOAD、などと同意èª?しますã?
049 * DEL:FTPサーバã?のæŒ?®šã?ファイルを削除しますã?こã?場合ã?みã€?localFile 属æ?のæŒ?®šã?不要ですã?
050 *
051 * -passive=[true/false] は、パãƒ?‚·ブモーãƒ?ローカルからサーバã?へ接続を張ã‚?を利用する場合に宣è¨?�—ますã?
052 * 初期値は、true:使用する ですã?
053 * 通常のFTPでは、アクãƒ?‚£ブモードでファイルをやり取りしますã?これは、クライアントå?がã?サーバã?ソケãƒ?ƒˆã‚?
054 * オープンしã?æŒ?®šã?ポã?トでå¾?�¡ã�?�‘状態を作り、FTPサーバã?側から接続してもらã�?�¾すã?
055 * ä¸?ˆ¬çš?�ª企業å†?�§は、ファイアウォールの設定等で、外部からの接続を制限してã�?‚‹ケースが多く、アクãƒ?‚£ブモーãƒ?
056 * では、繋がらなã�??合が、ほとんどですã?
057 * こã?クラスでは、å?期å?をパãƒ?‚·ブモードにすることで、企業å†??ファイアウォールのある環å¢?�§ã‚?
058 * 接続できるようにしてã�?�¾すã?
059 *
060 * -mkdirs=[true/false] は、受けå?のファイル(GETæ™?LOCAL、PUTæ™?FTPサーバã?)に取り込ã‚?ƒ•ァイルのãƒ?‚£レクトリã�?
061 * 存在しなã�??合に、作æ?するかどã�?�‹を指定しまã�?初期値:true)ã€?
062 * 通常、FTPサーバã?に、フォルãƒ?šŽ層を作æ?してPUTする場合ã?動的にフォルãƒ?šŽ層を作æ?したã�?‚±ースで便利ですã?
063 * é€?�«、フォルãƒ??確定しており、指定フォルãƒ?»¥外に PUT するのはバグってã�?‚‹と事がåˆ?�‹ってã�?‚‹場合には
064 * false に設定して、存在しなã�?ƒ•ォルãƒ?�«PUT しよã�?�¨すると、エラーになるよã�?�«しますã?
065 *
066 * 引数æ–?—å?中に空白を含ã‚??合ã?、ダブルコーãƒ??ション("") で括って下さã�??
067 * 引数æ–?—å?の ã€?』ã?前後には、空白は挟めません。å¿?�šã€?key=value の様に
068 * 繋げてくださいã€?
069 *
070 * @og.formSample
071 * FTPConnect -host=FTPサーバã? -user=ユーザー -passwd=パスワーãƒ?-remoteFile=FTP先ã?ファイルå�?[-localFile=ローカルのファイル名]
072 * [-mode=[ASCII/BINARY] ] [-command=[GET/PUT/DEL] ] [-passive=[true/false] ]
073 *
074 * -host=FTPサーバã? ?š接続å?のFTPサーバã?のアドレスまたã?、サーバã?å�?
075 * -user=ユーザー ?š接続するユーザーå�?
076 * -passwd=パスワーãƒ? ?š接続するユーザーのパスワーãƒ?
077 * -remoteFile=FTP先ã?ファイルå�? ?š接続å?のFTPサーバã?側のファイル名ã?PUT,GET 関係なくFTP側としてæŒ?®šしますã?
078 * [-localFile=ローカルのファイル名] ?šローカルのファイル名ã?PUT,GET 関係なくローカルファイルを指定しますã?
079 * [-port=ポã?ãƒ?] ?š接続するサーバã?のポã?トをæŒ?®šしますã?
080 * [-mode=[ASCII/BINARY] ] ?š扱ã�?ƒ•ァイルの種類をæŒ?®šしまã�?初期値:ASCII)
081 * [-command=[GET/PUT/DEL] ] ?šFTPサーバã?側での処ç�??方法をæŒ?®šしますã?
082 * GET:FTP⇒LOCAL、PUT:LOCAL⇒FTP への転送でã�?初期値:GET)
083 * DEL:FTPファイルを削除しますã?
084 * [-passive=[true/false] ] ?šパãƒ?‚·ブモーãƒ?ローカルからサーバã?へ接続を張ã‚?を利用するかどã�?�‹(初期値:true)
085 * (false:アクãƒ?‚£ブモーãƒ?通常のFTPの初期値)で通信しますã?)
086 * [-mkdirs=[true/false] ] ?š受けå?ファイル(GETæ™?LOCAL、PUTæ™?FTPサーバã?)にãƒ?‚£レクトリを作æ?するかどã�?�‹(初期値:true)
087 * (false:ãƒ?‚£レクトリが無ければ、エラーにしますã?)
088 * [-encode=エンコード名 ] ?š日本語ファイル名などのエンコード名を指定しまã�?初期値:UTF-8)
089 * [-timeout=タイãƒ?‚¢ウãƒ?秒] ] ?šDataタイãƒ?‚¢ウãƒ?初期値:600 [秒])
090 * [-display=[false/true] ] ?štrueは、検索状況を表示しまã�?初期値:false)
091 * [-debug=[false|true] ] ?šデバッグæƒ??を標準å?力に表示する(true)かしなã�?false)ã�?初期値:false[表示しない])
092 *
093 * @og.rev 5.1.6.0 (2010/05/01) 新規追åŠ?
094 *
095 * @version 5.0
096 * @author Kazuhiko Hasegawa
097 * @since JDK5.0,
098 */
099 public final class FTPConnect extends AbstractConnect {
100 private final FTPClient ftp;
101
102 private static final int DEF_PORT = 21; // ポã?ãƒ?
103
104 private boolean isAsciiMode = true; // 扱ã�?ƒ•ァイルの種類をæŒ?®šしますã? (true:ASCII / false:BINARY)
105 private boolean isPassive = true; // パッシブモードを利用するかどã�?�‹ã€?(true:パッシãƒ?/ false:アクãƒ?‚£ãƒ?
106 private String encode = "UTF-8"; // 日本語ファイル名などのエンコード名(初期値:UTF-8)
107
108 private boolean isConnect = false; // コネクト済みかどã�?�‹ã€?
109
110 private String lastRemoteDir = "/"; // FTP先ã?æœ?¾Œに登録したフォルãƒ?��(mkdir の高é?化ã?ため)
111
112 /**
113 * ãƒ?ƒ•ォルトコンストラクター
114 */
115 public FTPConnect() {
116 ftp = new FTPClient();
117 }
118
119 /**
120 * FTPサーバã?への接続ã?ログインを行いますã?
121 *
122 * こã?メソãƒ?ƒ‰は、å?期化メソãƒ?ƒ‰ですã?
123 * FTPサーバã?への接続ã?ログインを行いますã?で、è¤?•°ファイルを転送すã‚?
124 * ケースでは、最初に?‘度ã�?�‘呼び出すだけですã?
125 * 接続å?を変更する場合ã?、もã�?¸?º¦こã?メソãƒ?ƒ‰をコールするå¿?¦�がありますã?
126 * (そã?ような場合ã?ã€??常、オブジェクトを構築しなおす方がよã�?�¨思いますã?)
127 */
128 @Override
129 public void connect() {
130 if( isDisplay ) { System.out.println( "CONNECT: HOST=" + host + ",USER=" + user + ",PORT=" + port ); }
131
132 // もし、すでに接続されてã�?�Ÿ場合ã?、クロース処ç�?‚’行いますã?
133 if( isConnect ) { disconnect(); }
134
135 try {
136 // connectメソãƒ?ƒ‰の前に呼び出さなければならなã�?‚‰しいã€?
137 // 日本語ファイル名対å¿?初期値:UTF-8)
138 if( encode != null ) {
139 ftp.setControlEncoding( encode );
140 }
141
142 // サーバã?に対して接続を行いますã?
143 ftp.connect( host, getPort( DEF_PORT ) );
144
145 int reply = ftp.getReplyCode();
146
147 if( !FTPReply.isPositiveCompletion( reply ) ) {
148 errAppend( "FTP server refused connection. [PositiveCompletion]" );
149 errAppend( " host = [" , host , "]" );
150 errAppend( " user = [" , user , "]" );
151 errAppend( " port = [" , port , "]" );
152 errAppend( ftp.getReplyString() );
153 disconnect();
154 throw new RuntimeException( getErrMsg() );
155 }
156
157 // サーバã?に対してログインを行いますã?
158 if( !ftp.login( user, passwd ) ) {
159 errAppend( "ログインに失敗しましたã€? );
160 errAppend( " host = [" , host , "]" );
161 errAppend( " user = [" , user , "]" );
162 errAppend( " port = [" , port , "]" );
163 errAppend( ftp.getReplyString() );
164 disconnect();
165 throw new RuntimeException( getErrMsg() );
166 }
167
168 // å�?¨®パラメータを設定しますã?
169 paramInit();
170 }
171 catch ( IOException ex ) {
172 errAppend( "FTP server refused connection." );
173 errAppend( ftp.getReplyString() );
174 errAppend( ex );
175 if( isDebug ) { ex.printStackTrace(); }
176 disconnect();
177 throw new RuntimeException( getErrMsg(),ex );
178 }
179
180 isConnect = true;
181 }
182
183 /**
184 * 設定されたパラメータの初期化å?ç�?‚’行いますã?
185 *
186 * こã?メソãƒ?ƒ‰は、ftp.connectt( host )メソãƒ?ƒ‰の実行後に
187 * 呼び出すå¿?¦�がありますã?
188 * パラメータをå?セãƒ?ƒˆ後に、このメソãƒ?ƒ‰を呼び出すことになりますã?
189 * timeout , encode , CommandListener(display ã‚?debug属æ?) , isPassive ,
190 * isAsciiMode など、ファイル名以外ã?属æ?æƒ??の設定を反映させますã?
191 * è¤?•°ファイルの転送時には、localFile , remoteFile 属æ?を設定ã?上ã?action() メソãƒ?ƒ‰ã‚?
192 * 呼び出すことで、対応できますã?
193 *
194 * @throws IOException 入出力エラーが発生したとã�?
195 */
196 private void paramInit() throws IOException {
197
198 // Dataタイãƒ?‚¢ウãƒ?初期値:600 [秒])ã€?TPClient のメソãƒ?ƒ‰には、ミリ秒でセãƒ?ƒˆしますã?
199 ftp.setDataTimeout( timeout*1000 );
200
201 // コマンドリスナã?のセãƒ?ƒˆ(設定済みのListenerをremoveする実è£??、å?れてませんã€?
202 if( isDisplay && isDebug ) {
203 ftp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter(System.out) ) );
204 }
205
206 // パッシブモードを利用するかどã�?�‹ã€?(true:パッシãƒ?/ false:アクãƒ?‚£ãƒ?
207 if( isPassive ) { ftp.enterLocalPassiveMode(); }
208 else { ftp.enterLocalActiveMode(); }
209
210 // 扱ã�?ƒ•ァイルの種類をæŒ?®?true:ASCII / false:BINARY)
211 boolean isOK = FLAG_OK ;
212 if( isAsciiMode ) { isOK = ftp.setFileType( FTP.ASCII_FILE_TYPE ); }
213 else { isOK = ftp.setFileType( FTP.BINARY_FILE_TYPE );}
214
215 if( !isOK ) {
216 errAppend( "paramInit Error." );
217 errAppend( ftp.getReplyString() );
218 throw new RuntimeException( getErrMsg() );
219 }
220 }
221
222 /**
223 * FTPサーバã?との接続をクローズしますã?
224 *
225 * ログインされてã�?‚‹場合ã?、ログアウトも行いますã?
226 * コネクトされてã�?‚‹場合ã?、ディスコネクトしますã?
227 *
228 */
229 @Override
230 public void disconnect() {
231 if( isDisplay ) { System.out.println( "DISCONNECT:" ); }
232
233 if( isConnect ) {
234 isConnect = false;
235 try {
236 ftp.logout();
237 if( ftp.isConnected() ) {
238 ftp.disconnect();
239 }
240 }
241 catch( Throwable th ) {
242 errAppend( "disconnect Error." );
243 errAppend( ftp.getReplyString() );
244 errAppend( th );
245 if( isDebug ) { th.printStackTrace(); }
246 throw new RuntimeException( getErrMsg(),th );
247 }
248 }
249 }
250
251 /**
252 * command="GET" が指定されたときã?処ç�?‚’行いますã?
253 *
254 * 接続å?のFTPサーバã?側のファイル名をローカルにãƒ?‚¦ンロードしますã?
255 *
256 * @param localFile ローカルのファイルå�?
257 * @param remoteFile FTP先ã?ファイルå�?
258 * @throws IOException 入出力エラーが発生したとã�?
259 */
260 @Override
261 protected void actionGET( final String localFile, final String remoteFile ) throws IOException {
262 if( isDebug ) { System.out.println( "GET: " + remoteFile + " => " + localFile ); }
263
264 // GET(DOWNLOAD)取得時は、ローカルファイルのãƒ?‚£レクトリを作æ?するå¿?¦�があるã€?
265 if( isMkdirs ) {
266 makeLocalDir( localFile );
267 }
268
269 // InputStream input = null;
270 OutputStream output = null;
271 try {
272 // input = ftp.retrieveFileStream( remoteFile ); // 連続読み込みを行うと、null になったã?
273 output = new FileOutputStream( localFile );
274 // isOK = FileUtil.copy( input,output );
275 if( !ftp.retrieveFile( remoteFile, output ) ) {
276 errAppend( "ファイルの取å¾?GET)に失敗しましたã€? );
277 errAppend( " localFile = [" , localFile , "]" );
278 errAppend( " remoteFile = [" , remoteFile , "]" );
279 errAppend( ftp.getReplyString() );
280 throw new RuntimeException( getErrMsg() );
281 }
282 }
283 finally {
284 // Closer.ioClose( input );
285 Closer.ioClose( output );
286 }
287 }
288
289 /**
290 * command="GETDIR" が指定されたときã?処ç�?‚’行いますã?
291 *
292 * 接続å?のFTPサーバã?側のãƒ?‚£レクトリ以下をローカルãƒ?‚£レクトリに階層構é?のままãƒ?‚¦ンロードしますã?
293 *
294 * @param localDir ローカルのãƒ?‚£レクトリå�?
295 * @param remoteDir FTP先ã?ãƒ?‚£レクトリå�?
296 * @throws IOException 入出力エラーが発生したとã�?
297 */
298 @Override
299 protected void actionGETdir( final String localDir, final String remoteDir ) throws IOException {
300 FTPFile[] rmtFiles = ftp.listFiles( remoteDir );
301 for( int i=0; i<rmtFiles.length; i++ ) {
302 String rmt = rmtFiles[i].getName();
303 if( rmtFiles[i].isDirectory() ) {
304 actionGETdir( addFile( localDir,rmt ),addFile( remoteDir,rmt ) );
305 }
306 else {
307 actionGET( addFile( localDir,rmt ),addFile( remoteDir,rmt ) );
308 }
309 }
310 }
311
312 /**
313 * command="PUT" が指定されたときã?処ç�?‚’行いますã?
314 *
315 * ローカルファイルをã?接続å?のFTPサーバã?側にアãƒ??ロードしますã?
316 *
317 * @param localFile ローカルのファイルå�?
318 * @param remoteFile FTP先ã?ファイルå�?
319 * @throws IOException 入出力エラーが発生したとã�?
320 */
321 @Override
322 protected void actionPUT( final String localFile, final String remoteFile ) throws IOException {
323 if( isDebug ) { System.out.println( "PUT: " + localFile + " => " + remoteFile ); }
324
325 boolean isOK = FLAG_OK;
326
327 // PUT(UPLOAD)登録時ã?、リモートファイルのãƒ?‚£レクトリを作æ?するå¿?¦�があるã€?
328 if( isMkdirs ) {
329 // 前回のDIRとの比è¼?�§、すでに存在してã�?‚Œば、makeDirectory 処ç�?‚’パスするã€?
330 int ad = remoteFile.lastIndexOf( '/' ) + 1; // 区åˆ?‚Šæ–?—を?‹1するã?
331 String tmp = remoteFile.substring( 0,ad );
332 if( ad > 0 && !lastRemoteDir.startsWith( tmp ) ) {
333 lastRemoteDir = tmp;
334 if( tmp.startsWith( "/" ) ) {
335 String[] fls = tmp.split( "/" );
336 ftp.changeWorkingDirectory( "/" );
337 for( int i=1; i<fls.length; i++ ) {
338 isOK = ftp.changeWorkingDirectory( fls[i] ); // まずã?チェンジしてみるã?
339 if( ! isOK ) {
340 isOK = ftp.makeDirectory( fls[i] ); // チェンジできなければ、作るã€?
341 if( isOK ) {
342 isOK = ftp.changeWorkingDirectory( fls[i] ); // 作れれã?、チェンジするã€?
343 }
344 }
345 // チェンジできなければ、エラー
346 if( ! isOK ) {
347 errAppend( "チェンジãƒ?‚£レクトリに失敗しましたã€? );
348 errAppend( " remoteDir = [" , lastRemoteDir , "]" );
349 errAppend( ftp.getReplyString() );
350 throw new RuntimeException( getErrMsg() );
351 }
352 }
353 }
354 if( ! isOK ) {
355 errAppend( "ãƒ?‚£レクトリの作æ?に失敗しましたã€? );
356 errAppend( " remoteDir = [" , lastRemoteDir , "]" );
357 errAppend( ftp.getReplyString() );
358 throw new RuntimeException( getErrMsg() );
359 }
360 }
361 }
362
363 InputStream input = null;
364 // OutputStream output = null;
365 try {
366 input = new FileInputStream( localFile );
367 // output = ftp.storeFileStream( remoteFile );
368 // isOK = FileUtil.copy( input,output );
369 if( !ftp.storeFile( remoteFile, input ) ) {
370 errAppend( "ファイルのストア(PUT)に失敗しましたã€? );
371 errAppend( " localFile = [" , localFile , "]" );
372 errAppend( " remoteFile = [" , remoteFile , "]" );
373 errAppend( ftp.getReplyString() );
374 throw new RuntimeException( getErrMsg() );
375 }
376 }
377 finally {
378 Closer.ioClose( input );
379 // Closer.ioClose( output );
380 }
381 }
382
383 /**
384 * command="DEL" が指定されたときã?処ç�?‚’行いますã?
385 *
386 * 接続å?のFTPサーバã?側のファイル名を削除しますã?
387 *
388 * @param remoteFile FTP先ã?ファイルå�?
389 * @throws IOException 入出力エラーが発生したとã�?
390 */
391 @Override
392 protected void actionDEL( final String remoteFile ) throws IOException {
393 if( isDebug ) { System.out.println( "DEL: " + remoteFile ); }
394
395 if( !ftp.deleteFile( remoteFile ) ) {
396 errAppend( "ファイルの削除(DEL)に失敗しましたã€? );
397 errAppend( " remoteFile = [" , remoteFile , "]" );
398 errAppend( ftp.getReplyString() );
399 throw new RuntimeException( getErrMsg() );
400 }
401 }
402
403 /**
404 * command="DELDIR" が指定されたときã?処ç�?‚’行いますã?
405 *
406 * 接続å?のFTPサーバã?側のãƒ?‚£レクトリ名を削除しますã?
407 *
408 * @param remoteDir FTP先ã?ãƒ?‚£レクトリå�?
409 * @throws IOException 入出力エラーが発生したとã�?
410 */
411 @Override
412 protected void actionDELdir( final String remoteDir ) throws IOException {
413
414 FTPFile[] rmtFiles = ftp.listFiles( remoteDir );
415 for( int i=0; i<rmtFiles.length; i++ ) {
416 String rmt = addFile( remoteDir,rmtFiles[i].getName() );
417 if( rmtFiles[i].isDirectory() ) {
418 actionDELdir( rmt );
419 }
420 else {
421 actionDEL( rmt );
422 // isOK = ftp.deleteFile( rmt );
423 }
424 }
425
426 if( !ftp.removeDirectory( remoteDir ) ) {
427 errAppend( "Directoryの削除(DEL)に失敗しましたã€? );
428 errAppend( " remoteDir = [" , remoteDir , "]" );
429 errAppend( ftp.getReplyString() );
430 throw new RuntimeException( getErrMsg() );
431 }
432 }
433
434 /**
435 * 扱ã�?ƒ•ァイルの種類をæŒ?®šしまã�?(初期値:ASCII)ã€?
436 *
437 * ファイルã‚?FTP でã‚?‚Š取りする場合ã?ASCII æŒ?®šすると、OSの改行コードã?違いã‚?
438 * 吸収して、ファイルのã‚?‚Š取りが行われますã?(つまりã?改行コード変換が行われますã?)
439 * BINARY æŒ?®šすると、現状のファイルのまま、やり取りされますã?
440 * これは、たとえã?アスキーファイルであってもã?そã?ままの姿でã‚?‚Š取りされるとã�?�†ことですã?
441 * å†?ƒ¨çš?�«は、isAsciiMode 属æ?に、true:ASCII / false:BINARY で管ç�?�•れますã?
442 *
443 * @param mode 扱ã�?ƒ•ァイルの種類をæŒ?®šしまã�?[ASCII/BINARY]
444 */
445 public void setMode( final String mode ) {
446 if( mode != null ) {
447 if( ! "ASCII".equals( mode ) && ! "BINARY".equals( mode ) ) {
448 errAppend( "モード指定ã?、ASCII またã?、BINARY とæŒ?®šくã�?�•ã�??" );
449 errAppend( " mode = [" , mode , "]" );
450 throw new RuntimeException( getErrMsg() );
451 }
452
453 isAsciiMode = "ASCII".equals( mode ) ; // "ASCII" 以外ã?、すべて "BINARY" になるã?
454 }
455 }
456
457 /**
458 * パッシブモードを利用するかどã�?�‹(true:パッシãƒ?を設定しまã�?初期値:true)ã€?
459 *
460 * パッシブモーãƒ?ローカルからサーバã?へ接続を張ã‚?を利用する場合に宣è¨?�—ますã?
461 * 通常のFTPでは、アクãƒ?‚£ブモードでファイルをやり取りしますã?これは、クライアントå?がã?サーバã?ソケãƒ?ƒˆã‚?
462 * オープンしã?æŒ?®šã?ポã?トでå¾?�¡ã�?�‘状態を作り、FTPサーバã?側から接続してもらã�?�¾すã?
463 * ä¸?ˆ¬çš?�ª企業å†?�§は、ファイアウォールの設定等で、外部からの接続を制限してã�?‚‹ケースが多く、アクãƒ?‚£ブモーãƒ?
464 * では、繋がらなã�??合が、ほとんどですã?
465 * こã?クラスでは、å?期å?をパãƒ?‚·ブモードにすることで、企業å†??ファイアウォールのある環å¢?�§ã‚?
466 * 接続できるようにしてã�?�¾すã?
467 *
468 * @param isPassive パッシブモードを利用するかどã�?�‹ã€?true:パッシãƒ?
469 */
470 public void setPassive( final boolean isPassive ) {
471 this.isPassive = isPassive ;
472 }
473
474 /**
475 * 日本語ファイル名などのエンコード名を指定しまã�?初期値:UTF-8)ã€?
476 *
477 * @param encode エンコード名
478 */
479 public void setEncode( final String encode ) {
480 if( encode != null ) {
481 this.encode = encode ;
482 }
483 }
484
485 // ******************************************************************************************************* //
486 // 以下ã?単独で使用する場合ã? main処ç�?
487 // ******************************************************************************************************* //
488
489 private static final Map<String,String> mustProparty ; // ?»プロパティ?½å¿??チェãƒ?‚¯用 Map
490 private static final Map<String,String> usableProparty ; // ?»プロパティ?½整合æ?チェãƒ?‚¯ Map
491
492 static {
493 mustProparty = new LinkedHashMap<String,String>();
494 mustProparty.put( "host", "接続å?のFTPサーバã?のアドレスまたã?、サーバã?å�?å¿??)" );
495 mustProparty.put( "user", "接続するユーザーå�?å¿??)" );
496 mustProparty.put( "passwd", "接続するユーザーのパスワーãƒ?å¿??)" );
497 mustProparty.put( "remoteFile", "接続å?のFTPサーバã?側のファイルå�?å¿??)" );
498
499 usableProparty = new LinkedHashMap<String,String>();
500 usableProparty.put( "localFile", "ローカルのファイルå�? );
501 usableProparty.put( "port", "接続に利用するポã?ト番号を設定しますã?" );
502 usableProparty.put( "mode", "扱ã�?ƒ•ァイルの種é¡?ASCII/BINARY)を指定しまã�?初期値:ASCII)" );
503 usableProparty.put( "command", "FTPサーバã?側での処ç�??方æ³?GET/PUT/DEL)を指定しまã�?初期値:GET)" );
504 usableProparty.put( "passive", "パッシブモーãƒ?ローカルからサーバã?へ接続を張ã‚?を利用するかどã�?�‹(初期値:true)" );
505 usableProparty.put( "mkdirs", "受け側ファイル(GETæ™?LOCAL、PUTæ™?FTPサーバã?)にãƒ?‚£レクトリを作æ?するかどã�?�‹(初期値:true)" );
506 usableProparty.put( "encode", "日本語ファイル名などのエンコード名を指定しまã�?初期値:UTF-8)" );
507 usableProparty.put( "timeout", "Dataタイãƒ?‚¢ウãƒ?初期値:600 [秒])" );
508 usableProparty.put( "display", "[false/true]:trueは、検索状況を表示しまã�?初期値:false)" );
509 usableProparty.put( "debug", "ãƒ?ƒ�ãƒ?‚°æƒ??を標準å?力に表示する(true)かしなã�?false)ã�? +
510 CR + "(初期値:false:表示しなã�?" );
511 }
512
513 private static final String[] MODE_LST = new String[] { "ASCII","BINARY" };
514 private static final String[] CMD_LST = new String[] { "GET","PUT","DEL","GETDIR","PUTDIR","DELDIR" };
515
516 /**
517 * こã?クラスの動作確認用の、main メソãƒ?ƒ‰ですã?
518 *
519 * @param args コマンド引数配å?
520 */
521 public static void main( final String[] args ) {
522 Argument arg = new Argument( "org.opengion.fukurou.util.FTPConnect" );
523 arg.setMustProparty( mustProparty );
524 arg.setUsableProparty( usableProparty );
525 arg.setArgument( args );
526
527 FTPConnect ftp = new FTPConnect();
528
529 String host = arg.getProparty( "host"); // FTPサーバã?
530 String user = arg.getProparty( "user" ); // ユーザー
531 String passwd = arg.getProparty( "passwd" ); // パスワーãƒ?
532
533 ftp.setHostUserPass( host , user , passwd );
534
535 ftp.setPort( arg.getProparty( "port" ) ); // 接続に利用するポã?ト番号を設定しますã?
536 ftp.setMode( arg.getProparty( "mode" ,"ASCII",MODE_LST ) ); // 扱ã�?ƒ•ァイルの種類をæŒ?®šしますã?
537 ftp.setPassive( arg.getProparty( "passive" ,true ) ); // パッシブモードを利用するかどã�?�‹
538 ftp.setMkdirs( arg.getProparty( "mkdirs" ,true ) ); // 受け側ファイルにãƒ?‚£レクトリを作æ?するかどã�?�‹
539 ftp.setEncode( arg.getProparty( "encode" ,"UTF-8" ) ); // 日本語ファイル名などのエンコード名を指定しまã�?初期値:UTF-8)
540 ftp.setTimeout( arg.getProparty( "timeout" ,TIMEOUT ) ); // Dataタイãƒ?‚¢ウãƒ?初期値:600 [秒])
541 ftp.setDisplay( arg.getProparty( "display" ,false ) ); // trueは、検索状況を表示しまã�?初期値:false)
542 ftp.setDebug( arg.getProparty( "debug" ,false ) ); // ãƒ?ƒ�ãƒ?‚°æƒ??を標準å?力に表示する(true)かしなã�?false)ã�?
543
544 try {
545 // コネクトしますã?
546 ftp.connect();
547
548 String command = arg.getProparty( "command" ,"GET" ,CMD_LST ); // FTP処ç�??方法をæŒ?®šしますã?
549 String localFile = arg.getProparty( "localFile" ); // ローカルのファイルå�?
550 String remoteFile = arg.getProparty( "remoteFile" ); // FTP先ã?ファイルå�?
551
552 // command , localFile , remoteFile をå?に、SFTP処ç�?‚’行いますã?
553 ftp.action( command,localFile,remoteFile );
554 }
555 catch( RuntimeException ex ) {
556 System.err.println( ftp.getErrMsg() );
557 }
558 finally {
559 // ホストとの接続を終äº?�—ますã?
560 ftp.disconnect();
561 }
562 }
563 }