package lis.co.jp.java.common;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* FTP管理クラス
*
*/
public class FtpUtil {
public FTPClient ftpClient = null;
/**
* コンストラクタ
*/
public FtpUtil() {
ftpClient = new FTPClient();
}
/**
* FTPサーバーの接続
* @param hostname サーバー名
* @param port ポート
* @param username ユーザー名
* @param password パスワード
* @return 処理結果を真偽で返します。
* @throws IOException
*/
public boolean connect(String hostname, String username,String password) throws IOException{
ftpClient.connect(hostname, 21);
ftpClient.enterLocalPassiveMode();
ftpClient.setControlEncoding("SHIFT_JIS");
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username, password)){
return true;
}
}
closeConnect();
return false;
}
/**
* ファイル内容全てファイルを取得。
* @param dirPath ファイルパス
* @return ファイルリスト
* @throws IOException
*/
public FTPFile[] getListFile(String dirPath) throws IOException {
FTPFile[] files = null;
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
dirPath = new String(dirPath.getBytes("SHIFT_JIS"),"ISO-8859-1");
try {
files = ftpClient.listFiles(dirPath);
} catch (IOException e) {
e.printStackTrace();
}
return files;
}
/**
* ファイルのバイナリデータを取得。
* @param filePath ファイルパス
* @return ファイルのバイナリデータ
* @throws IOException
*/
public InputStream getInputstream(String filePath) throws IOException {
InputStream in = null;
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
filePath = new String(filePath.getBytes("SHIFT_JIS"),"ISO-8859-1");
try {
in = ftpClient.retrieveFileStream(filePath);
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
/**
* 削除フォルダとフォルダ内のファイル
* @param dirPath フォルダのパス
* @return 処理結果を真偽で返します。
* @throws IOException
*/
public boolean deleteDirectory(String dirPath) throws IOException {
boolean result = true;
ftpClient.enterLocalPassiveMode();
dirPath = new String(dirPath.getBytes("SHIFT_JIS"),"ISO-8859-1");
try {
FTPFile[] files = ftpClient.listFiles(dirPath);
for (int i = 0; i < files.length; i++) {
FTPFile file = files[i];
String name = file.getName();
name = new String(name.getBytes("SHIFT_JIS"),"ISO-8859-1");
if (file.isDirectory()) {
result = deleteDirectory(dirPath + "\\" + name);
if (!result) {
break;
}
} else {
result = ftpClient.deleteFile(dirPath + "\\" + name);
if (!result) {
break;
}
}
}
if (!result) {
return false;
}
result = ftpClient.removeDirectory(dirPath);
} catch (Exception e) {
return false;
}
return result;
}
/**
* FTPの接続を解放します。
* @throws IOException
*/
public void closeConnect() throws IOException {
if(ftpClient.isConnected()){
ftpClient.disconnect();
}
}
/**
* バイナリデータを解放します。
* @param in
*/
public void close(InputStream in) {
try {
in.close();
ftpClient.completePendingCommand();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ps:特别注意的是ftpClient.completePendingCommand();要写在in.close();之后,否则有可能出现reset的问题。