正所谓工欲善其事必先利其器,熟悉了下一套流程,以此铭记。

1.FTP服务搭建

由于本人使用wondiow系统,所以针对window的童鞋们可以查看。至于windowX这里配置类似,所以不要纠结于window系统不同。经过以上操作我们可以获取到ftp的用户名,密码,以及IP地址,端口号(ps:默认端口号,搭建时可以设置)

2.FTP编码集问题

由于FTP使用编码集为ISO-8859-1,所以在中文文件夹以及文件名称上传,获取服务器文件等会出现问题。有以下2中解决方案:

1.将目录、文件转码,这样就造成跟目录、文件相关的都要转码,以至于

      ftpClient.changeWorkingDirectory(new String(onepath.getBytes("GBK"),"iso-8859-1"));
      ftpClient.storeFile(new String(fileName.getBytes("GBK"),"iso-8859-1"), in);
      ftpClient.listFiles(new String(remotePath.getBytes("GBK"),"iso-8859-1"));

那这样岂不是很麻烦,而且每次使用都需要转码,有没有更简单的呢?有,请看第2中

2.设置相应编码集等信息

      ftpClient = new FTPClient();
      ftpClient.setControlEncoding("GBK");
      FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);//系统类型   这表示window系统
      conf.setServerLanguageCode("zh"); 
      ftpClient.configure(conf);
      ftpClient.connect(this.ip, this.port);

这边需要注意的是顺序必须这么写

3.工具类相关

1.Jar包问题

由于本人使用maven,pom中的配置

      <dependency>
      <groupId>commons-net</groupId>
       <artifactId>commons-net</artifactId>
      <version>3.5</version>
       <classifier>ftp</classifier>
      </dependency>

2.工具类开发

 import org.apache.commons.net.ftp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.*;
