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. Python中datetime库的用法

    datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...

  2. 网络编程[第一篇]基于tcp协议的套接字编程

    将服务端-客户端的连接比作双方打电话的过程 2019-07-24 一.客户端 主动的一方: 客户端实例化一个socket对象--> 主动像服务端发送连接请求--> (服务端接受请求后即可进 ...

  3. SAS学习笔记38 SAS Comments注释语句

    通常来讲,注释语句有四种: 1.* message; 2.COMMENT message; 3./* message */ 4.%* message; 第一种的主要限制是注释之中不得有“:”符号.通常 ...

  4. react封装通用tab组件

    import React, { Component } from 'react' import PropTypes from 'prop-types' import _ from 'lodash' i ...

  5. ubuntu 快捷方式添加 applications添加

    首先我们要了解,Ubuntu 的 Dash 里所有程序都是在 /usr/share/applications 中的,所以我们的思路很简单——建一个类似于“快捷方式”一样的东西扔进去就好了.所以第一步自 ...

  6. Sparse PCA 稀疏主成分分析

    Sparse PCA 稀疏主成分分析 2016-12-06 16:58:38 qilin2016 阅读数 15677 文章标签: 统计学习算法 更多 分类专栏: Machine Learning   ...

  7. CSS一些常用样式

    限制行数溢出省略号 display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: ; overflow: hidden ...

  8. C#通过重载构造函数传递参数、实现两个窗体下的方法的互相调用

    直接切入主题 有时候同一个项目下我们可能会使用多个窗体,窗体间方法互相调用也不可避免,好了,使用无参无返回值的方法,开始上图 1.新建一个winform项目Form1,并再添加一个窗体Form2:拖入 ...

  9. IDEA安装及默认配置习惯配置(一)

    最新新转战IDEA,每次安装完需要做一些操作习惯的设置,在这里记录一下,下次安装可以快速上手用. 第一步,JAVA安装 JDK官方下载地址:https://www.oracle.com 下载JDK时根 ...

  10. Point to class member

    #include <iostream> using namespace std; class Student { public: Student(string n, int nu):nam ...