- 浏览: 772285 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
wangyudong:
新版本 Wisdom RESTClienthttps://gi ...
jersery client调用REST框架web services服务的一个示例 -
wangyudong:
很多REST Client是不支持自动化测试RESTful A ...
jersery client调用REST框架web services服务的一个示例 -
doubledumbao:
感谢你的代码,在使用中发现两处小问题,已经做了修改,再次感谢。 ...
Java Zip Utils 压缩/解压缩工具包 -
doubledumbao:
package com.ry.messagedigest;
...
Java Zip Utils 压缩/解压缩工具包 -
phrmgb:
对wsdl讲解的很细致,收藏
Web Service (二) WSDL详解
edtFtp是一个开源的纯Java的FTP客户端类库。由于它实现了FTP协议,程序员可以通过它对远程FTP服务器进行访问,而不用再手工编写各种命令和处理服务器的回应。下面是我在实现开发中实现的一个FTP操作类,将其记录在此,方便以后使用。源代码如下:
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.enterprisedt.net.ftp.FTPException; import com.enterprisedt.net.ftp.FTPFile; import com.enterprisedt.net.ftp.FileTransferClient; import com.enterprisedt.net.ftp.FileTransferOutputStream; /** * FTP服务器操作工具类 * * @author XXX * */ public class FtpService { private static final Log logger = LogFactory.getLog(FtpService.class .getName()); // 部分静态常量 private static final String SYSTEM_CONFIG_ITEM_FTPIP = "ip"; private static final String SYSTEM_CONFIG_ITEM_FTPPORT = "port"; private static final String SYSTEM_CONFIG_ITEM_FTPLOGINNAME = "username"; private static final String SYSTEM_CONFIG_ITEM_FTPLOGINPWD = "password"; private static final String SYSTEM_CONFIG_ITEM_PARAM_TYPE = "marketingActiveFtpParam"; // FTP服务器IP地址 private String ftpServiceIP; // FTP服务器端口 private int ftpServicePort; // 登录账号 private String loginUser; // 登录密码 private String loginPwd; // FTP客户端对象 private FileTransferClient ftpClient = null; // 单例对象 private static FtpService ftpService = null; /** * 获取单例对象方法 * * @return * @throws Exception */ public static synchronized FtpService getInstance() throws Exception { try { if (ftpService == null) { logger.info("从数据库获取FTP服务器相关配置,创建FTP客户端"); String ftpServiceIP = SystemData.getParameterAPI().getParameterValue(SYSTEM_CONFIG_ITEM_PARAM_TYPE,SYSTEM_CONFIG_ITEM_FTPIP); int ftpServicePort = Integer.parseInt(SystemData.getParameterAPI().getParameterValue(SYSTEM_CONFIG_ITEM_PARAM_TYPE,SYSTEM_CONFIG_ITEM_FTPPORT)); String loginUser = SystemData.getParameterAPI().getParameterValue(SYSTEM_CONFIG_ITEM_PARAM_TYPE,SYSTEM_CONFIG_ITEM_FTPLOGINNAME); String loginPwd = SystemData.getParameterAPI().getParameterValue(SYSTEM_CONFIG_ITEM_PARAM_TYPE,SYSTEM_CONFIG_ITEM_FTPLOGINPWD); ftpService = new FtpService(ftpServiceIP, ftpServicePort, loginUser, loginPwd); } } catch (Exception e) { throw new Exception("获取FTP服务器实例时异常,FTP服务器配置错误,请检查" + e); } return ftpService; } /** * 为了系统参数修改后,能创建新的Ftp客户端对象,特增加了此方法 * * @return * @throws Exception */ public static synchronized FtpService getInstance(boolean isNew) throws Exception { try { if(isNew) { logger.info("从数据库获取FTP服务器相关配置,创建FTP客户端"); String ftpServiceIP = SystemData.getParameterAPI().getParameterValue(SYSTEM_CONFIG_ITEM_PARAM_TYPE,SYSTEM_CONFIG_ITEM_FTPIP); int ftpServicePort = Integer.parseInt(SystemData.getParameterAPI().getParameterValue(SYSTEM_CONFIG_ITEM_PARAM_TYPE,SYSTEM_CONFIG_ITEM_FTPPORT)); String loginUser = SystemData.getParameterAPI().getParameterValue(SYSTEM_CONFIG_ITEM_PARAM_TYPE,SYSTEM_CONFIG_ITEM_FTPLOGINNAME); String loginPwd = SystemData.getParameterAPI().getParameterValue(SYSTEM_CONFIG_ITEM_PARAM_TYPE,SYSTEM_CONFIG_ITEM_FTPLOGINPWD); ftpService = new FtpService(ftpServiceIP, ftpServicePort, loginUser, loginPwd); } else getInstance(); } catch (Exception e) { throw new Exception("获取FTP服务器实例时异常,FTP服务器配置错误,请检查" + e); } return ftpService; } /** * 获取指定路径下指定目录或文件名称列表 * * @param dir:指定路径 * @param directoryName:指定的目录名称,全称或部分 * @return:名称列表 */ public synchronized List<String> getDirectoryListByDir(String dir, String directoryName) throws Exception { logger .info(String.format("从FTP服务上获取指定路径下的目录或文件名称列表,路径[%s],目录或文件名称[%s]。", dir, directoryName)); List<String> allDirectorys = null; try { ftpClient.changeDirectory(dir); FTPFile[] files = ftpClient.directoryList(directoryName.trim()); int length = files.length; if (length > 0) { allDirectorys = new ArrayList<String>(); for (int i = 0; i < length; i++) { allDirectorys.add(files[i].getName().trim()); } } changeToRootDirectoryByDir(dir); } catch (Exception e) { throw new Exception("获取指定路径下指定目录名称列表时发生异常,原因:" + e); } return allDirectorys; } /** * 将FTP服务器指定目录下的指定文件下载到本地指定的文件中并将FTP上的文件删除掉 * * @param dir:FTP服务器上目录 * @param localFileName:存放到本地的文件名称(包含存放的路径,如无路径则默认为当前路径) * @param remoteFileName:FTP服务器上的文件名称 */ public synchronized void downloadRemoteFileToLocalAndDeleteIt(String dir, String localTempProcessFileName, String localBackupFileName, String remoteFileName) throws Exception { logger .info(String .format( "将FTP服务器指定目录下的指定文件下载到本地指定的文件中并将FTP上的文件删除掉,FTP服务器目录[%s],远程文件名[%s],本地文件名[%s]。", dir, remoteFileName, localTempProcessFileName)); try { ftpClient.changeDirectory(dir); boolean isExists = ftpClient.exists(remoteFileName); if (!isExists) { logger.info(remoteFileName + "文件在FTP服务器目录" + dir + "不存在,请检查FTP服务器"); return; } ftpClient.downloadFile(localTempProcessFileName, remoteFileName); logger.info("已将FTP服务器上的文件" + remoteFileName + "下载到本地文件" + localTempProcessFileName); ftpClient.downloadFile(localBackupFileName, remoteFileName); ftpClient.deleteFile(remoteFileName); logger.info("已将FTP服务器上的文件" + remoteFileName + "删除"); changeToRootDirectoryByDir(dir); } catch (Exception e) { throw new Exception("将FTP服务器指定目录下的指定文件下载到本地指定的文件时发生异常,原因:" + e); } } /** * 将FTP服务器上的文件下载到本地 * * @param remoteFileName * @param localFileName * @throws Exception */ public synchronized boolean downloadRemotFileToLocal(String remoteFileName, String localFileName) throws Exception { logger.info(String.format("将FTP服务器上的文件下载到本地,远程文件名[%s],本地文件名[%s]。", remoteFileName, localFileName)); String msg = ""; try { // connectToServer(); boolean isExists = ftpClient.exists(remoteFileName); if (!isExists) { msg = remoteFileName + "文件在FTP服务器上不存在,请检查FTP服务器"; logger.info(msg); throw new RuntimeException(msg); } ftpClient.downloadFile(localFileName, remoteFileName); logger.info("已将FTP服务器上的文件" + remoteFileName + "下载到本地文件" + localFileName); } catch (RuntimeException e) { msg = "将FTP服务器上文件下载到本地时发生异常,原因:" + e.getMessage(); logger.info(msg); throw new RuntimeException(msg); } return true; } /** * 将指定的本地文件上传到FTP服务器上 * * @param localFileName * @param remoteFileName */ public synchronized void uploadFileToFtpServer(String localFileName, String remoteFileName) throws Exception { logger.info(String.format("将指定的本地文件上传到FTP服务器上,本地文件名[%s],远程文件名[%s]。", localFileName, remoteFileName)); try { ftpClient.uploadFile(localFileName, remoteFileName); logger.info("成功将文件" + localFileName + "上传到FTP服务器"); } catch (Exception e) { throw new Exception("将指定的本地文件上传到FTP服务器上时发生异常,原因:" + e); } } /** * 将内容串上传到FTP服务器 * * @param remoteFileName * @param allContents * @throws Exception */ public synchronized void uploadContentsToFtpServer(String remoteFileName, String allContents) throws Exception { logger.info(String.format("将内容串上传到FTP服务器上指定的文件中,远程文件名[%s],内容串[%s]。", remoteFileName, allContents)); FileTransferOutputStream outputStream = null; try { outputStream = ftpClient.uploadStream(remoteFileName); outputStream.write(allContents.getBytes()); outputStream.flush(); logger.info("成功将文件" + remoteFileName + "上传到FTP服务器"); } catch (Exception e) { throw new Exception("将内容上传到FTP服务器上时发生异常,原因:" + e); } finally { if (outputStream != null) { outputStream.close(); outputStream = null; } } } /** * 根据传入的目录路径返回到FTP服务器根目录,供下次使用 * * @param dir * @throws FTPException * @throws IOException */ private synchronized void changeToRootDirectoryByDir(String dir) throws FTPException, IOException { logger .info(String.format("根据传入的目录路径返回到FTP服务器根目录,供下次使用,当前路径[%s]。", dir)); int parentLevels = dir.split("/").length; for (int i = 0; i < parentLevels; i++) ftpClient.changeToParentDirectory(); } /** * 连接FTP服务器,并登录服务器 * * @throws FTPException * @throws IOException */ public synchronized void connectToServer() throws Exception { ftpClient = new FileTransferClient(); try { ftpClient.setRemoteHost(ftpServiceIP); ftpClient.setRemotePort(ftpServicePort); ftpClient.setUserName(loginUser); ftpClient.setPassword(loginPwd); ftpClient.connect(); logger.info(String.format("连接并登录到FTP服务器,IP[%s],Port[%d],User[%s]。", ftpServiceIP, ftpServicePort, loginUser)); } catch (Exception e) { ftpService = null; throw new Exception("连接和登录FTP服务器时发生异常,请检查FTP服务器参数配置,原因:" + e); } } /** * 断开与FTP服务器的连接 * * @throws FTPException * @throws IOException */ public synchronized void disconnectToServer() throws Exception { try { if (ftpClient != null) { if (ftpClient.isConnected()) { ftpClient.disconnect(); ftpClient = null; logger.info("断开与FTP服务器连接"); } } } catch (Exception e) { throw new Exception("断开与FTP服务器连接时发生异常,原因:" + e); } } /** * 无参私有构造器 */ private FtpService() { super(); } /** * 全参私有构造器 * * @param ftpServiceIP * @param ftpServicePort * @param loginUser * @param loginPwd */ private FtpService(String ftpServiceIP, int ftpServicePort, String loginUser, String loginPwd) { super(); this.ftpServiceIP = ftpServiceIP; this.ftpServicePort = ftpServicePort; this.loginUser = loginUser; this.loginPwd = loginPwd; } }
发表评论
-
java 中JSON数据特殊字符的处理
2014-05-05 18:41 6873JSon 数据中的String 传递数据时,需要处理好特殊 ... -
Java Zip Utils 压缩/解压缩工具包
2014-05-05 11:27 6933实际开发中可能会用到压缩或解压缩,底层借助于apache的z ... -
JRE 与 JDK ,JVM client模式与server模式
2012-10-11 20:08 10249本文大部分内容转载自:JRE 与 JDK ,JVM Clien ... -
Java class文件的JDK编译版本号
2012-10-11 09:30 1815本人转载自新浪博客,原文地址:http://blog.sina ... -
TOMCAT支持J2EE版本问题
2012-09-03 18:33 6990j2ee1.4 支持的是servlet2.4,j ... -
SimpleDateFormat 线程安全的问题
2012-05-25 18:10 1270SimpleDateFormat中的日期 ... -
Java编程中“为了性能”尽量要做到的一些地方
2012-05-17 08:48 836这篇文章COPY自:http://www.iteye.c ... -
分享一个文件监听器类:FileMonitor
2011-06-17 15:51 3865一直使用tomcat开发,只要将<Context ... -
在eclipse中给jar关联相应的源文件(Source)
2011-05-09 17:17 6737以给commons.dbcp.jar关联源文件为例进行说明。 ... -
tomcat的启动内存设置
2011-05-07 17:16 1111转载自:http://blog.csdn.net/chen ... -
java调用sql server存储过程
2011-05-06 17:21 3444为了便于测试,先创建一个简单的表: CREATE TA ... -
session过期问题
2010-12-16 11:43 1219摘自:http://dev.firnow.com/course ... -
awttest
2010-12-06 22:20 0protected void doPost(Htt ... -
java环境变量设置
2010-07-01 22:01 3206windows xp下配置JDK环境变量: 1.安装JD ... -
一个删除指定目录下所有文件及子目录的Java程序
2010-05-04 14:30 6870今天在je上闲逛时,看到很多人分享的一个删除指定目录下所有文件 ... -
Publishing failed with multiple errors和Timeout waiting for Tomcat v5.5的解决方法
2010-04-27 14:59 2183转载自:http://sucre.iteye.com/bl ... -
eclipse注释规范设置
2010-04-13 14:57 1787本文来自CSDN博客,转载请标明出处:http://blog. ... -
String , StringBuffer, StringBuilder的区别
2010-03-03 17:38 3498转载自:http://www.cnblogs.com/liuy ... -
解决一台机器同时运行多个Tomcat服务
2010-03-03 15:36 1253转载自: http://www.cnblog ... -
如何生成javadoc文档
2010-03-03 15:31 5500编写Javadoc 代码 在Java 代码的类、 ...
相关推荐
java操作FTP工具类:实现基本断点上传下载、实现上传下载进度汇报、实现中文目录创建及中文文件创建,添加对于中文的支持
ftp工具类,构造方法初始化类,连接ftp,列出所有文件内容,下载文件
这是使用c#实现FTP功能的工具类,包含socket技术。
FTP 工具类,赚钱积分下载东西,谢谢各位!谢谢各位!
Java ftp工具类,可以实现ftp上传,读取,目录切换,内容创建,目录创建、检查文件是否存在,支持主动方式和被动方式读取
ftp工具类,帮助你很容易的实现ftp功能
采用java实现FTP文件的上传下载,包含文件以及文件夹上传下载,新建文件夹等基本相关操作,不会出现文件名的中文乱码,内含demo相关测试以及jar包,可直接导入使用,采用MyEclipse8.5,jdk1.6亲测无问题
ftp操作工具类,用户ftp文件的添加,删除,等操作!
FTP工具类,包括:文件夹上传下载,文件删除(包括非空文件夹),重命名等操作 基本的操作应该都有
FTP 工具类. 可以完成对目录创建的创建、修改、删除,对文件的上传下载等操作.
ftp 上传时,用到的工具类,项目上配置好ftp服务器后,controller可以方便的调用此工具类进行上传
java的ftp工具类,需要的自行下载查看,有切换目录,创建目录方法。
org.apache.commons.net.ftp.FTPClient FTP工具类,实现上传、下载、压缩到输出流下载等功能
Apache FTPClient操作FTP工具类
ftp 上传 下载 工具类 ftp 上传 下载 工具类 ftp 上传 下载 工具类 ftp 上传 下载 工具类
jdk1.7以上专用FTP工具类,本人花了半天时间调试通过,拿来即用,具体用法详见main函数。
本类提供了java对ftp的各种操作的!经过精心的整理!相信对初学者有很大的帮助!
java上传ftp服务器工具类,提供完成的方法,直接调用即可
使用apache的common-net.jar,实现ftp文件的上传和下载,封装成工具类,方便开发。
java ftp文件传输工具类源码 依赖包 <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.7 ...