package com.repkin.ftp;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.commons.vfs.CacheStrategy;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.Selectors;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileSystemManager;
import org.apache.commons.vfs.provider.ftp.FtpFileSystemConfigBuilder;
import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder;
public class FtpManagerImpl {
private FileObject rootDirFileObject;
private FileObject currentDirFileObject;
public static DefaultFileSystemManager fsManager = null;
static {
try {
fsManager = (DefaultFileSystemManager) VFS.getManager();
fsManager.setCacheStrategy(CacheStrategy.ON_RESOLVE);
} catch (Exception e) {
}
}
public FtpManagerImpl(String uriString) throws CommonFtpException {
try {
FileSystemOptions fileSystemOptions = new FileSystemOptions();
if(uriString.startsWith("sftp")) {
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fileSystemOptions, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fileSystemOptions, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(fileSystemOptions, 180000);
} else if(uriString.startsWith("ftp")) {
FtpFileSystemConfigBuilder.getInstance().setPassiveMode(fileSystemOptions, true);
FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fileSystemOptions, true);
FtpFileSystemConfigBuilder.getInstance().setDataTimeout(fileSystemOptions, 180000);
}
rootDirFileObject = fsManager.resolveFile(uriString, fileSystemOptions);
if(rootDirFileObject == null || !rootDirFileObject.exists()){
throw new CommonFtpException("ftp.connection.remote.path.does.not.exist");
}
}catch(Exception e){
throw new CommonFtpException("ftp.connection.undefined.exception"+(e == null ? "" : " ," + e.getMessage()));
}
}
public void closeConnection() throws CommonFtpException {
if(fsManager != null) {
try{
fsManager.freeUnusedResources();
fsManager.close();
}catch(Exception e){
throw new CommonFtpException("ftp.connection.can.not.be.closed",e);
}
}
fsManager = null;
}
private void goStartDirectory() throws CommonFtpException {
try {
fsManager.resolveName(rootDirFileObject.getName(), "");
} catch (Exception e) {
throw new CommonFtpException("ftp.connection.cant.back.to.start.directory");
}
}
public List listFilesInDirectory(String workingDirectory, String fileName, String prefix, Calendar startTime, Calendar endTime) throws CommonFtpException {
List fileList = new ArrayList();
if(currentDirFileObject == null) {
currentDirFileObject = rootDirFileObject;
}
try {
if(workingDirectory != null) {
if(workingDirectory.startsWith("/")){
goStartDirectory();
workingDirectory = workingDirectory.substring(1);
}
if( (currentDirFileObject = fsManager.resolveFile(rootDirFileObject, workingDirectory)) == null || !currentDirFileObject.exists()) {
throw new CommonFtpException("unable.chg.dir");
}
}
FileObject[] ftpFileObjectArray = currentDirFileObject.findFiles(Selectors.SELECT_FILES);
for(int i=0; i ftpRemoteFileList = listFilesInDirectory(spec.getRemoteDirectory(), spec.getRemoteFile(), null, null, null);
if(ftpRemoteFileList == null || ftpRemoteFileList.size() == 0) {
throw new CommonFtpException("specified.remote.file.not.found");
}
if(spec.getLocalFile() != null && ftpRemoteFileList.size() != 1) {
throw new CommonFtpException("more.than.specified.remote.file");
}
try {
for (FileObject remoteFileObject : ftpRemoteFileList) {
String localFileName;
if(StringUtil.isLengthful(spec.getLocalFile())) {
localFileName = StringUtil.asNotNullString(spec.getLocalDirectory()) + StringUtil.asNotNullString(spec.getLocalFile());
} else {
localFileName = StringUtil.asNotNullString(spec.getLocalDirectory()) + StringUtil.asNotNullString(remoteFileObject.getName().getBaseName());
}
File localFile = new File(localFileName);
if(localFile.exists()) {
localFile.delete();
}
localFile.createNewFile();
FileOutputStream fos = new FileOutputStream(localFile);
BufferedInputStream bis = new BufferedInputStream(remoteFileObject.getContent().getInputStream());
int content;
while ((content = bis.read()) != -1) {
fos.write(content);
}
if(spec.isDeleteFromSource()) {
remoteFileObject.delete();
}
fos.close();
fos = null;
bis.close();
bis = null;
}
} catch (Exception e) {
throw new CommonFtpException("exception.occured.on.retrieve.file",e);
}
}
public void storeFile(FileTransferSpecification spec) throws CommonFtpException {
if( spec == null || (spec.getLocalDirectory() == null && spec.getLocalFile() == null) ){
throw new CommonFtpException("file.transfer.specification.not.specified");
}
ArrayList ftpSendFileList = new ArrayList();
if(spec.getLocalDirectory() != null) {
File[] localFileList = new File(spec.getLocalDirectory()).listFiles();
if(localFileList != null && localFileList.length>0){
for(int i=0; i ftpRemoteFileList = listFilesInDirectory(spec.getRemoteDirectory(), spec.getRemoteFile(), null, null, null);
for(int i=0; i=0){
if(localFile.length() == ftpRemoteFileList.get(inServer).getContent().getSize()){
localFile = null;
continue;
} else {
ftpRemoteFileList.get(inServer).delete();
}
}
FileObject localFileObject = fsManager.resolveFile(localFile, "");
FileObject remoteFileObject = fsManager.resolveFile(currentDirFileObject,remoteFileName);
remoteFileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);
}
ftpRemoteFileList = null;
}catch(Exception e){
e.printStackTrace();
throw new CommonFtpException("exception.occured.on.file.transfer");
}finally{
}
}
public void removeRemoteFile(FileTransferSpecification spec) throws CommonFtpException {
if( spec == null || (spec.getRemoteDirectory() == null && spec.getRemoteFile() == null) ){
throw new CommonFtpException("file.transfer.specification.not.specified");
}
try{
List ftpRemoteFileList = listFilesInDirectory(spec.getRemoteDirectory(), spec.getRemoteFile(), null, null, null);
for (FileObject fileObject : ftpRemoteFileList) {
if(!fileObject.delete()) {
throw new CommonFtpException("exception.file.can.not.be.deleted");
}
}
}catch(Exception e){
throw new CommonFtpException("exception.occured.on.file.delete");
}
}
public static void main(String[] args) {
try {
// ftp://[ username [: password ]@] hostname [: port ][ absolute-path ]
String userName = "ftp_test_user";
String pass = "password";
String serverIP = "127.0.0.1";
String serverPath = "ftp_test"; //Relative to user root directory
StringBuffer uri = new StringBuffer();
//uri.append("sftp://" + userName + ":" + pass + "@" + serverIP + "/" + serverPath);
uri.append("ftp://" + userName + ":" + pass + "@" + serverIP +":21"+ "/" + serverPath);
FtpManagerImpl ftpManager = new FtpManagerImpl(uri.toString());
// ftpManager.retrieveFile(new FileTransferSpecification("/", "Trigonometric_functions.xls", "/home/repkin/ftp_download/", "retrived_file.test", false));
// ftpManager.retrieveFile(new FileTransferSpecification("/", null, "/home/repkin/ftp_download/", null, false));
// ftpManager.storeFile(new FileTransferSpecification(null, "ftp_test_4/letter_3_test", null, "/home/repkin/ftp_downloaspec)d/letter_2_test", false));
ftpManager.removeRemoteFile(new FileTransferSpecification("ftp_test_4", "letter_3_test", null, null, false));
ftpManager.closeConnection();
} catch (CommonFtpException e) {
e.getMessage();
e.printStackTrace();
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}
}
}
15 Ocak 2014 Çarşamba
Example Usage of Common VFS
Kaydol:
Kayıt Yorumları (Atom)
Hiç yorum yok:
Yorum Gönder