Jakarta Commons NETによるFTP処理(FTPClient)
2009/01/28 19:40Update
Jakarta Commons NET(FTPClient)によるFTP処理(ファイルのアップロード、ダウンロード)の例です。
Apache Commons Net(FTPClient)について
サンプル
package com.test.ftp;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpClientHelper {
private static final int FTP_PORT = 21;
public static void main(String[] args) {
try {
//ファイルアップロード
FileInputStream fis = new FileInputStream("c:\testftp.txt");
FtpClientHelper.sendFile("localhost", FTP_PORT, "testuser", "testpassword",
"remoteFilename", fis);
//ファイルダウンロード
FileOutputStream fos = new FileOutputStream("localfile");
FtpClientHelper.retrieveFile("localhost", FTP_PORT, "testuser", "testpassword",
"remoteFilename", fos);
} catch (Exception e) {
e.printStackTrace();
}
}
//ファイルアップロード
public static void sendFile (String host,
int port,
String user,
String password,
String remoteFilename,
InputStream is
) throws Exception {
FTPClient ftpclient = new FTPClient();
try {
//指定するホスト、ポートに接続します
ftpclient.connect(host, port);
int reply = ftpclient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
//接続エラー時処理
Exception ee = new Exception("Can't Connect to :" + host);
throw ee;
}
//ログイン
if (ftpclient.login(user, password) == false) {
// invalid user/password
Exception ee = new Exception("Invalid user/password");
throw ee;
}
//ファイル転送モード設定
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
//ftpclient.cwd("filetype=pdf");
//ファイル転送
ftpclient.storeFile(remoteFilename, is);
//"ファイル転送完了
// ファイル受信
FileOutputStream fos = new FileOutputStream("localfile");
ftpclient.retrieveFile("remotefile", fos);
} catch (IOException e) {
//TODO エラー処理
throw e;
} finally {
try {
ftpclient.disconnect(); //接続解除
} catch (IOException e) {
}
}
}
//ファイルダウンロード
public static void retrieveFile(String host,
int port,
String user,
String password,
String remoteFilename,
OutputStream os) throws Exception {
FTPClient ftpclient = new FTPClient();
try {
//指定するホスト、ポートに接続します
ftpclient.connect(host, port);
int reply = ftpclient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
//接続エラー時処理
Exception ee = new Exception("Can't Connect to :" + host);
throw ee;
}
//ログイン
if (ftpclient.login(user, password) == false) {
// invalid user/password
Exception ee = new Exception("Invalid user/password");
throw ee;
}
//ファイル転送モード設定
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
//ftpclient.cwd("filetype=pdf");
// ファイル受信
ftpclient.retrieveFile(remoteFilename, os);
} catch (IOException e) {
//TODO エラー処理
throw e;
} finally {
try {
ftpclient.disconnect(); //接続解除
} catch (IOException e) {
}
}
}
}
.
Sponsored Link
- Relative Articles
- Java言語の基礎 - javacコマンドによるコンパイル - (2008/10/10 17:58)
- Javaでpropertiesファイルの読み書き処理(1) - (2009/01/24 16:36)
- URLから画像を取得してファイルに書き込み 例 - (2009/01/22 16:40)
- リソースファイルの国際化対応 - java.util.ResourceBundle - (2009/01/24 22:41)
- Javaで指定するフォルダにあるすべてのファイルをソートするには - (2009/01/24 23:27)
- FileReader/FileWriterでテキストファイルの読み書き - (2009/01/28 17:57)
- DTDファイル処理 - DTDParser - Java DTD パーサー - (2009/01/30 17:48)
- Apache Commons Digesterを使おう - サンプルから学ぶXML文書の読み込み - (2009/02/09 17:09)
- リソースからInputStreamの取得するための汎用的なサンプル - (2009/02/16 17:29)
- InputStreamオブジェクトにあるデータをファイルに出力 - (2009/02/16 18:07)