2012年08月25日
Apache Commons Net
公式サイト: http://commons.apache.org/net/
参考サイト: http://www.syboos.jp/java/doc/jakarta-commons-net-ftpclient.html
------------------------------------------------------------------------------------------------
参考サイト: http://www.syboos.jp/java/doc/jakarta-commons-net-ftpclient.html
------------------------------------------------------------------------------------------------
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) {
}
}
}
}
Posted by kumix at 02:39│Comments(0)
│Java