FTPClient上传下载等
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上传下载等的更多相关文章
- JAVA中使用FTPClient上传下载
Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...
- JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)
package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...
- Java FTPClient实现文件上传下载
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...
- JAVA中使用FTPClient实现文件上传下载
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...
- 【FTP】使用org.apache.commons.net.ftp.FTPClient 实现FTP的上传下载
在此之前,在项目中加上FTP的架包 第一步:配置FTP服务器的相关配置 FtpConfig.java 实体类(配置类) package com.sxd.ftp; public class FtpCo ...
- 【FTP】org.apache.commons.net.ftp.FTPClient实现复杂的上传下载,操作目录,处理编码
和上一份简单 上传下载一样 来,任何的方法不懂的,http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net ...
- 【FTP】FTP文件上传下载-支持断点续传
Jar包:apache的commons-net包: 支持断点续传 支持进度监控(有时出不来,搞不清原因) 相关知识点 编码格式: UTF-8等; 文件类型: 包括[BINARY_FILE_TYPE(常 ...
- java实现多线程断点续传,上传下载
采用apache 的 commons-net-ftp-ftpclient import java.io.File; import java.io.FileOutputStream; import ja ...
- Java语言实现简单FTP软件------>上传下载队列窗口的实现(七)
1.首先看一下队列窗口的界面 2.看一下上传队列窗口的界面 3.看一下下载队列窗口的界面 4.队列窗口的实现 package com.oyp.ftp.panel.queue; import stati ...
随机推荐
- laravel中间件的实现原理
中间件的实现原理 运用 array_reduce 以及 call_user_func 实现 interface Middleware { public static function handle(C ...
- python学习-11 运算符2
布尔值 1.真 true 假false name = 'abc' c = 'c' in name print(c) 运算结果: True Process finished with exit code ...
- 20191011-构建我们公司自己的自动化接口测试框架-TestData的数据准备
创建excel测试数据准备,excel的第一个sheet存储测试集,后面分别为测试用例和断言结果表 测试集构成如下: 按列分别为测试序号,测试用例说明,对应的sheetname,测试用例是否允许,测试 ...
- adb 安装 app/apk链接不上设备和安装出现failed_install_user_restricted的解决方法
1.手机链接电脑,保持网段一致,通过ping 看是否可以ping通 2.如果可以ping通,查看telnet ip 5555 看是否可以连接 3.如果无法连接查看手机是否开启开发者模式中的debug模 ...
- Docker相关环境全套安装文档兼小技能
Docker相关环境全套安装文档兼小技能 以下环境皆为ubuntu16.04,主要安装docker,docker-compose,docker仓库等. Docker安装 参考官方 A: 有源安装 Ub ...
- Java容器汇总【红黑树需要再次学习】
1,概述 2,Collection 2.1,Set[接触比较少] 2.1.1 TreeSet 底层由TreeMap实现 基于红黑树实现,支持有序性操作,例如根据一个范围查找元素的操作.但是查找效率不如 ...
- PHP函数问题
有时候,运行nginx和PHP CGI(PHP FPM)web服务的Linux服务器,突然系统负载上升,用top命令查看,很多phpcgi进程的CPU利用率接近100%后来通过跟踪发现,这种情况与PH ...
- Linux命令——cat、more、less、head、tail
cat 一次显示整个文件 -n:显示行号 -b :和 -n 相似,只不过对于空白行不编号 -s:当遇到有连续两行以上的空白行,就代换为一行的空白行 -E显示换行符 [root@localhost ~] ...
- CentOS 8 (1905)系统安装
本章内容: CentOS 8 的安装(CentOS-8-1905) 一.安装光盘,选择Install CentOS Linux 8.0.1905 二.选择系统语言,我这里选的是英文,也可以选择中文,往 ...
- Ubuntu系统---C++之Eclipse编译器 CDT插件安装
Ubuntu系统---Ecli ...