|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-02-12
项目需要实现从某银行的ftp服务器上定时下载数据文件的模块.于是使用apache的commons-net包实现ftp服务器的链接 中间使用了commons的io和Oro包.Io包封装了一些使用的方法,eg:安全的关闭各种流.根据文件的长度得到文件的方便识别的大小(k,m.g).还有一些其它使用的方法,读者可以下载后自行研究.. commons-net包封装了一些常见的网络包:ftp,smtp,pop3等..本文使用了ftp包的功能. 代码如下: java 代码
寥寥几行实现了ftp服务器某个目录下的所有文件.当然本例中没有处理中文的问题,所以如果ftp上存在中文目录,则会出现乱码. 通过获得ftp的流,可以将文件下载到本地.配合正则表达式,可以实现从服务器上下载匹配规则的文件.... 欢迎交流.. 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-02-12
不错不错,好东西。有空要仔细看一看的。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-02-12
不错,apache的那些commons包很灵。
楼主有否考虑写ant的build.xml,让ant来搞定? |
|
| 返回顶楼 | |
|
最后更新时间:2007-02-12
连上服务器只是第一步,还需要定义一个job来定时执行.所以不能像楼上说的那样.
|
|
| 返回顶楼 | |
|
最后更新时间:2007-02-12
有没有上传功能,如果有,那文件共享又多了一种思路,通过FTP服务器实现文件共享,简单方便!
|
|
| 返回顶楼 | |
|
最后更新时间:2007-02-13
不错,收着。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-02-13
package org.uranus.tools.net.ftp;
import java.io.InputStream; import java.util.Properties; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPReply; import org.uranus.util.StringUtils; /** * 基于apache组织的commonNet开源组件实现ftp客户端<br> * 本程序运行依赖于一个config成员变量属性,其是一个属性(Properties)对象<br> * 系统要求这个config对象,必需有如下属性key:<br> * server(ftp服务器ip地址或域名)<br> * username(登录用户名)<br> * password(登录密码)<br> * 如下属性key是可选的:<br> * systemKey(ftp服务器的操作系统key,如window)<br> * serverLanguageCode(ftp服务器使用的语言,如zh)<br> * <br> * 本程序是线程安全,在每一个上传时都将创建ftp连接,上传结束后再关闭ftp连接<br> * * 例子:<br> * InputStream localIn = new FileInputStream("c:\\计算机.txt");<br> * * java.util.Properties config = new Properties();<br> * config.setProperty("server", "192.168.5.120");<br> * config.setProperty("username", "upload");<br> * config.setProperty("password", "upload");<br> * * FtpClientCommonNetImpl client = new FtpClientCommonNetImpl();<br> * client.setConfig(config);<br> * client.upload(localIn, "/aaa/计算机.txt", true);<br> * * * @author zhangdb * */ public class FtpClientCommonNetImpl implements IFtpClient { // ftp配置 private Properties config; /** * 连接到ftp服务器 * * @param server * @param userName * @param password * @return * @throws FtpException */ protected FTPClient connectFtpServer(String server, String userName, String password) throws FtpException { // 创建ftp客户端对象 FTPClient ftp = new FTPClient(); try { ftp.configure(this.getFTPClientConfig()); // 连接ftp服务器 ftp.connect(server); // 登录 ftp.login(userName, password); // 返回值 int reply = ftp.getReplyCode(); if ((!FTPReply.isPositiveCompletion(reply))) { ftp.disconnect(); throw new FtpException("登录ftp服务器失败,请检查server[" + server + "]、username[" + userName + "]、password[" + password + "]是否正确!"); } return ftp; } catch (Exception ex) { throw new FtpException(ex); } } /** * 关闭连接 * * @param ftp */ protected void disconnectFtpServer(FTPClient ftp) { try { ftp.logout(); ftp.disconnect(); } catch (Exception ex) { throw new FtpException(ex); } } /** * 上传 */ public void upload(InputStream localIn, String remoteFilePath) throws FtpException { String server = this.config.getProperty("server"); String userName = this.config.getProperty("username"); String password = this.config.getProperty("password"); FTPClient ftp = this.connectFtpServer(server, userName, password); try { boolean result = ftp.storeFile(this .enCodingRemoteFilePath(remoteFilePath), localIn); if (!result) { throw new FtpException("文件上传失败!"); } } catch (Exception ex) { throw new FtpException(ex); } finally { this.disconnectFtpServer(ftp); } } /** * 上传结束以后关闭输入流 * * @param localIn * @param remoteFilePath * @param afterUploadCloseInputStream * @throws FtpException */ public void upload(InputStream localIn, String remoteFilePath, boolean afterUploadCloseInputStream) throws FtpException { try { // 上传 this.upload(localIn, remoteFilePath); } finally { if (afterUploadCloseInputStream) { if (localIn != null) { try { localIn.close(); } catch (Exception ex) { throw new FtpException(ex); } } } } } /** * 得到配置 * * @return */ protected FTPClientConfig getFTPClientConfig() { // 创建配置对象 FTPClientConfig conf = new FTPClientConfig(this.config.getProperty( "systemKey", FTPClientConfig.SYST_NT)); conf.setServerLanguageCode(this.config.getProperty( "serverLanguageCode", "zh")); return conf; } /** * 远程文件路径编码(上传到ftp上的文件路径) * * @param remoteFilePath * @return */ protected String enCodingRemoteFilePath(String remoteFilePath) { return StringUtils.gbkToIso8859EnCoding(remoteFilePath); } public Properties getConfig() { return config; } public void setConfig(Properties config) { this.config = config; } } |
|
| 返回顶楼 | |
|
最后更新时间:2007-02-13
这个办法不错
以前做过一个通过定时器访问SAMBA目录读取数据文件的实现 |
|
| 返回顶楼 | |
|
最后更新时间:2007-02-13
package org.darrenstudio.wrapper.ftp;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
/**
* A Wrapper Class to Commons Net's FTPClient to ease the ftp transfer process
* so Jakarta Commons Net needed.
* @author Darren.Wang
* @since 1.0 (2005-06-13)
*/
public class FtpTemplate {
private FTPClientConfig ftpClientConfig;// optional
//
private String server;// required
private String username; // required
private String password; // required
//
public FtpTemplate(String server,String user,String pwd)
{
this.server = server;
this.username = user;
this.password = pwd;
}
public void transfer(FtpTransferCallback callback) throws IOException
{
FTPClient ftp = new FTPClient();
try {
if(this.getFtpClientConfig() != null)
ftp.configure(this.getFtpClientConfig());
ftp.connect(server);
// check whether the connection to server is confirmed
int reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
throw new IOException("failed to connect to the FTP Server:"+server);
}
// login
ftp.login(this.getUsername(),this.getPassword());
// do your ftp operations in call back method
callback.doTransfer(ftp);
// logout
ftp.logout();
}
finally
{
if(ftp.isConnected())
{
ftp.disconnect();
}
}
}
// Setters and getters
public FTPClientConfig getFtpClientConfig() {
return ftpClientConfig;
}
public void setFtpClientConfig(FTPClientConfig ftpClientConfig) {
this.ftpClientConfig = ftpClientConfig;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
|
|
| 返回顶楼 | |
|
最后更新时间:2007-04-09
有没有处理中文文件夹或者中文文件名的dd
|
|
| 返回顶楼 | |











