利用Apache commons-net 包进行FTP文件和文件夹的上传与下载
首先声明:这段代码是我在网上胡乱找的,调试后可用。
需要提前导入jar包,我导入的是:commons-net-3.0.1,在网上可以下载到。以下为源码,其中文件夹的下载存在问题:FTPFile[] allFile = this.ftpClient.listFiles("/uploadimagefiles"); listFiles()方法总是返回为空,问题仍未解决,如有大神,还请指点。
package Ftp;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.TimeZone;
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.apache.log4j.Logger; public class Ftp {
private FTPClient ftpClient;
private String strIp;
private int intPort;
private String user;
private String password; //Ftp构造函数
public Ftp(String strIp, int intPort, String user, String password) {
this.strIp = strIp;
this.intPort = intPort;
this.user = user;
this.password = password;
this.ftpClient = new FTPClient();
} //登录到FTP服务器,判断是否登入成功,成功返回true,不成功返回false。
public boolean ftpLogin() {
boolean isLogin = false;
FTPClientConfig ftpClientConfig = new FTPClientConfig();
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
this.ftpClient.setControlEncoding("GBK");
this.ftpClient.configure(ftpClientConfig);
try {
if (this.intPort > 0) {
this.ftpClient.connect(this.strIp, this.intPort);
} else {
this.ftpClient.connect(this.strIp);
}
// FTP服务器连接回答
int reply = this.ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
System.out.println("登录FTP服务失败!");
return isLogin;
}
this.ftpClient.login(this.user, this.password);
// 设置传输协议
this.ftpClient.enterLocalPassiveMode();
this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
System.out.println("用户 " + this.user + " 成功登陆FTP服务器");
isLogin = true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(this.user + "登录FTP服务失败!" + e.getMessage());
}
this.ftpClient.setBufferSize(1024 * 2);
this.ftpClient.setDataTimeout(30 * 1000);
return isLogin;
} //退出关闭服务器链接
public void ftpLogOut() {
if (null != this.ftpClient && this.ftpClient.isConnected()) {
try {
boolean reuslt = this.ftpClient.logout();// 退出FTP服务器
if (reuslt) {
System.out.println("成功退出服务器");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("退出FTP服务器异常!" + e.getMessage());
} finally {
try {
this.ftpClient.disconnect();// 关闭FTP服务器的连接
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭FTP服务器的连接异常!");
}
}
}
} // 上传Ftp文件 @param localFile 本地文件 @param romotUpLoadePath 远程服务器路径 - 应该以/结束
public boolean uploadFile(File localFile, String romoteUpLoadePath) {
BufferedInputStream inStream = null;
boolean success = false;
try {
this.ftpClient.changeWorkingDirectory(romoteUpLoadePath);// 改变工作路径
inStream = new BufferedInputStream(new FileInputStream(localFile));
System.out.println(localFile.getName() + "开始上传.....");
long beforeTime=System.currentTimeMillis();
success = this.ftpClient.storeFile(localFile.getName(), inStream);
long afterTime=System.currentTimeMillis();
if (success == true) {
double time=(afterTime-beforeTime)/1000.0;
double uploadSpeed=(localFile.length()/(time*1024));
uploadSpeed = (double)Math.round(uploadSpeed*100)/100;
System.out.println(localFile.getName() + "上传成功,用时:"+time+"秒."+"平均上传速度:"+uploadSpeed+"KB/秒");
return success;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println(localFile + "未找到");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
} /***
* 下载文件
* @param remoteFileName 待下载文件名称
* @param localDires 下载到当地那个路径下
* @param remoteDownLoadPath remoteFileName所在的路径
* */
public boolean downloadFile(String remoteFileName, String localDires,
String remoteDownLoadPath) {
String strFilePath = localDires + "/" + remoteFileName;
BufferedOutputStream outStream = null;
boolean success = false;
try {
this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);
outStream = new BufferedOutputStream(new FileOutputStream(strFilePath));
System.out.println(remoteFileName + "开始下载....");
long beforeTime=System.currentTimeMillis();
success = this.ftpClient.retrieveFile(remoteFileName, outStream);
long afterTime=System.currentTimeMillis(); if (success == true) {
double time=(afterTime-beforeTime)/1000.0;
File file=new File(strFilePath);
long length=file.length();
double downloadSpeed=(length/(time*1024));
downloadSpeed = (double)Math.round(downloadSpeed*100)/100;
System.out.println(remoteFileName + "成功下载到" + strFilePath+"用时"+time+"秒."+"平均下载速度:"+downloadSpeed+"KB/秒");
return success;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(remoteFileName + "下载失败");
} finally {
if (null != outStream) {
try {
outStream.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (success == false) {
System.out.println(remoteFileName + "下载失败!!!");
}
return success;
} /***
* @上传文件夹
* @param localDirectory
* 当地文件夹
* @param remoteDirectoryPath
* Ftp 服务器路径 以目录"/"结束
* */
public boolean uploadDirectory(String localDirectory,
String remoteDirectoryPath) {
File src = new File(localDirectory);
try {
remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/";
this.ftpClient.makeDirectory(remoteDirectoryPath);
// ftpClient.listDirectories();
} catch (IOException e) {
e.printStackTrace();
System.out.println(remoteDirectoryPath + "目录创建失败");
}
File[] allFile = src.listFiles();
for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();
uploadFile(new File(srcName), remoteDirectoryPath);
}
}
for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
if (allFile[currentFile].isDirectory()) {
// 递归
uploadDirectory(allFile[currentFile].getPath().toString(),
remoteDirectoryPath);
}
}
return true;
} /***
* @下载文件夹
* @param localDirectoryPath本地地址
* @param remoteDirectory 远程文件夹
* */
public boolean downLoadDirectory(String localDirectoryPath,String remoteDirectory) {
try {
String fileName = new File(remoteDirectory).getName();
localDirectoryPath = localDirectoryPath + "/"+ fileName + "/";
new File(localDirectoryPath).mkdirs();
ftpClient.enterLocalPassiveMode();
FTPFile[] allFile = this.ftpClient.listFiles("/uploadimagefiles");
for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
if (!allFile[currentFile].isDirectory()) {
downloadFile(allFile[currentFile].getName(),localDirectoryPath, remoteDirectory);
}
}
for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
if (allFile[currentFile].isDirectory()) {
String strremoteDirectoryPath = remoteDirectory + "/"+ allFile[currentFile].getName();
downLoadDirectory(localDirectoryPath,strremoteDirectoryPath);
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("下载文件夹失败");
return false;
}
return true;
}
// FtpClient的Set 和 Get 函数
public FTPClient getFtpClient() {
return ftpClient;
}
public void setFtpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
} public static void main(String[] args) throws IOException {
// Ftp ftp=new Ftp("172.19.211.0",21,"Vincent","wanghe");
// ftp.ftpLogin();
//上传文件夹
//ftp.uploadDirectory("e://a1", "/");
//下载文件夹
// ftp.downLoadDirectory("d://shixizkr", "/uploadimagefiles/");
// File file = new File("e://余罪.mp4");
//ftp.uploadFile(file, "/uploadimagefiles");
// ftp.downloadFile("余罪.mp4", "e://a1", "/uploadimagefiles");
// ftp.ftpLogOut();
}
}
利用Apache commons-net 包进行FTP文件和文件夹的上传与下载的更多相关文章
- Java实现FTP文件与文件夹的上传和下载
Java实现FTP文件与文件夹的上传和下载 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制 ...
- win7下利用ftp实现华为路由器的配置文件上传和下载
win7下利用ftp实现华为路由器的配置文件上传和下载 1. Win7下ftp的安装和配置 (1)开始—>控制面板—>程序—>程序和功能—>打开或关闭Windows功能 (2 ...
- Java web开发——文件夹的上传和下载
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
- JavaScript开发——文件夹的上传和下载
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...
- php web开发——文件夹的上传和下载
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- JS开发——文件夹的上传和下载
文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...
- 基于commons-net实现ftp创建文件夹、上传、下载功能
原文:http://www.open-open.com/code/view/1420774470187 package com.demo.ftp; import java.io.FileInputSt ...
- B/S开发——文件夹的上传和下载
本人在2010年时使用swfupload为核心进行文件的批量上传的解决方案.见文章:WEB版一次选择多个文件进行批量上传(swfupload)的解决方案. 本人在2013年时使用plupload为核心 ...
- asp.net web开发——文件夹的上传和下载
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...
随机推荐
- 源码安装zabbix_server服务端
按照上一篇安装lnmp环境:http://www.cnblogs.com/armo/p/6067716.html 保证lnmp正常运行,然后安装zabbix_server 安装依赖 yum -y in ...
- ISO文件:AMD64和i386的区别
下载kali系统时,出现两个选项:ADM64和i386,那么这两者的区别是什么? i386=Intel 80386.其实i386通常被用来作为对Intel(英特尔)32位微处理器的统称. AMD6 ...
- Java学习笔记--监视目录变化
1.在实际开发中可能会需要监视某个目录下的文件所发生的变化. 2.在java7之前的做法 在一个独立的线程中使用File类的listFiles方法来定时检查目录中的内容,并与之前的内容进行比较 ...
- org.apache.commons.lang3 的随机数生成
apache org.apache.commons.lang3 的随机数生成工具,方便使用. String a12 = RandomStringUtils.random(4, "012345 ...
- 【Android Developers Training】 92. 序言:使用同步适配器传输数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- centos生成公钥私钥 securecrt通过公钥访问服务器 winscp通过公钥访问服务器
忙碌了一下午,一直到写博客现在.都在纠结阿里云服务器上配置公钥私钥,网上的说辞总是参差不齐,需要各个去综合,合理取舍.今天终于配置好了. 我就不说这种方式的重要性了,往往黑客都不需要你的登陆账户密码就 ...
- 利用CSS3新特性实现完全兼容的自定义滚动条。
背景:最近项目里面因为统一页面风格,用到了自定义滚动条,在完成之前的那个滚动条的时候,与网上各个滚动条插件实现的方法类似,相当于造了轮子,通过css3的 网上看到的滚动条插件多数是通过监听内容的滚动事 ...
- 读书共享 Primer Plus C-part 7
第十章 数组和指针 1.关于二维数组跟指针 #include<stdio.h> int main() { ][]={{,,,},{,,,},{,,,}}; ; ;i< ;i++) ...
- 我的学习之路_第二十五_javaScript
Javascript 作用:可以对表单数据进行校验,可以对页面实现一些动态效果 定义: JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型. 它的解释器被称为 ...
- Bootstrap模态弹出框
前面的话 在 Bootstrap 框架中把模态弹出框统一称为 Modal.这种弹出框效果在大多数 Web 网站的交互中都可见.比如点击一个按钮弹出一个框,弹出的框可能是一段文件描述,也可能带有按钮操作 ...