import java.util.Arrays;
import java.util.List; /**
* FTP上传工具类
*/
public class FtpUtil {
private static Logger log = LoggerFactory.getLogger(FtpUtil.class);
private final static String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");
private String ip;
private String username;
private String password;
private int port = 21;
private FTPClient ftpClient = null; public FtpUtil(String serverIP, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
} public FtpUtil(String serverIP, int port, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;
} /**
* 连接ftp服务器
*/
public boolean connectServer() {
boolean result = true;
try {
ftpClient = new FTPClient();
ftpClient.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
ftpClient.configure(conf);
ftpClient.connect(this.ip, this.port);
ftpClient.login(this.username, this.password);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
result = false;
}
} catch (IOException e) {
e.printStackTrace();
log.error("连接ftp服务器失败", e);
result = false;
}
return result;
} /**
* 断开与ftp服务器连接
*/
public boolean closeServer() {
try {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
return true;
} catch (IOException e) {
log.error("断开与ftp服务器连接失败", e);
return false;
}
} /**
* 向FTP根目录上传文件
*
* @param localFile
* @param newName 文件名称
* @throws Exception
*/
public boolean uploadFile(String localFile, String newName) {
boolean success = false;
InputStream input = null;
try {
File file = null;
if (checkFileExist(localFile)) {
file = new File(localFile);
}
input = new FileInputStream(file);
success = ftpClient.storeFile(newName, input);
} catch (Exception e) {
log.error("FTP根目录上传文件上传失败(ps:传入文件名)", e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
log.error("FTP根目录上传文件上传(ps:传入文件名),输入流关闭失败", e);
}
}
}
return success;
} /**
* 向FTP根目录上传文件
*
* @param input
* @param newName 新文件名
* @throws Exception
*/
public boolean uploadFile(InputStream input, String newName) {
boolean success = false;
try {
success = ftpClient.storeFile(newName, input);
} catch (Exception e) {
log.error("FTP根目录上传文件上传失败(ps:传入文件流)", e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
log.error("FTP根目录上传文件上传(ps:传入文件流),输入流关闭失败", e);
}
}
}
return success;
} /**
* 向FTP指定路径上传文件
*
* @param localFile
* @param newName 新文件名
* @param remoteFoldPath
* @throws Exception
*/
public boolean uploadFile(String localFile, String newName, String remoteFoldPath) {
InputStream input = null;
boolean success = false;
try {
// 改变当前路径到指定路径
if (!this.changeDirectory(remoteFoldPath)) {
return false;
}
File file = null;
if (checkFileExist(localFile)) {
file = new File(localFile);
}
input = new FileInputStream(file);
success = ftpClient.storeFile(newName, input);
} catch (Exception e) {
log.error("FTP指定路径上传文件失败(ps:本地文件路径以及ftp服务器中文件名)", e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
log.error("FTP指定路径上传文件失败(ps:本地文件路径以及ftp服务器中文件名),输入流关闭失败", e);
}
}
}
return success;
} /**
* 向FTP指定路径上传文件
*
* @param input
* @param newName 新文件名
* @param remoteFoldPath
* @throws Exception
*/
public boolean uploadFile(InputStream input, String newName, String remoteFoldPath) throws Exception {
boolean success = false;
try {
// 改变当前路径到指定路径
if (!this.changeDirectory(remoteFoldPath)) {
return false;
}
success = ftpClient.storeFile(newName, input);
} catch (Exception e) {
log.error("FTP指定路径上传文件失败(ps:本地文件流及ftp服务器中文件名)", e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
log.error("FTP指定路径上传文件失败(ps:本地文件流及ftp服务器中文件名),输入流关闭失败", e);
}
}
}
return success;
} /**
* 从FTP服务器下载文件
*
* @param remotePath FTP路径(不包含文件名)
* @param fileName 下载文件名
* @param localPath 本地路径
*/
public boolean downloadFile(String remotePath, String fileName, String localPath) {
BufferedOutputStream output = null;
boolean success = false;
try {
// 检查本地路径
if (!this.checkFileExist(localPath)) {
return false;
}
// 改变工作路径
if (!this.changeDirectory(remotePath)) {
return false;
}
// 列出当前工作路径下的文件列表
List<FTPFile> fileList = this.getFileList();
if (fileList == null || fileList.size() == 0) {
return false;
}
for (FTPFile ftpfile : fileList) {
if (ftpfile.getName().equals(fileName)) {
File localFilePath = new File(localPath + File.separator + ftpfile.getName());
output = new BufferedOutputStream(new FileOutputStream(localFilePath));
success = ftpClient.retrieveFile(ftpfile.getName(), output);
}
}
} catch (Exception e) {
log.error("FTP服务器下载文件失败", e);
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
log.error("FTP服务器下载文件,输出流关闭失败", e);
}
}
}
return success;
} /**
* 从FTP服务器获取文件流
*
* @param remoteFilePath
* @return
* @throws Exception
*/
public InputStream downloadFile(String remoteFilePath) {
try {
return ftpClient.retrieveFileStream(remoteFilePath);
} catch (IOException e) {
log.error("FTP服务器获取文件流失败", e);
}
return null;
} /**
* 获取FTP服务器上[指定路径]下的文件列表
*
* @param remotePath
* @return
*/
public List<FTPFile> getFtpServerFileList(String remotePath) {
try {
FTPListParseEngine engine = ftpClient.initiateListParsing(remotePath);
return Arrays.asList(engine.getNext(25));
} catch (IOException e) {
log.error("获取FTP服务器上[指定路径]下的文件列表(getFtpServerFileList)失败", e);
}
return null;
} /**
* 获取FTP服务器上[指定路径]下的文件列表
*
* @param remotePath
* @return
*/
public List<FTPFile> getFileList(String remotePath) {
List<FTPFile> ftpfiles = null;
try {
ftpfiles = Arrays.asList(ftpClient.listFiles(remotePath));
} catch (IOException e) {
log.error("获取FTP服务器上[指定路径]下的文件列表(getFileList)失败", e);
}
return ftpfiles;
} /**
* 获取FTP服务器[当前工作路径]下的文件列表
*
* @return
*/
public List<FTPFile> getFileList() {
List<FTPFile> ftpfiles = null;
try {
ftpfiles = Arrays.asList(ftpClient.listFiles());
} catch (IOException e) {
log.error("获取FTP服务器[当前工作路径]下的文件列表失败", e);
}
return ftpfiles;
} /**
* 改变FTP服务器工作路径
*
* @param remoteFoldPath
*/
public boolean changeDirectory(String remoteFoldPath) {
boolean result = false;
try {
result = ftpClient.changeWorkingDirectory(new String(remoteFoldPath));
} catch (IOException e) {
log.error("改变FTP服务器工作路径失败", e);
}
return result;
} /**
* 删除文件
*
* @param remoteFilePath
* @return
* @throws Exception
*/
public boolean deleteFtpServerFile(String remoteFilePath) {
boolean result = false;
try {
result = ftpClient.deleteFile(remoteFilePath);
} catch (IOException e) {
log.error("FTP服务器删除文件失败", e);
}
return result;
} /**
* 删除目录
*
* @param remoteFoldPath
* @return
* @throws Exception
*/
public boolean deleteFold(String remoteFoldPath) {
boolean result = false;
try {
result = ftpClient.removeDirectory(remoteFoldPath);
} catch (IOException e) {
log.error("FTP服务器删除目录失败", e);
}
return result;
} /**
* 删除目录以及文件
*
* @param remoteFoldPath
* @return
*/
public boolean deleteFoldAndsubFiles(String remoteFoldPath) {
boolean success = false;
List<FTPFile> list = this.getFileList(remoteFoldPath);
if (list == null || list.size() == 0) {
return deleteFold(remoteFoldPath);
}
for (FTPFile ftpFile : list) {
String name = ftpFile.getName();
if (ftpFile.isDirectory()) {
success = deleteFoldAndsubFiles(remoteFoldPath + FILE_SEPARATOR + name);
} else {
success = deleteFtpServerFile(remoteFoldPath + FILE_SEPARATOR + name);
}
if (!success) {
break;
}
}
if (!success) {
return false;
}
success = deleteFold(remoteFoldPath);
return success;
} /**
* 创建目录
*
* @param remoteFoldPath
* @return
*/
public boolean createFold(String remoteFoldPath) {
boolean result = false;
try {
result = ftpClient.makeDirectory(remoteFoldPath);
} catch (IOException e) {
log.error("FTP服务器创建目录失败", e);
}
return result;
} /**
* 检查本地路径是否存在
*
* @param filePath
* @return
* @throws Exception
*/
public boolean checkFileExist(String filePath) {
return new File(filePath).exists();
} /**
* 把文件移动到特定目录下
*
* @param remoteFile
* @param remoteNewPath
* @return
*/
public boolean moveFtpServerFile(String remoteFile, String remoteNewPath) {
boolean result = false;
try {
result = ftpClient.rename(remoteFile, remoteNewPath);
} catch (IOException e) {
log.error("FTP服务器文件移动失败", e);
}
return result;
} /**
* 判断是文件还是目录
*
* @param remoteOldPath
* @return
*/
public boolean isContents(String remoteOldPath) {
return changeDirectory(remoteOldPath);
} /**
* 递归创建远程服务器目录
*
* @param dirpath 远程服务器文件绝对路径
* @return 目录创建是否成功
*/
public void createDirecroty(String dirpath) {
String directory = dirpath.substring(0, dirpath.lastIndexOf("/") + 1);
try {
if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(new String(directory))) {
// 如果远程目录不存在,则递归创建远程服务器目录
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
while (true) {
String subDirectory = new String(dirpath.substring(start, end));
if (!ftpClient.changeWorkingDirectory(subDirectory)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
}
}
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
} catch (IOException e) {
log.error("递归生成目录失败", e);
}
} /**
* 复制
*
* @param remoteOldPath
* @param remoteNewPath
* @return
*/
public boolean copyFtpServerFile(String remoteOldPath, String remoteNewPath) {
boolean copyFalg = false;
List<FTPFile> filelist;
try {
ftpClient.enterLocalPassiveMode();
filelist = this.getFileList(remoteOldPath);
int length = filelist == null || filelist.isEmpty() ? 0 : filelist.size();
String category = null;
ByteArrayOutputStream fos = null;
ByteArrayInputStream in = null;
if (length > 0) {
boolean flage = false;
if (this.isContents(remoteOldPath)) {
flage = true;
}
changeDirectory("/");
for (FTPFile ftpFile : filelist) {
category = ftpFile.getName();
if (ftpFile.isFile()) {
// 如果是文件则复制文件
ftpClient.setBufferSize(1024);
fos = new ByteArrayOutputStream();
copyFalg = ftpClient.retrieveFile(flage ? remoteOldPath + FILE_SEPARATOR + category : remoteOldPath, fos);
if (!copyFalg) {
return copyFalg;
}
// 如果读取的文件流不为空则复制文件
if (fos != null) {
in = new ByteArrayInputStream(fos.toByteArray());
copyFalg = ftpClient.storeFile(remoteNewPath + FILE_SEPARATOR + category, in);
// 关闭文件流
fos.close();
in.close();
if (!copyFalg) {
return copyFalg;
}
}
} else if (ftpFile.isDirectory()) {
// 如果是目录则先创建该目录
copyFalg = this.createFold(remoteNewPath + FILE_SEPARATOR + category);
// 再进入子目录进行递归复制
copyFtpServerFile(remoteOldPath + FILE_SEPARATOR + category, remoteNewPath + FILE_SEPARATOR + category);
}
}
}
} catch (IOException e) {
log.error("文件复制失败", e);
copyFalg = false;
}
return copyFalg;
} public static void main(String[] args) {
FtpUtil ftpUtil = new FtpUtil("", "", "");
//执行文件移动
/*if (ftpUtil.connectServer()) {
if (ftpUtil.changeDirectory("load")) {
ftpUtil.moveFtpServerFile("xlsj_h1_v0.7.apk", ".." + FILE_SEPARATOR + "down" + FILE_SEPARATOR + "1.apk");
ftpUtil.closeServer();
}
}*/
//复制 xlsj_h1_v0.8.apk是目录 xlsj_h1_v0.7.apk是文件
/* if (ftpUtil.connectServer()) {
ftpUtil.copyFtpServerFile("load/xlsj_h1_v0.8.apk", "down");
ftpUtil.copyFtpServerFile("load/xlsj_h1_v0.8.apk/xlsj_h1_v0.7.apk", "down");
ftpUtil.closeServer();
}*/ if (ftpUtil.connectServer()) {
ftpUtil.copyFtpServerFile("load/安卓上传包/安卓开发包.apk", "down");
ftpUtil.closeServer();
}
}
}

 参考:

  http://blog.csdn.net/iheng_scau/article/details/41682511

  http://blog.163.com/biangji_and/blog/static/3382923020102491050525/

  http://bbs.csdn.net/topics/390373219

  http://commons.apache.org/proper/commons-net/javadocs/api-1.4.1/org/apache/commons/net/ftp/FTPClient.html

  http://yangyangmyself.iteye.com/blog/1299997

