edtFTPj
edtFTPj是一个FTP客户端包它可以嵌入到应用程序中以便以提供FTP的功能.它提供一些方法如:put(),get(),chdir(),dir()与pwd()等来完成FTP的功能.
通过对其的学习,尤其是取FILENAME是一个大的挑战,我曾经为这个走了很多的冤枉路,我对其代码总结。
package com.throuhout.j2eemodel.util;
/**
* 保留Enterprisedt公司信息
* Copyright (C); 2000-2004 Enterprise Distributed Technologies Ltd
*
* www.enterprisedt.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option); any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Bug fixes, suggestions and comments should be sent to support@enterprisedt.com
* nether add by heweiya
* 对FTP服务器的通用操作
* ConnFTPServer(String host,String username,String password);
* Upload(String localfile,String filename);
* Download(String localpath);
*
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPMessageCollector;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.util.debug.Logger;
/**
* Simple test class for FTPClient
*
* @author Hans Andersen
* @author Bruce Blackshaw
*/
public class FtpClient {
/**
* Log stream
*/
private static Logger log = Logger.getLogger(FtpClient.class);;
private FTPClient ftp = null;
public void ConnFTPServer(String host, String username, String password); {
try {
// set up client
ftp = new FTPClient();;
ftp.setRemoteHost(host);;
FTPMessageCollector listener = new FTPMessageCollector();;
ftp.setMessageListener(listener);;
//ftp.setAutoPassiveIPSubstitution(true);;
// connect
log.info("Connecting");;
ftp.connect();;
// login
log.info("Logging in");;
ftp.login(username, password);;
// set up passive ASCII transfers
log.debug("Setting up passive, ASCII transfers");;
ftp.setConnectMode(FTPConnectMode.ACTIVE);;
ftp.setType(FTPTransferType.ASCII);;
} catch (Exception e); {
log.error("Demo failed", e);;
}
}
public void Upload(String localfile, URL url); {
String host = url.getHost();;
int port = url.getPort();;
File filePath = new File(url.getPath(););;
String directory = filePath.getParent();.substring(1);;
String filename = filePath.getName();;
try {
//connect(host, port);;
ftp.chdir(directory);;
ftp.put(new FileInputStream(localfile);, filename);;
} catch (Exception e); {
log.error("Demo failed", e);;
}
}
public void Upload(String localpath,String filename); {
try {
ftp.put(localpath, filename);;
} catch (Exception e); {
log.error("Demo failed", e);;
}
}
public void DownloadAll(String localpath);{
try {
ftp.chdir("xml");;
String[] files = ftp.dir(".", true);;
for (int i = 0; i < files.length; i++); {
String testfile = files[i].substring(39);;//大家可以看到这里,testfile取得不是filename而是一个字条串,我只是从开始的位置取得FILENAME,方法可能很笨,但是我不知道如何来取得真正的FILENAME。
log.debug(files[i]);;
ftp.get(new FileOutputStream(localpath+testfile);,testfile);;
//ftp.get(localpath + testfile, testfile);;
}
} catch (IOException e); {
// TODO 自动生成 catch 块
e.printStackTrace();;
} catch (FTPException e); {
// TODO 自动生成 catch 块
e.printStackTrace();;
}
}
public void DisConn();{
try {
ftp.quit();;
} catch (IOException e); {
// TODO 自动生成 catch 块
e.printStackTrace();;
} catch (FTPException e); {
// TODO 自动生成 catch 块
e.printStackTrace();;
}
}
}
分享到:
相关推荐
机械博物馆的开源代码:Lockpicking_C#_ShaderLab_下载.zip
开源代码使用指南 开源代码(Open Source):是促进软件发展的一种协作方式。与商用软件不同,它公开源代码, 允许用户直接拿来学习、开发。使用Open Source的应用受到Open Source的许可证限制,如有些 Open ...
分布式仿真协议HLA、RTI案例RTI-航空案例.rar
开源OScommerce:轻松架设专业电子商务平台>>源代码
GIS开源代码GIS开源代码GIS开源代码 用于空间分析。底层代码。保罗了GIS的主要功能
2、图文编辑;...4、自定义菜单;以及扩展功能:1、微信的图文列表,2、分类以及微网站服务。为了能够让每篇文章、分类都能够通过...www.yimi.ch CSDN开源代码库:https://code.csdn.net/u014491629/yimi-weixinportal
真实世界的React Native应用及其开源代码库供开发人员学习
这是工业通信modbus的免费开源代码,主要用于工业通信协议开发人员,参考学习之用。代码比较全,支持linux和arm处理器。
H.264 开源代码 (作者:许可),是从正规网站下载的,没经过修改,绝代原版。
PHPOA为国内首家开源OA软件,采用领先的B/S架构、跨平台语言(php+mysql)等灵活应用,是中国用户群最广泛的OA系统,
SumatraPDF 开源项目的源代码
有限元开源代码 FEAP伯克利_Fortran_混合有限元,用于伯克利大学有限元课程教学使用。
远程桌面开源代码, 远程桌面开源代码, 远程桌面开源代码,重要的事情说三遍,完整project
有关开源DRM的开源项目:OpenIPMP业务模式简介
资源名称:WPF优秀开源代码合集资源目录:【】caliburnmicro-5f63ca29da0c【】chronoswpf-96133【】dynamicdatadisplay-94877【】fluent-72f5e3ec252b【】graphsharp-9c108fe2dfa4【】helixtoolkit-8c0d58aa0295【IT...
有限元开源代码FreeFEM++,个人感觉是基于OOFEM开发的开源代码。
Android的四种播放器开源代码。。。
Python开源项目之人工智能老照片修复算法学习 Bringing-Old-Photos-Back-to-Life-master完整代码,只为更加节省各位开发者的时间,开箱即用,无需烦恼各种确实配置等。 开源代码:...
LookAround开源代码移动互联网客户端使用众多第三方库做客户端开发初步可以学习借鉴。.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
贪吃蛇小游戏,java开源代码,仅供学习,此是小编最近更新的版本9.0,欢迎下载学习