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 ...
随机推荐
- laravel6.0路由
1.基本路由路由定义在routes目录下,路由执行是在控制器之前,路由路径 routes目录下api.php 关于接口路由定义文件包含的路由位于 api 中间件组约束之内,支持频率限制功能,这些路由是 ...
- web服务器/HTTP协议基础
1.http协议:一种规范和约定,实现客户端和服务器的通信2.http请求格式:请求行+请求头+请求体 请求行:method + request-URI + http-version 方法+请求的资源 ...
- WUSTOJ 1276: 峰峰不搞G(Java)
1276: 峰峰不搞G 题目 给 n 数量的油漆,写出最大的数,每个数对应有油漆的花费.更多内容点击标题. 分析 我读完题,就想到用动态规划,结果是Time Limit Exceed.然后看了 ...
- diy操作系统 附录:gcc栈帧开启与关闭
在gcc命令行参数中可以使用-fno-omit-frame-pointer来开启栈帧的使用,或者使用-fomit-frame-pointer选项来关闭. 然而,也可以针对某一个函数进行配置方法如下,这 ...
- Git拉取代码切换分支
1.克隆代码 cd d:/GitTest //指定存放的目录 git clone https://git.oschina.net/name/test.git //你的仓库地址 2.查看远程所有分支 g ...
- react中jsx文件是如何转换成js对象的
通过在线babel转换器,转换出jsx是如何变成js对象的 jsx文件 加入了正常的标签以及嵌套标签以及方法属性 function hello() { click=()=>{ console.l ...
- CAS 5.x搭建常见问题系列(3).Failure to find org.apereo.cas:cas-server-support-pm-jdbc:jar:5.1.9
错误内容 cas overlay的pom.xml增加了cas-server-support-pm-jdbc.jary依赖后, 打包(mvn package)出现如下的报错 D:\casoverlay\ ...
- IE浏览器 Table 兼容问题
border篇: 直接在<tr>标签上添加 border-bottom:1px solid red; 在IE8以上浏览器有效,IE7以下版本无效 解决方案:需给<table&g ...
- Java Web 深入分析(3) CDN
CDN (Content Delivery NetWork) 内容分发网络,它是构筑在现有互联网基础上的一种先进的流量分配网络.区别于镜像,相当于是 CDN = 镜像(mirror) + 缓存(Cac ...
- Django2.0 开始一个项目
python项目运行环境: 安装虚拟环境工具 pip install virtualenv 使用虚拟环境: 创建虚拟环境: virtualenv <虚拟环境名称> 进去虚拟环境: S ...