彩神大发快三_神彩大发快三官方

java FTP 工具类 同步文件夹 下载文件..等操作(FtpHelper 需要commons

时间:2020-01-06 20:11:13 出处:彩神大发快三_神彩大发快三官方

ftp.connect(this.server, this.port);

*/

if (input != null) {

return false;

throw e;

}

remotePath+="/";

ftp.disconnect();

System.out.println("ftp文件 特殊类型"+f.getType());

return ftp;

ftp.setBufferSize(1024);

try {

* @param remoteFilePath

public class FtpHelper {

if (list == null || list.size() == 0) {

for(int i=0;i<lst.size();i++){

}

return ftp;

}

        //输出格式化

public void setPort(int port) {

return false;

Element currentElement = fatherElement; //当前的目录节点

* 关闭FTP连接

public boolean deleteFold(String remoteFoldPath) throws Exception {

List<FTPFile> list = this.getFileList();

public boolean createFold(String remoteFoldPath) throws Exception {

boolean flag = false;

success = ftp.storeFile(newName, input);

if (ftpFile.isDirectory()) {

//ftp.setControlEncoding("UTF-8");

*/

* @param remoteFoldPath

}*/

private int port = 21;

File localFilePath = new File(localPath + File.separator

/*fu.changeDirectory("/2012-07-11/US0000000009");

*/

}

//fu.downloadFile("/21877","《技术服务部制度》修订版V1.4.pdf","D:/workspace/testFTP/ftp") ;

public List<FTPFile> getAllFiles(String remotePath) throws Exception{

if(".".equals(f.getName()) || "..".equals(f.getName())){

return ftp.changeWorkingDirectory(remoteFoldPath);

import org.apache.commons.net.ftp.FTPClientConfig;

boolean success = false;

return conf;

boolean success = false;

FTPListParseEngine engine = ftp.initiateListParsing(remotePath);

if(f.getType()==FTPFile.DIRECTORY_TYPE){

// 文件类型,默认是ASCII

public void setUname(String uname) {

FTPFile f=lst.get(i);

/*import org.dom4j.Document;

//fu.do"ftp://HoPacs:cnhis@192.168.1.137:88/RisImgDir/2016-06-21/US0000000001/US0000000001.jpg"

public String getPassword() {

fu.createDirectoryXML("/", fatherElement);

List<FTPFile> fileList = this.getFileList();

public Boolean uploadFile(String localFile, String newName)

throw e;

* @param path

* 删除文件

}

* 关闭FTP连接

}

File localFilePath = new File(localPath + File.separator

}

String systemKey = FTPClientConfig.SYST_UNIX;//FTPClientConfig.SYST_NT;

        try {

success = ftp.storeFile(newName, input);

*/

System.out.println("类型:"+f.getType()+"  "+f);

* 连接FTP服务器

String localPath) throws Exception {

public List<FTPFile> getFileList(String remotePath) throws Exception {

// 释放空间

* @throws Exception

}

} catch (Exception e) {

success = deleteFold(remoteFoldPath);

throw e;

return fileCount;

System.out.println("文件同步总数量"+count);*/

// 改变当前路径到指定路径

        }

remotePath="/";

fileElement.setText(ftpfile.getName()) ;

if(remotePath==null){

this.server = server;

* 获取FTP服务器[当前工作路径]下的文件列表

}

* 向FTP根目录上传文件

}

}

fu.connectFTPServer();

// 列出当前工作路径下的文件列表

System.out.println(f.getName());

* 向FTP指定路径上传文件

files.add(f);

} catch (Exception e) {

/**

import java.io.InputStream;

        OutputFormat format = OutputFormat.createPrettyPrint();

public InputStream downloadFile(String remoteFilePath) throws Exception {

} else {

FTPFile f=lst.get(i);

}

/**

* Ftp服务器

fu.downloadFile("/RisImgDir/2013-11-27/US0000000013", f.getName(), "D:/workspace/testFTP/ftp");

*/

for(int i=0;i<lst.size();i++){

}

* Set Attribute Method

* @param remoteFoldPath

}else{

/**

List<FTPFile> lst=getFileList(remotePath);

}*/

* @return

* 连接端口,默认21

throws Exception {

input.close();

}

* 密码

public FTPClient getFtp() {

*            新文件名

return success;

// 响应信息

if (!success) {

import org.apache.commons.net.ftp.FTPReply;

* @throws Exception

*/

/**

for(int i=0;i<lst.size();i++){

*            下载文件名

System.out.println("同步文件:"+remotePath+"/"+f.getName()+"  ");

try {

/*try {

import java.util.ArrayList;

} catch (Exception e) {

return false;

this.port = port;

* @param server

}

Element fatherElement = fu.getCurrentElement();

public Boolean uploadFile(InputStream input, String newName)

* @param newName

try {

}

* 改变FTP服务器工作路径 

try {

dir.mkdirs();

*/

*            FTP路径(不中含文件名)

File file = null;

return ftpfiles;

}

}

*/

try {

/*for(FTPFile f: fu.getFileList("/21877")){

* @param password

*/

private String password;

*/

System.err.println("文件下载失败:"+remotePath+" "+fileName);

if (!ftp.login(this.uname, this.password)) {

public Boolean uploadFile(InputStream input, String newName,

}

*/

import org.dom4j.DocumentHelper;

/**

if(ftpfile.isDirectory()){

* @throws Exception

}

this.checkFileExist(localPath);

// 改变工作路径

*/

/**

throws Exception {

/**

private String server;

fileCount++;

public static void main(String[] args) throws Exception {

/*public void saveXML(){

return password;

}

        } catch (IOException e) {

if(f.getType()==FTPFile.DIRECTORY_TYPE){

}else{

* @param remoteFilePath

throw new Exception("文件下载失败!");

throws Exception {

/**

*/

throw new Exception("登录FTP服务器失败,请检查![Server:" + server + "、"

*            新文件名

* @param newName

return success;

* @return

* 主方法(测试)

*/

* 获取FTP服务器上[指定路径]下的文件列表

success = ftp.storeFile(newName, input);

*/

List<FTPFile> allFiles=fu.getAllFiles(null);

conf.setServerLanguageCode(serverLanguageCode);

this.uname = uname;

*/

}

ftp = new FTPClient();

return true;

return uname;

InputStream input = null;

+ fileName);

* @throws Exception

* @param filePath

*/

}

} finally {

*/

/**

/* 

throw new Exception("创建目录失败");

/*public Element getCurrentElement(){

return document.addElement("root");

            e.printStackTrace();

List<FTPFile> ftpfiles = Arrays.asList(engine.getNext(25));

* 从FTP服务器获取文件流

* 获取FTP服务器上指定路径下的文件列表

*/

XMLWriter output = new XMLWriter();

break;

public void setServer(String server) {

this.closeFTPClient(this.ftp);

List<FTPFile> ftpfiles = Arrays.asList(ftp.listFiles(remotePath,new FTPFileFilter() {

BufferedOutputStream output = null;

public List<FTPFile> getFtpServerFileList(String remotePath)

@Override

*            新文件名

String serverLanguageCode = "zh";

boolean success = false;

output.close();

throw new Exception("关闭FTP服务出错!");

}

String remoteFoldPath) throws Exception {

public boolean checkFileExist(String filePath) throws Exception {

System.out.println("遍历"+remotePath+" 耗时 "+(System.currentTimeMillis()-begin.getTime())/60 0d+" s");

localFilePath));

if (!this.changeDirectory(remotePath)) {

import java.io.FileOutputStream;

return false;

* 现象:上传时命名的新文件名没办法有中文,否则上传失败.

for (FTPFile ftpFile : list) {

file = new File(localFile);

}else{

}

*/

}

FtpHelper fu = new FtpHelper("192.168.1.137", 88, "HoPacs","cnhis");

/*

throw new Exception("文件上传失败!");

flag = true;

//同步文件夹

if ((!FTPReply.isPositiveCompletion(replyCode))) {

} catch (Exception e) {

if(!dir.exists()){

ftp = null;

if (!success)

}

System.out.println("同步文件夹:" +remotePath+" "+lst.size());

}

if (input != null) {

FTPClientConfig conf = new FTPClientConfig(systemKey);

/**

/**

* @param remoteFoldPath

* @param filePath

*/

return ftp.deleteFile(remoteFilePath);

input = new FileInputStream(file);

import java.util.List;

//private Document document ;

// 设置被动模式

* @return

* @return

fu.saveXML();

throw e;

if (!flag) {

}

downloadFile(remotePath, f.getName(), localPath);

}

import java.util.Date;

import org.apache.commons.net.ftp.FTPFileFilter;

if(!success){

/**

}else if(f.getType()==FTPFile.FILE_TYPE){

for(FTPFile ftpfile:list){

remotePath="/";

int replyCode = ftp.getReplyCode();

output = new BufferedOutputStream(new FileOutputStream(

}

}

}

* @param remotePath

/**

} finally {

FtpHelper fu = new FtpHelper("192.168.2.18", 21, "administrator","sunshine");

            output = new XMLWriter(new FileWriter("src/com/shine/Ftp/config/dir.xml"), format);

@Override

public void closeFTPClient() throws Exception {

File dir =new File(localPath);

}

fu.connectFTPServer();

ftp.setControlEncoding("GBK");

createDirectoryXML(newRemotePath,dirElement);

* @return

currentElement = dirElement;

}

return server;

if (checkFileExist(localFile)) {

}

} else {

return deleteFold(remoteFoldPath);

ftp.setConnectTimeout(60 00);

this.port = port;

file = new File(localFile);

}else{

}

* 配置FTP连接参数

File dir=new File(localPath);

String name = ftpFile.getName();

//fu.downloadFile("/test","《技术服务部制度》修订版V1.4.pdf","D:/workspace/testFTP/ftp/localftp") ;

public Boolean uploadFile(String localFile, String newName,

dirElement.addAttribute("name",ftpfile.getName());

throw e;

* 保存xml

* @param localPath

* 向FTP指定路径上传文件

public void closeFTPClient(FTPClient ftp) throws Exception {

}

public int getPort() {

/**

remotePath="/"+remotePath;

*/

* @param localFile

/*fu.changeDirectory("RisImgDir/2013-11-27/US0000000013");

* @return

} else {

/**

}));

if (!success) {

}

import org.dom4j.Element;

throw new Exception("文件上传失败!");

System.err.println("文件下载失败:"+remotePath+" "+fileName);

/**

}else if(f.getType()==FTPFile.FILE_TYPE){

int count=fu.synFtpFolder("/2015-08-18","D:/workspace/testFTP/ftp");

} finally {

ftp.configure(getFTPClientConfig());

}*/

package com.esb.component;

System.out.println("处于目录? "+b);*/

this.server = server;

* 向FTP根目录上传文件

String ftpFileName=ftpfile.getName();

/*if (!success) {

public boolean accept(FTPFile f) {

private String uname;

* @param newName

* @return

* @param remoteFoldPath

} finally {

return flag;

input.close();

/**

success = ftp.retrieveFile(ftpFileName, output);

this.ftp = ftp;

}*/

* @throws Exception

List<FTPFile> ftpfiles = Arrays.asList(ftp.listFiles(null,new FTPFileFilter() {

}

if(remotePath==null){

* 删除目录

InputStream input = null;

* 从FTP服务器下载文件

remotePath="/"+remotePath;

} catch (Exception e) {

/**

/*fu.changeDirectory("/2012-07-11/US0000000009");

int fileCount=0;

return ftpfiles;

// 检查本地路径

/**

*            新文件名

input.close();

System.out.println(fu.getFileList("/").size());*/

}

}

/**

*/

* @param ftp

if (input != null) {

return ftp.removeDirectory(remoteFoldPath) ;

boolean b=fu.changeDirectory("/2012-07-11/US0000000009");

}

}

this.changeDirectory(newRemotePath); //从根目录结束了

FTPFile f=lst.get(i);

}

return port;

return files;

output = new BufferedOutputStream(new FileOutputStream(

input.close();

ftp.logout();

throws Exception {

boolean success = false;

* @return

try {

return success;

}

}else{

}

System.out.println(lst.size());

}

/**

System.out.println("服务器路径不存!");

}

            output.close();

}

success = deleteFoldAndsubFiles(remoteFoldPath + "/" + name);

for(FTPFile f: allFiles){

conf.setDefaultDateFormatStr("yyyy-MM-dd");

import org.dom4j.io.OutputFormat;

}

/**

* @param args

}

}else if(f.getType()==FTPFile.FILE_TYPE){

ftp.logout();

}

ftp = null;

public void setPassword(String password) {

success = ftp.storeFile(newName, input);

import org.apache.commons.net.ftp.FTPFile;

*/

break;

File file = new File(filePath);

} finally {

}

/**

* @throws Exception

}

public Boolean downloadFile(String remotePath, String fileName,

}

System.out.println("ftp文件 特殊类型"+f.getType());

+ ftpFileName);

}

* @param localFile

return ftp.retrieveFileStream(remoteFilePath);

System.out.println("当前路径 "+ftp.printWorkingDirectory()+" 服务器路径"+remotePath+"不处于");

if (!success)

ftp.enterLocalPassiveMode();

boolean success = false;

* @throws Exception

* @param remoteFoldPath

throw e;

this.uname = uname;

return false;

System.out.println("异常信息:" + e.getMessage());

//遍历根目录

ftp = null;

if (ftpFileName.equals(fileName)) {

* 用户名

* @return

ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

closeFTPClient();

}

return success;

return success;

import org.apache.commons.net.ftp.FTPClient;

}

document = DocumentHelper.createDocument();

* 生成目录XML文件

System.err.println("ftpFileName.equals(fileName) 不匹配 "+ftpFileName+" "+fileName);

String newRemotePath = remotePath+ftpfile.getName();

+ "User:" + uname + "、" + "Password:" + password);

throw new Exception("文件上传失败!");

if(f.getType()==FTPFile.DIRECTORY_TYPE){

return ftp;

* @throws Exception

}

throw new Exception("文件上传失败!");

public List<FTPFile> getFileList() throws Exception {

}else{

/**

/**

}else if(!remotePath.startsWith("/")){

* @param newName

fu.closeFTPClient();

*/

Element fileElement = fatherElement.addElement("file");//文件节点

public FTPClientConfig getFTPClientConfig() throws Exception {

List<FTPFile> lst=fu.getFileList();

}*/

/**

/**

if (ftp.isConnected())

System.out.println("服务器路径不存!");

this.password = password;

}*/

/**

* @return

}

return success;

public FtpHelper(String server, int port, String uname,

//FtpHelper fu = new FtpHelper("192.168.1.213", 88, "martin","martin");

throw new Exception("本地路径不处于,请检查!");

if (!success) {

* @param input

Date begin=new Date();

public void setFtp(FTPClient ftp) {

}

}

this.password = password;

import java.io.BufferedOutputStream;

* @return

import java.io.FileInputStream;

if (this.port > 0){

File file = null;

import org.dom4j.io.XMLWriter;

if(!dir.exists())

import java.io.File;

// 关闭Ftp连接

success = deleteFtpServerFile(remoteFoldPath + "/" + name);

} catch (Exception e) {

return true;

* @param uname

// 改变当前路径到指定路径

* 删除目录以及文件

* @return

public Boolean deleteFtpServerFile(String remoteFilePath) throws Exception {

List<FTPFile> lst=getFileList(remotePath);

* @param input

if (input != null) {

* @throws Exception

}

try {

}else if(!remotePath.startsWith("/")){

public boolean deleteFoldAndsubFiles(String remoteFoldPath)

* @throws Exception

dir.mkdirs();

public Boolean changeDirectory(String remoteFoldPath) throws Exception {

localFilePath));

public String getUname() {

ftp.disconnect();

public int synFtpFolder(String remotePath,String localPath) throws Exception{

public boolean accept(FTPFile f) {

}

}

System.out.println("ftp文件 特殊类型"+f.getType());

}

boolean flag = ftp.makeDirectory(remoteFoldPath);

}

* @throws Exception 

if (!this.changeDirectory(remoteFoldPath)) {

}*/

*/

}

*/

if (fileList == null || fileList.size() == 0) {

}

if(!success){

}

return false;

}

if (output != null) {

*/

}

boolean success = false;

Element dirElement = fatherElement.addElement("dir") ;

* 检查本地路径不是处于

* @throws Exception

List<FTPFile> list = this.getFileList(remoteFoldPath);

}

* @throws Exception

//初始化

fileCount+=synFtpFolder((remotePath=="/"?"":remotePath)+"/"+f.getName(),localPath+"/"+f.getName());

}

* @param fileName

import java.util.Arrays;

if (!success) {

/**

if (!this.changeDirectory(remoteFoldPath)) {

* @throws Exception

*/

System.out.println("下载成功 (耗时:"+(System.currentTimeMillis()-begin.getTime())/60 0d+" s):"+remotePath+"/"+fileName+"-> "+localPath+"/"+fileName);

}*/

input = new FileInputStream(file);

private FTPClient ftp = null;

}

}));

String remoteFoldPath) throws Exception {

*            本地路径

} catch (Exception e) {

/*public void createDirectoryXML(String remotePath,Element fatherElement) throws Exception{

} catch (Exception e) {

}

return false;

* Get Attribute Method

public FTPClient connectFTPServer() throws Exception {

success = ftp.retrieveFile(fileName, output);

}

* 创建目录

}

Date begin=new Date();

List<FTPFile> files=new ArrayList<>();

/*for (FTPFile ftpfile : fileList) {

}

* 创建XML文件

}*/

}

public String getServer() {

return success;

System.out.println("服务器当前路径下不处于文件!");

if (!file.exists()) {

*/

            output.write(this.document);

}*/

* @throws Exception

* @param path

return false;

if (checkFileExist(localFile)) {

}

if (!success)

* @param remoteFoldPath

System.out.println(f.getName()+" "+(f.getTimestamp()==null?null:f.getTimestamp().getTime().toLocaleString()));

files.addAll(getAllFiles((remotePath=="/"?"":remotePath)+"/"+f.getName()));

return ftpfiles;

String password){

热门

热门标签