package com.lct.conference.controller.MonitorManagement.cofer;

 import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply; import com.lct.conference.entity.filemngt.FileManageRes; import java.io.*;
import java.net.SocketException;
import java.util.ArrayList; public class FtpUtil {
/**
* 获取FTPClient对象
*
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return
*/
public static FTPClient getFTPClient(FileManageRes ftpinfo) {
FTPClient ftpClient = new FTPClient();
String ftpHost = ftpinfo.getFtpHost();
String ftpUserName = ftpinfo.getFtpUserName();
String ftpPassword = ftpinfo.getFtpPassword();
int ftpPort = ftpinfo.getFtpPort();
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
System.out.println("FTP连接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置。");
}
return ftpClient;
} /*
* 从FTP服务器下载文件
*
* @param ftpHost FTP IP地址
* @param ftpUserName FTP 用户名
* @param ftpPassword FTP用户名密码
* @param ftpPort FTP端口
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param localPath 下载到本地的位置 格式:H:/download
* @param fileName 文件名称
*/
public static boolean downloadFtpFile(String ftpPath, String localPath,
String fileName,FileManageRes ftpinfo) {
boolean flag = false;
FTPClient ftpClient = null; try {
ftpClient = getFTPClient(ftpinfo);
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(new String(ftpPath.getBytes("GBK"),"iso-8859-1")); File localFile = new File(localPath + File.separatorChar + fileName);
OutputStream os = new FileOutputStream(localFile);
flag = ftpClient.retrieveFile(new String(fileName.getBytes("GBK"),"iso-8859-1"), os);
os.close();
ftpClient.logout();
} catch (FileNotFoundException e) {
System.out.println("没有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (SocketException e) {
System.out.println("连接FTP失败.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取错误。");
e.printStackTrace();
}
return flag;
} /**
* Description: 向FTP服务器上传文件
* @param ftpHost FTP服务器hostname
* @param ftpUserName 账号
* @param ftpPassword 密码
* @param ftpPort 端口
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param fileName ftp文件名称
* @param input 文件流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String ftpPath,String fileName,InputStream input,FileManageRes ftpinfo) {
boolean success = false;
FTPClient ftpClient = null;
try {
int reply;
ftpClient = getFTPClient(ftpinfo);
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return success;
}
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(new String(ftpPath.getBytes("GBK"),"iso-8859-1"));
ftpClient.storeFile(new String(fileName.getBytes("GBK"),"iso-8859-1"), input);
input.close();
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* Description: 向FTP服务器新建文件夹
* @param ftpHost FTP服务器hostname
* @param ftpUserName 账号
* @param ftpPassword 密码
* @param ftpPort 端口
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param fileName ftp文件名称
* @param input 文件流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFolder(String ftpPath,String fileName,FileManageRes ftpinfo) {
boolean success = false;
FTPClient ftpClient = null;
try {
int reply;
ftpClient = getFTPClient(ftpinfo);
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return success;
}
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(new String(ftpPath.getBytes("GBK"),"iso-8859-1"));
ftpClient.makeDirectory(new String(fileName.getBytes("GBK"),"iso-8859-1"));
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* Description: 重命名
* @param ftpHost FTP服务器hostname
* @param ftpUserName 账号
* @param ftpPassword 密码
* @param ftpPort 端口
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param fileName ftp文件名称
* @param input 文件流
* @return 成功返回true,否则返回false
*/
public static boolean renameFile(String ftpPath, String oldfileName,String newfileName,FileManageRes ftpinfo) {
boolean success = false;
FTPClient ftpClient = null;
try {
int reply;
ftpClient = getFTPClient(ftpinfo);
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return success;
}
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(new String(ftpPath.getBytes("GBK"),"iso-8859-1"));
ftpClient.rename(new String(oldfileName.getBytes("GBK"),"iso-8859-1"),new String(newfileName.getBytes("GBK"),"iso-8859-1"));
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
//TODO
public static boolean downLoadFolder(String ftpPath, String localPath,String fileName,FileManageRes ftpinfo){
boolean flag = false;
FTPClient ftpClient = null;
String newfilename = null;
String folderpath = fileCreate(localPath + File.separatorChar + fileName);
try {
ftpClient = getFTPClient(ftpinfo);
ftpClient.setControlEncoding("GBK");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ArrayList<String> pathArray=new ArrayList<String>();
if("".equals(ftpPath)){
ftpClient.changeWorkingDirectory(new String(fileName.getBytes("GBK"),"iso-8859-1"));
}else{
ftpClient.changeWorkingDirectory(new String((ftpPath+File.separatorChar+fileName).getBytes("GBK"),"iso-8859-1"));
}
getPath(ftpClient,fileName,pathArray,folderpath);
download(ftpClient, pathArray, folderpath);
// if(ftpPath==null){
// ftpClient.changeWorkingDirectory(new String(fileName.getBytes("GBK"),"iso-8859-1"));
// }else{
// ftpClient.changeWorkingDirectory(new String((ftpPath+File.separatorChar+fileName).getBytes("GBK"),"iso-8859-1"));
// }
// FTPFile[] files = ftpClient.listFiles();
// for(FTPFile file:files){
// if(file.isFile()){
// newfilename = file.getName();
// File localFile = new File(folderpath + File.separatorChar + newfilename);
// OutputStream os = new FileOutputStream(localFile);
// flag = ftpClient.retrieveFile(new String(newfilename.getBytes("GBK"),"iso-8859-1"), os);
// os.close();
// ftpClient.logout();
// }else if(file.isDirectory()){
// newfilename = file.getName();
// }
// }
} catch (FileNotFoundException e) {
System.out.println("没有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (SocketException e) {
System.out.println("连接FTP失败.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取错误。");
e.printStackTrace();
}
return flag;
}
public static void getPath(FTPClient ftp,String path,ArrayList<String> pathArray,String folderpath) throws IOException{
FTPFile[] files = ftp.listFiles();
for (FTPFile ftpFile : files) {
if(ftpFile.isFile()){
if("".equals(path)){
path+=ftpFile.getName();
}else{
path+="\\"+ftpFile.getName();
}
pathArray.add(path);
}
if(ftpFile.isDirectory()){//如果是目录,则递归调用,查找里面所有文件
if("".equals(path)){
path+=ftpFile.getName();
}else{
path+="\\"+ftpFile.getName();
}
folderpath=folderpath+"\\"+ftpFile.getName();
fileCreate(folderpath);
ftp.changeWorkingDirectory(new String(ftpFile.getName().getBytes("GBK"),"iso-8859-1"));//改变当前路径
getPath(ftp,path,pathArray,folderpath);//递归调用
//path=path.substring(0, path.lastIndexOf("/"));//避免对之后的同目录下的路径构造作出干扰,
}
}
}
public static void download(FTPClient ftp,ArrayList<String> pathArray,String localRootPath) throws IOException{
for (String string : pathArray) {
String localPath=localRootPath+string;
File localFile = new File(localPath);
if (!localFile.exists()) {
localFile.mkdirs();
}
}
for (String string : pathArray) {
String localPath=localRootPath+string;//构造本地路径
ftp.changeWorkingDirectory(string);
FTPFile[] file=ftp.listFiles();
for (FTPFile ftpFile : file) {
if(ftpFile.getName().equals(".")||ftpFile.getName().equals(".."))continue;
File localFile = new File(localPath);
if(!ftpFile.isDirectory()){
OutputStream is = new FileOutputStream(localFile+"/"+ftpFile.getName());
ftp.retrieveFile(ftpFile.getName(), is);
is.close();
}
}
}
}
/**
* 创建文件夹
* @param parentPath 父类路径
* @param fileDirName 创建文件夹的名称
* @return 返回创建文件夹的路径
*/
public static String fileCreate(String parentPath){
String path=null;
File pFile=new File(parentPath);
if(!pFile.exists()){
pFile.mkdir();//如果父类不存在则新建
} path=pFile.getPath();//得到文件路径
//cFile.setWritable(true);//设置为可写操作
return path;
}
}

FTPClient上传下载等的更多相关文章

  1. JAVA中使用FTPClient上传下载

    Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...

  2. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  3. Java FTPClient实现文件上传下载

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  4. JAVA中使用FTPClient实现文件上传下载

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  5. 【FTP】使用org.apache.commons.net.ftp.FTPClient 实现FTP的上传下载

    在此之前,在项目中加上FTP的架包 第一步:配置FTP服务器的相关配置 FtpConfig.java  实体类(配置类) package com.sxd.ftp; public class FtpCo ...

  6. 【FTP】org.apache.commons.net.ftp.FTPClient实现复杂的上传下载,操作目录,处理编码

    和上一份简单 上传下载一样 来,任何的方法不懂的,http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net ...

  7. 【FTP】FTP文件上传下载-支持断点续传

    Jar包:apache的commons-net包: 支持断点续传 支持进度监控(有时出不来,搞不清原因) 相关知识点 编码格式: UTF-8等; 文件类型: 包括[BINARY_FILE_TYPE(常 ...

  8. java实现多线程断点续传,上传下载

    采用apache 的 commons-net-ftp-ftpclient import java.io.File; import java.io.FileOutputStream; import ja ...

  9. Java语言实现简单FTP软件------>上传下载队列窗口的实现(七)

    1.首先看一下队列窗口的界面 2.看一下上传队列窗口的界面 3.看一下下载队列窗口的界面 4.队列窗口的实现 package com.oyp.ftp.panel.queue; import stati ...

随机推荐

  1. Django之ORM相关操作

    一般操作 常用的13个操作 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <3> get(** ...

  2. Java坑人面试题之自动装箱和拆箱后的引用数据类型和基本数据类型的计算

    在Java1.5以后的版本中,引用类型Integer和基本数据类型int之间是可以通过自动装箱和拆箱进行计算的 例如: Integer in = 1; //it means  Integer in = ...

  3. [yarn]yarn和npm的对比

    一.简介 NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服 ...

  4. sql注入测试(2)---实例测试

    以下篇幅,用一个简单的实例说明如何进行测试. 功能:根据用户NAME删除用户,采用的是SQL拼接的方式,核心代码部分如下: public static void deleteByName(String ...

  5. SQL Server系统函数:日期函数

    原文:SQL Server系统函数:日期函数 1.返回当前日期和时间 select GETDATE() '当前日期-精确到33毫秒' select GETUTCDATE() 'UTC日期和时间-精确到 ...

  6. Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form ResumeForm needs updating.

    django 报错 django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fiel ...

  7. ThinkPad T420i 上 Ubuntu 12.04 实现指纹识别登录

    ThinkPad T420i 上 Ubuntu 12.04 实现指纹识别登录 # add ppa add-apt-repository ppa:fingerprint/fprint # update ...

  8. Seaborn(二)之数据集分布可视化

    Seaborn(二)之数据集分布可视化 当处理一个数据集的时候,我们经常会想要先看看特征变量是如何分布的.这会让我们对数据特征有个很好的初始认识,同时也会影响后续数据分析以及特征工程的方法.本篇将会介 ...

  9. Apache ---- Solrl漏洞复现

    Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过Http Get操 ...

  10. Java 之 HashMap 集合

    一.HashMap 概述 java.util.HashMap<k,v> 集合 implements Map<k,v> 接口 HashMap 集合的特点: 1.HashMap 集 ...