FTP工具类开发的更多相关文章

  1. java:工具(汉语转拼音,压缩包,EXCEL,JFrame窗口和文件选择器,SFTP上传下载,FTP工具类,SSH)

    1.汉语转拼音: import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuP ...

  2. Java操作FTP工具类(实例详解)

    这里使用Apache的FTP jar 包 没有使用Java自带的FTPjar包  工具类 package com.zit.ftp; import java.io.File; import java.i ...

  3. 静态资源上传至远程ftp服务器,ftp工具类封装

    工具类,是一个单独的工程项目 提取必要信息至ftp.properties配置文件中 ftp_host=192.168.110.128 ftp_port=21 ftp_username=ftpuser ...

  4. ftp工具类

    package com.ytd.zjdlbb.service.zjdlbb; import java.io.File;import java.io.FileInputStream;import jav ...

  5. 数据库模型类转换实体类的dbToPojoUtil工具类开发

    idea颜色说明http://blog.csdn.net/saindy5828/article/details/53319693 1,中途运用了properties,properties.getPro ...

  6. Java-FtpUtil工具类

    package cn.ipanel.app.newspapers.util; import java.io.BufferedReader; import java.io.DataInputStream ...

  7. java 工具类使用

    BigDecimalUtil 金额计算工具类 import java.math.BigDecimal; public class BigDecimalUtil { private BigDecimal ...

  8. MSCL超级工具类(C#),开发人员必备,开发利器

    MSCL超强工具类库 是基于C#开发的超强工具类集合,涵盖了日常B/S或C/S开发的诸多方面,包含上百个常用封装类(数据库操作类全面支持Mysql.Access.Oracle.Sqlserver.Sq ...

  9. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

随机推荐

  1. 开始研究web,mark一下

    之前想要搞引擎,经过思考之后,定位为webgl方面的引擎,这个决定早就做了,只是没有写下来   做了一些调研之后,确定使用babylon.js 和typescript 和c# 来开发   Babylo ...

  2. 有关bootstrap

    最近在接触对移动浏览器很友好的bootstrap,遂整理了一点笔记: 简单的html页面: <!DOCTYPE html><html> <head> <tit ...

  3. UIwebView 和 H5交互详情

    背景: 最近公司准备上一个只有原生登录界面 + H5网页 ,并且支持ios7.0 以上系统的混合app;这可把我难住了,原生的UI界面我可以正写反写各种style把界面搭建起来.而要这个app的难点在 ...

  4. 让easyui datagrid支持bootstrap的tooltip

    让easyui datagrid支持bootstrap的tooltip 发表于 下午 1:53 by ylpro.net & 分类 Java. Easyui在1.3.3版本之前是不支持tool ...

  5. KnockoutJS 3.X API 第七章 其他技术(7) 微任务

    注意:本文档适用于Knockout 3.4.0及更高版本. Knockout的微任务队列 Knockout的微任务队列支持调度任务尽可能快地运行,同时仍然是异步的,努力安排它们在发生I / O,回流或 ...

  6. Entity Framework Code First使用DbContext查询

    DbContext.DbSet及DbQuery是Entity Framework Code First引入的3个新的类,其中DbContext用于保持数据库会话连接,实体变化跟踪及保存,DbSet用于 ...

  7. java中volatile关键字

    一.前言 JMM提供了volatile变量定义.final.synchronized块来保证可见性. 用volatile修饰的变量,线程在每次使用变量的时候,都会读取变量修改后的最的值.volatil ...

  8. Java 8新特性-3 Lambda 表达式

    在 Java 8 之前,匿名内部类,监听器和事件处理器的使用都显得很冗长,代码可读性很差. 在Java 8 之前使用匿名内部类: 例如 interface ITestPrint{ public voi ...

  9. Ubuntu杂记之——JDK、ANT安装、配置

    一.安装JDK 方法一:使用软件源安装openjdk sudo apt-get install openjdk-7-jdk 方法二:使用软件源安装sun jdk sudo apt-get instal ...

  10. MySQL常见错误

    1. TokuFT file system space is really low and access is restricted 解决方法:修改tokudb_fs_reserve_percent参 ...