- 浏览: 136385 次
- 性别:
- 来自: 杭州
-
文章分类
最新评论
-
522823979:
JXL POI 导出excel 包括图片 -
贝塔ZQ:
实现导出excel文件和图片,感觉用poi和jxl代码量好多, ...
JXL POI 导出excel 包括图片 -
bmpbhg:
和你的类似,不过我这边报的是
org.dom4j.Docume ...
Servlet 接受解析HTTP请求XML数据,返回XML -
Wuaner:
引用Non Field Validators排在前面的先执行 ...
struts2 内建效验器 -
timelion:
请将正式简历发送至
timelion@163.com
找工作中...放上简历,求推荐
功能就是将指定文件夹下面的所有文件都上传到FTP上
----------------------------20100426-----------------------------
今天换了FTP 不知道什么原因上面的代码不能用了 ,差了很久 还是有问题,郁闷....
对FTP协议也还是不是很了解,后来换了Apache的FTP组件,就可以了....
/** *上传自定路径的下的文件到FTP */ public class FtpTools { private String ip = ""; private String username = ""; private String password = ""; private int port = 21; private String localFileFullName = "";// 需要上传的目录 FtpClient ftpClient = null; OutputStream os = null; FileInputStream is = null; public FtpTools(String serverIP, int port, String username, String password) { this.ip = serverIP; this.username = username; this.password = password; this.port = port; } public FtpTools(String serverIP, String username, String password) { this.ip = serverIP; this.username = username; this.password = password; } /** * 创建文件夹 * * @param dir * @param ftpClient * @throws Exception */ private void createDir(String dir, FtpClient ftpClient) throws Exception { ftpClient.ascii(); StringTokenizer s = new StringTokenizer(dir, "/"); // sign s.countTokens(); String pathName = ""; while (s.hasMoreElements()) { pathName = pathName + "/" + (String) s.nextElement(); try { ftpClient.sendServer("MKD " + pathName + "\r\n"); } catch (Exception e) { e = null; } ftpClient.readServerResponse(); } ftpClient.binary(); } /** * 检查文件夹是否存在 * * @param dir * @param ftpClient * @return */ private boolean isDirExist(String dir, FtpClient ftpClient) { try { ftpClient.cd(dir); } catch (Exception e) { return false; } return true; } /** * ftp上传 * * @param localFileFullName * 上传的源文件夹 * @return */ public boolean upload(String localFileFullName) { this.localFileFullName = localFileFullName; try { String savefilename = new String(localFileFullName .getBytes("ISO-8859-1"), "GBK"); // 新建一个FTP客户端连接 ftpClient = new FtpClient(); ftpClient.openServer(this.ip, this.port); ftpClient.login(this.username, this.password); // 打开本地待长传的文件 File file_in = new File(savefilename); processFile(file_in, ftpClient); if (is != null) { is.close(); } if (os != null) { os.close(); } if (ftpClient != null) { ftpClient.closeServer(); } return true; } catch (Exception e) { e.printStackTrace(); System.err.println("Exception e in Ftp upload(): " + e.toString()); return false; } } /** * 上传文件 * @Deprecated * @param source * @param ftpClient * @throws Exception * */ private void processFile(File source, FtpClient ftpClient) throws Exception { if (source.exists()) { if (source.isDirectory()) { if (!isDirExist(source.getPath().substring(localFileFullName.length()).replace('\\', '/'), ftpClient)) { createDir(source.getPath().substring( localFileFullName.length()).replace('\\', '/'), ftpClient); } File sourceFile[] = source.listFiles(); for (int i = 0; i < sourceFile.length; i++) { if (sourceFile[i].exists()) { if (sourceFile[i].isDirectory()) { this.processFile(sourceFile[i], ftpClient); } else { ftpClient.cd(cheangPath(sourceFile[i].getPath())); ftpClient.binary(); os = ftpClient.put(sourceFile[i].getName()); byte[] bytes = new byte[1024]; is = new FileInputStream(sourceFile[i]); // 开始复制 int c; // 暂未考虑中途终止的情况 while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); } is.close(); os.close(); } } } } else { ftpClient.cd(cheangPath(source.getPath())); ftpClient.binary(); os = ftpClient.put(source.getName()); byte[] bytes = new byte[1024]; is = new FileInputStream(source); // 开始复制 int c; // 暂未考虑中途终止的情况 while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); } is.close(); os.close(); } } else { throw new Exception("此文件或文件夹[" + source.getName() + "]有误或不存在!"); } } /** * 获取当前的FTP路径 * * @param path * @return */ private String cheangPath(String path) { path = path.substring(localFileFullName.length()).replace('\\', '/'); if ("".equals(path)) { path = "/"; } else { path = path.substring(0, path.lastIndexOf("/") + 1); } return path; } }
----------------------------20100426-----------------------------
今天换了FTP 不知道什么原因上面的代码不能用了 ,差了很久 还是有问题,郁闷....
对FTP协议也还是不是很了解,后来换了Apache的FTP组件,就可以了....
package com.wasu.itv.common.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.SocketException; import java.util.StringTokenizer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; public class FtpTools { private static Log log = LogFactory.getLog(FtpTools.class); private FTPClient ftpClient = null; private String ip = ""; private String username = "itvFtp"; private String password = "itv"; private int port = 21; private String localFileFullName = "";// 需要上传的目录 public FtpTools(String serverIP, int port, String username, String password) { this.ip = serverIP; this.username = username; this.password = password; this.port = port; this.loginFtp(); } public boolean loginFtp() { boolean flag = true; if (ftpClient == null) { int reply; try { ftpClient = new FTPClient(); ftpClient.setControlEncoding("GBK"); ftpClient.connect(ip); ftpClient.login(username, password); ftpClient.setDefaultPort(port); reply = ftpClient.getReplyCode(); ftpClient.setDataTimeout(120000); } catch (SocketException e) { flag = false; e.printStackTrace(); System.err.println("登录ftp服务器失败,连接超时!"); log.debug("登录ftp服务器失败"); } catch (IOException e) { flag = false; e.printStackTrace(); System.err.println("登录ftp服务器失败,FTP服务器无法打开!"); log.debug("登录ftp服务器失败"); } } return flag; } public boolean logoutFtp() { try { if (ftpClient != null) { ftpClient.logout(); ftpClient.disconnect(); } } catch (Exception e) { e.printStackTrace(); } return true; } public String getUploadPath(String path){ File source = new File(path); return source.getPath().substring( localFileFullName.length()).replace('\\', '/'); } /** * 输出到文件 * * @param fileName * @param data * @throws Exception */ public boolean upload(String fileName) throws Exception { boolean flag = true; File source = new File(fileName); if (source.exists()) { ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); if (source.isDirectory()) { if (!isDirExist(source.getPath().substring(localFileFullName.length()).replace('\\', '/'))) { createDir(source.getPath().substring(localFileFullName.length()).replace('\\', '/')); } File sourceFile[] = source.listFiles(); for (int i = 0; i < sourceFile.length; i++) { if (sourceFile[i].exists()) { if (sourceFile[i].isDirectory()) { this.upload(sourceFile[i].getCanonicalPath()); } else { //改变目录 this.changerToPath(sourceFile[i].getCanonicalPath()); flag = ftpClient.storeFile(sourceFile[i].getName(), new FileInputStream(sourceFile[i])); log.debug("文件:"+sourceFile[i].getName()+"上传"+(flag==true?"成功":"失败")); //返回根目录 ftpClient.changeWorkingDirectory("~"); } } } }else { try { FileInputStream bis = new FileInputStream(source); this.changerToPath(source.getCanonicalPath()); flag = ftpClient.storeFile(source.getName(), bis); log.debug("文件:"+source.getName()+"上传"+(flag==true?"成功":"失败")); ftpClient.changeWorkingDirectory("~"); bis.close(); } catch (Exception e) { e.printStackTrace(); log.debug("本地文件上传失败!"+source.getCanonicalPath(), e); } } } return flag; } /** * 获取当前的FTP路径 * * @param path * @return */ private boolean changerToPath(String path) { boolean isOK = false; path = path.substring(localFileFullName.length()).replace('\\', '/'); StringTokenizer s = new StringTokenizer(path, "/"); s.countTokens(); String pathName = ""; while (s.hasMoreElements()) { pathName = (String) s.nextElement(); try { isOK=ftpClient.changeWorkingDirectory(pathName); } catch (Exception e) { e = null; isOK=false; } } return isOK; } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { FtpTools packer = new FtpTools("125.200.222.164",21,"itvFtp","itv"); packer.localFileFullName="E:\\biyatech\\washu\\SMSP\\itv\\src\\com\\wasu"; packer.upload("E:\\biyatech\\washu\\SMSP\\itv\\src\\com\\wasu"); packer.logoutFtp(); } /** * 创建文件夹 * * @param dir * @param ftpClient * @throws Exception */ private void createDir(String dir) throws Exception { StringTokenizer s = new StringTokenizer(dir, "/"); s.countTokens(); String pathName = ""; while (s.hasMoreElements()) { pathName = (String) s.nextElement(); try { ftpClient.makeDirectory(pathName); ftpClient.changeWorkingDirectory(pathName); } catch (Exception e) { e = null; } } ftpClient.changeWorkingDirectory("~"); } /** * 检查文件夹是否存在 * * @param dir * @param ftpClient * @return */ private boolean isDirExist(String dir) { boolean isDirExist = false; try { isDirExist = ftpClient.changeWorkingDirectory(dir); } catch (IOException e) { e.printStackTrace(); } return isDirExist; } public void setLocalFileFullName(String localFileFullName) { this.localFileFullName = localFileFullName; } }
发表评论
-
Maven 常用命令
2011-03-03 16:37 849mvn dependency:resolve 了解你项目的 ... -
JXL POI 导出excel 包括图片
2010-05-27 15:22 9256主要的问题还是JXL只支持PNG格式的图片,没辙,只有转换格式 ... -
decorator(装饰模式)io包理解
2010-03-30 16:01 1519decorator的结构如下: MyInterfac ... -
java设计模式
2010-03-25 11:28 877策略设计模式: 创建一个能够根据所传递的参数对象的不同而具有不 ... -
web开发的问题
2010-03-22 00:56 9941. filter的优先级 在应用中会用到很多的过滤器,想下面 ... -
Servlet 接受解析HTTP请求XML数据,返回XML
2010-03-13 11:33 25123项目需要Servlet接受http提交过来的XML解析后,返回 ... -
csv文件下载
2010-02-05 15:21 2504BufferedOutputStream bos = null ... -
struts不定数文件上传
2010-02-04 22:18 1003Hashtable files=voteForm.getMul ... -
JFreeChart封装工具类
2010-02-03 18:37 3955package com.wasu.itv.common.uti ... -
J2SE
2010-01-22 15:08 877public static void main(String[ ... -
struts2 内建效验器
2010-01-22 10:54 1453http://struts.apache.org/2.0.9/ ... -
struts2内建拦截器
2010-01-21 12:50 11221. alias:实现在不同请求中相似参数别名的转换; 2. ... -
struts2异常收集
2010-01-19 13:21 9421. ognl.OgnlException: target i ... -
apache VSF 操作类
2010-01-13 17:54 1165package com.biya.dao.jdbc; ... -
记录开发过程用的代码段001--POI生成excel表格,如何合并单元格
2009-12-30 16:51 3297POI生成excel表格,如何 ... -
List 存取date排序
2009-12-10 14:46 7670主要利用 Comparator这个接口来实现,我这里是排序li ... -
JSP,SERVLET
2009-12-03 16:03 8671. 直接中文作为参数提交R=URL(test.do?name ... -
数字,金额 转 中文大写
2009-11-27 16:05 1366import java.text.DecimalFormat; ... -
Lucene:基于Java的全文检索引擎简介
2009-11-24 15:53 1040http://www.chedong.com/tech/luc ... -
BIRT 安装
2009-11-17 10:31 2152最近又用到birt报表 ,年初的时候用过一次,到现在忘的差不多 ...
相关推荐
ftp工具类,构造方法初始化类,连接ftp,列出所有文件内容,下载文件
FTP 工具类,赚钱积分下载东西,谢谢各位!谢谢各位!
ftp工具类,帮助你很容易的实现ftp功能
java操作FTP工具类:实现基本断点上传下载、实现上传下载进度汇报、实现中文目录创建及中文文件创建,添加对于中文的支持
FTP工具类,包括:文件夹上传下载,文件删除(包括非空文件夹),重命名等操作 基本的操作应该都有
在本文中,我们将深入探讨如何利用这个库开发一个FTP工具类,以便在Java应用程序中进行文件上传、下载和其他FTP操作。 首先,我们需要了解FTP的基本概念。FTP是一种用于在网络上进行文件传输的标准协议。它允许用户...
ftp操作工具类,用户ftp文件的添加,删除,等操作!
Java ftp工具类,可以实现ftp上传,读取,目录切换,内容创建,目录创建、检查文件是否存在,支持主动方式和被动方式读取
jdk1.7以上专用FTP工具类,本人花了半天时间调试通过,拿来即用,具体用法详见main函数。
对于初学者来说,理解并使用这样的FTP工具类可以帮助他们快速掌握FTP操作,避免了重复编写相同功能的代码。同时,这个工具类也展示了如何在Java中利用第三方库(如Apache Commons Net)来扩展Java标准库的功能。 ...
接下来,我们将深入探讨FTP工具类的主要功能、使用方法以及源码分析。 **1. FTPClient类** FTPClient是Apache Commons Net的核心类,它实现了FTP协议的大部分功能。通过这个类,我们可以连接到FTP服务器,执行登录...
本篇文章将详细讲解如何使用Java编写一个FTP工具类,实现连接FTP服务器、上传文件、删除文件、下载文件以及检索文件的功能。 首先,我们需要引入Apache Commons Net库,该库提供了丰富的FTP客户端API。在`pom.xml`...
Apache FTPClient操作FTP工具类
本话题将详细介绍如何使用Java实现FTP工具类以及所需的jar包。 Apache Commons Net是一个强大的Java网络实用程序库,它提供了多种网络协议的实现,包括FTP。在这个场景中,`commons-net-3.3.jar`是这个库的一个版本...
org.apache.commons.net.ftp.FTPClient FTP工具类,实现上传、下载、压缩到输出流下载等功能
Java FTP工具类是Java开发中用于处理FTP(File Transfer Protocol)协议的一种实用程序,它使得在Java应用程序中上传、下载、删除或者管理远程服务器上的文件变得简单。在本压缩包中,我们有一个名为"util"的文件,...
ftp 上传时,用到的工具类,项目上配置好ftp服务器后,controller可以方便的调用此工具类进行上传
ftp工具类,包含文件上传,文件删除,文件列表,查询当天文件类表方法
java的ftp工具类,需要的自行下载查看,有切换目录,创建目录方法。
本文将详细介绍标题和描述中提到的几个关键知识点:Java中的zip、rar(包括处理带密码的RAR文件)、gz压缩,以及FTP工具类的使用。 1. **Java ZIP压缩与解压缩**: Java内置的`java.util.zip`包提供了处理ZIP文件...