- 浏览: 96392 次
- 性别:
- 来自: 南京
文章分类
最新评论
自己写的利用apache的net包写的ftp的上传、下载功能,可以上单个文件、文件夹,下载单个文件及文件夹整个目录,并且解决了文件、文件夹汉字问题,经过自己的正式测试。
不讲废话了,直接上代码,愿给需要的朋友提供个帮助。
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; import org.apache.commons.lang.StringUtils; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.d6.utils.AppUtils; /** * @author jjl * */ public class FtpUtil { static String ftpUrl = null; // ftp 服务器路径 static int ftpPort = 21; // ftp 服务器端口 static String ftpUserName = null; // ftp 服务器用户名 static String ftpPassword = null; // ftp 服务器密码 static String encoding = null; static String serverLanguageCode = FTP.DEFAULT_CONTROL_ENCODING; static int timeout = 60000; // 配置文件路径 static String configFile = "ftp.properties"; static FTPClient ftpClient = null; /** * 创建FTPClient * * @throws Exception */ private static void createFtpClient() throws Exception { if (ftpClient == null) { ftpClient = new FTPClient(); setConnectConfig(); connectFtp(ftpClient); } } /** * 关闭FTPClient * * @throws Exception */ private static void closeFtpClient() throws Exception { if (ftpClient != null) { ftpClient.logout(); if (ftpClient.isConnected()) { ftpClient.disconnect(); } ftpClient = null; } } /** * ftp工具连接 * * @param ftpClient * @return * @throws Exception */ public static boolean connectFtp(FTPClient ftpClient) throws Exception { // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 ftpClient.connect(ftpUrl, ftpPort); getFtpConfig(); // 返回登录情况 boolean isConnect = ftpClient.login(ftpUserName, ftpPassword); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // ftpClient.enterLocalPassiveMode(); ftpClient.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE); ftpClient.setControlEncoding(encoding); // 设置连接时间 ftpClient.setDataTimeout(timeout); // 返回代码 int reply = ftpClient.getReplyCode(); if (isConnect && FTPReply.isPositiveCompletion(reply)) { return true; } return false; } /** * 上传文件 * * @param localPath * 本地文件路径 * @param filename * 本地文件名 * @param remotePath * 服务器文件路径 * @return 成功返回true,否则返回false * @throws Exception */ public static boolean uploadFile(File file, String remotePath) throws Exception { boolean success = false; try { // 创建FTPClient createFtpClient(); // 判断是文件,还是文件目录 if (file.isDirectory()) { success = uploadAll(file, remotePath + File.separator + file.getName()); } else { success = upload(file, remotePath); } // 关闭连接 closeFtpClient(); } catch (IOException e) { e.printStackTrace(); } // 返回 return success; } /** * 上传文件 * * @param file * @param remotePath * @return * @throws Exception */ private static boolean upload(File file, String remotePath) throws Exception { boolean success; // 文件 FileInputStream inputStream = new FileInputStream(file); // 切换ftp工作目录 changeWorkingDirectory(remotePath); // 上传文件 success = ftpClient.storeFile(encodingGBKToISO8859(file.getName()), inputStream); inputStream.close(); return success; } /** * 上传文件 * * @param localPath * 本地文件路径 * @param filename * 本地文件名 * @param remotePath * 服务器文件路径 * @return * @throws Exception */ public static boolean uploadFile(String localPath, String filename, String remotePath) throws Exception { File file = new File(localPath + File.separator + filename); return uploadFile(file, remotePath); } /** * 上传文件目录 * * @param file * @param remotePath * @return * @throws Exception */ public static boolean uploadDirectory(String localPath, String remotePath) throws Exception { return uploadDirectory(new File(localPath), remotePath); } /** * 上传文件目录 * * @param file * @param remotePath * @return * @throws Exception */ public static boolean uploadDirectory(File file, String remotePath) throws Exception { boolean success = false; // 创建FTPClient createFtpClient(); success = uploadAll(file, remotePath); // 关闭连接 closeFtpClient(); return success; } /** * 上传 * * @param file * @param remotePath * @throws Exception * @throws FileNotFoundException * @throws IOException */ private static boolean uploadAll(File file, String remotePath) throws Exception { boolean success = false; // 得到文件列表 File[] fileList = file.listFiles(); for (File uploadFile : fileList) { // 循环子目录 if (uploadFile.isDirectory()) { ftpClient.changeWorkingDirectory(remotePath); ftpClient.makeDirectory(uploadFile.getName()); success = uploadAll(uploadFile, remotePath + File.separator + uploadFile.getName()); } else { // 上传文件 success = upload(uploadFile, remotePath); } } return success; } /** * 下载文件目录下所有文件 * * @param localPath * 本地文件路径 * @param fileName * 本地文件名 * @param remotePath * 服务器路径 * @return 成功返回true,否则返回false * @throws Exception */ public static boolean downloadDirectory(String localPath, String remotePath) throws Exception { boolean success = false; try { // 创建FTPClient createFtpClient(); success = downloadAll(localPath, remotePath); // 关闭连接FTPClient closeFtpClient(); } catch (IOException e) { e.printStackTrace(); } return success; } /** * 下载文件目录下的所有文件 * * @param localPath * @param remotePath * @param success * @return * @throws IOException * @throws Exception * @throws FileNotFoundException */ private static boolean downloadAll(String localPath, String remotePath) throws Exception { boolean success = false; // 切换ftp工作目录 changeWorkingDirectory(remotePath); // 取出下载文件 FTPFile[] ftpFiles = ftpClient.listFiles(); for (FTPFile ftpFile : ftpFiles) { // 过滤文件名不正确的文件 if (ftpFile.getName().equals(".") || ftpFile.getName().equals("..")) { continue; } if (ftpFile.isDirectory()) { // 创建目录 File fileDir = new File(localPath + File.separator + ftpFile.getName()); if (!fileDir.exists()) { fileDir.mkdirs(); } // 下载文件 success = downloadAll(localPath + File.separator + ftpFile.getName(), remotePath + File.separator + ftpFile.getName()); } else { // 切换ftp工作目录 changeWorkingDirectory(remotePath); // 下载文件 success = download(localPath, ftpFile); } } return success; } /** * 切换ftp工作目录 * * @param remotePath * @throws IOException */ private static void changeWorkingDirectory(String remotePath) throws IOException { // 转移到FTP服务器目录 ftpClient.changeWorkingDirectory("/"); if (StringUtils.isNotBlank(remotePath)) { ftpClient.changeWorkingDirectory(remotePath); } } /** * @param localPath * @param ftpFile * @return * @throws Exception */ private static boolean download(String localPath, FTPFile ftpFile) throws Exception { boolean success; // 文件目录不存在创建文件 File fileDir = new File(localPath); if (!fileDir.exists()) { fileDir.mkdirs(); } File localFile = new File(localPath + File.separator + ftpFile.getName()); OutputStream is = new FileOutputStream(localFile); // 下载文件 success = ftpClient.retrieveFile( encodingGBKToISO8859(ftpFile.getName()), is); is.close(); return success; } /** * 下载文件 * * @param localPath * 本地文件路径 * @param fileName * 本地文件名 * @param remotePath * 服务器路径 * @return 成功返回true,否则返回false * @throws Exception */ public static boolean downloadFile(String localPath, String fileName, String remotePath) throws Exception { boolean success = false; try { // 创建FTPClient createFtpClient(); // 转移到FTP服务器目录 if (StringUtils.isNotBlank(remotePath)) { ftpClient.changeWorkingDirectory(remotePath); } // 取出下载文件 FTPFile[] ftpFiles = ftpClient.listFiles(); for (FTPFile ftpFile : ftpFiles) { String ftpFileName = ftpFile.getName(); if (ftpFileName.equals(fileName)) { success = download(localPath, ftpFile); break; } } // 关闭连接FTPClient closeFtpClient(); } catch (IOException e) { e.printStackTrace(); } return success; } /** * 删除一个文件 * * @throws Exception */ public static boolean deleteFile(String filename) throws Exception { boolean flag = true; try { // 创建FTPClient createFtpClient(); // 删除文件 flag = ftpClient.deleteFile(filename); } catch (IOException ioe) { ioe.printStackTrace(); } finally { // 关闭连接FTPClient closeFtpClient(); } return flag; } /** * 删除目录 * * @throws Exception */ public static void deleteDirectory(String pathname) throws Exception { try { // 创建FTPClient createFtpClient(); File file = new File(pathname); if (!file.isDirectory()) { deleteFile(pathname); } ftpClient.removeDirectory(pathname); } catch (IOException ioe) { ioe.printStackTrace(); } finally { // 关闭连接FTPClient closeFtpClient(); } } /** * 设置参数 * * @param configFile * --参数的配置文件 */ private static void setConnectConfig() { Properties property = new Properties(); BufferedInputStream bis = null; try { // 读取配置文件 File file = new File(configFile); bis = new BufferedInputStream(new FileInputStream(file)); property.load(bis); // 设置文件信息 ftpUserName = property.getProperty("ftp.userName"); ftpPassword = property.getProperty("ftp.password"); ftpUrl = property.getProperty("ftp.url"); ftpPort = Integer.parseInt(property.getProperty("ftp.port")); encoding = property.getProperty("ftp.encoding"); serverLanguageCode = property.getProperty("ftp.serverLanguageCode"); // 关闭文件 if (bis != null){ bis.close(); } } catch (FileNotFoundException e1) { System.out.println("配置文件 " + configFile + " 不存在!"); } catch (IOException e) { System.out.println("配置文件 " + configFile + " 无法读取!"); } } /** * 重命名文件 * * @param oldName * 原文件名 * @param newName * 新文件名 * @throws Exception */ public static void renameFile(String oldName, String newName) throws Exception { try { // 创建FTPClient createFtpClient(); // 重命名 ftpClient.rename(oldName, newName); } catch (IOException ioe) { ioe.printStackTrace(); } finally { // 关闭连接FTPClient closeFtpClient(); } } /** * 设置FTP客服端的配置 * * @return ftpConfig */ private static FTPClientConfig getFtpConfig() { // FTPClientConfig.SYST_UNIX FTPClientConfig ftpConfig = new FTPClientConfig(FTPClientConfig.SYST_NT); ftpConfig.setServerLanguageCode(serverLanguageCode); return ftpConfig; } /** * 将ISO-8859-1编码转为 GBK * * @param obj * @return "" * @throws Exception */ private static String encodingToGBK(Object obj) throws Exception { if (obj != null) { return new String(obj.toString().getBytes("iso-8859-1"), encoding); } return null; } /** * 将 GBK编码转为ISO-8859-1 * * @param obj * @return "" * @throws Exception */ private static String encodingGBKToISO8859(Object obj) throws Exception { if (obj != null) { return new String(obj.toString().getBytes(encoding), "iso-8859-1"); } return null; } /** * @param args */ public static void main(String[] args) { boolean success = false; try { java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // success = FtpUtil.uploadFile("D:\\ftp", "中文的.docx", "temp"); // System.out.println(success); // // success = FtpUtil.uploadFile(new File("D:\\ftp\\abc.txt"), // "temp"); // System.out.println(success); // success = FtpUtil.uploadDirectory("D:\\ftp", "temp"); // System.out.println(success); System.out.println(dateFormat.format(new java.util.Date())); success = FtpUtil.downloadFile("d:\\", "test.zip", "temp"); System.out.println(dateFormat.format(new java.util.Date())); System.out.println(success); // success = FtpUtil.downloadDirectory("d:\\ftp\\a2", "temp"); // System.out.println(success); } catch (Exception e) { System.out.println(e.getMessage()); } } }
发表评论
-
规范的模板化项目架构管理
2012-05-03 23:31 732总在寻找项目开发简单化、标准化、统一化的开发管理方法,在项目 ... -
SmartGWT学习注意事项(一)
2012-03-31 20:42 1442首先表明,我 ... -
J2ME程序开发全方位基础讲解汇总
2007-08-03 14:19 551一、J2ME中需要的Java基础知识现在有大部分人,都是从零开 ... -
使用J2ME技术开发RPG游戏
2007-08-03 14:24 514RPG(角色扮演游戏)是手机游戏中的一类主要类型,也是相对来说 ... -
移动视频: QuickTime for Java API 入门
2007-08-05 12:45 563在 Java 平台上创建 iPod 视频内容 ... -
技术交流:QuickTime流媒体和Java(图)
2007-08-05 12:46 588这并不是即将问世的Quic ... -
JMF下载安装与支持格式
2007-08-05 12:47 823JMF开发进度不是很快,所以目前还是比较薄弱。 JMF,全名 ... -
搭建J2ME开发环境
2007-08-05 12:48 531由于WTK并没有提供代码编辑的功能,因此本文讲述如何使用Ecl ... -
JMF系统介绍
2007-08-09 15:18 618一.简介 1.1JMF 体系结构 ... -
Experiments in Streaming Content in Java ME(一)
2007-08-13 13:43 552Since my book on Mobile Media A ... -
Experiments in Streaming Content in Java ME(二)----Creating an RTSP Protocol Handler
2007-08-13 13:44 1408Recall that RTSP is the actual ... -
Experiments in Streaming Content in Java ME(三)-----Back to RTPSourceStream and StreamingDataSource
2007-08-13 13:46 994With the protocol handler in pl ... -
java中文件操作大全
2007-08-22 15:40 531一.获得控制台用户输入的信息 /***//**获得控 ... -
Pocket PC、Pocket PC Phone、Smartphone的区别
2007-08-23 16:59 606首先说明几个概念: 1、什么是Pocket PC?Pocket ... -
Struts配置文件详解(来自csdn)
2007-11-30 08:35 467Struts应用采用两个基于X ... -
jBPM开发入门指南(5)
2007-11-30 08:45 456jBPM开发入门指南(5) 前篇说起要讲在JBPM中实现用 ... -
jBPM开发入门指南(4)
2007-11-30 08:48 531jBPM开发入门指南(4) 7 jBPM 的客户端 ... -
jBPM开发入门指南(3)
2007-11-30 08:51 5525 安装 jBPM 的 Eclipse 开发插件 有 ... -
jBPM开发入门指南(2)
2007-11-30 08:52 5454 数据库初始化 jBPM 需要数据库支持, jBPM ... -
jBPM开发入门指南(1)
2007-11-30 08:54 546工作流虽然还在不成熟 ...
相关推荐
使用Apache commons net组件实现ftp上传与下载功能
使用apache的common-net.jar,实现ftp文件的上传和下载,封装成工具类,方便开发。
基于java的ftp文件上传下载工具,支持文件夹上传下载,带进度
ftp 上传 下载 工具类 ftp 上传 下载 工具类 ftp 上传 下载 工具类 ftp 上传 下载 工具类
java写的FTP工具,登陆用的是access 数据库,支持多用户登陆。数据库在database文件夹里面,初始登陆名lhz,密码123。
java上传ftp服务器工具类,提供完成的方法,直接调用即可
java操作FTP工具类:实现基本断点上传下载、实现上传下载进度汇报、实现中文目录创建及中文文件创建,添加对于中文的支持
此文档包含几乎所有日常用到的java 对 ftp的操作方法,都可以直接进行调用使用。
这是我使用java实现的linux和ftp服务器文件上传下载工具,需要电脑安装jdk8, 启动命令,java -jar linuxAndFtp.jar 启动成功后,浏览器访问:http://localhost:9999 服务器的账号密码通过服务器列表页面管理,添加的...
采用java实现FTP文件的上传下载,包含文件以及文件夹上传下载,新建文件夹等基本相关操作,不会出现文件名的中文乱码,内含demo相关测试以及jar包,可直接导入使用,采用MyEclipse8.5,jdk1.6亲测无问题
NULL 博文链接:https://liugaojian13135.iteye.com/blog/837708
用java语言编写的ftp小工具,可以按指定时间监控ftp服务器,把服务器指定目录内新产生的文件或者文件夹下载到本地指定文件夹,下载后删除数据。 也可以监控本地文件夹,把文件夹内新产生的文件或者文件夹整体上传到...
android上可以用的ftp客户端代码,支持断点续传。
本类提供了java对ftp的各种操作的!经过精心的整理!相信对初学者有很大的帮助!
java 连接 FTP 文件操作(上传,下载,删除,复制
Java ftp工具类,可以实现ftp上传,读取,目录切换,内容创建,目录创建、检查文件是否存在,支持主动方式和被动方式读取
凭借着这个工具类,可以方便的上传文件到FTP服务器,也可从FTP下载指定的文件到本地
因此选择了FTP上传,在网上搜索了很多例子后并经自己对比整理后决定采用apache包中的FTP实现上传下载,在使用过程需要注意的是对中文名进行字符转码,FTP服务器我选用的是Serve-U,这个工具以前用过,比较稳定。...
java ftp 文件上传,两个工具类,一个是直接写的,一个是进行了二次封装的。代码示例,项目工程jdk版本1.7的。
改工具是方便自己开发FTP上传下载需求写的,直接拿来就可以使用,方便那些自己懒得写,或者是想借鉴开发的人用。