Java-FtpUtil工具类
package cn.ipanel.app.newspapers.util;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/**
* FTP工具类<br>
* 注:上传,可上传文件、文件夹;下载,仅实现下载文件功能,不能识别子文件夹
* @version 1.0.0
*/
public class FtpUtil
{
private static Logger logger = Logger.getLogger(FtpUtil.class);
private FtpClient ftpClient;
/**
* 连接FTP服务器,使用默认FTP端口
* @since
* @param serverIp
* 服务器Ip地址
* @param user
* 登陆用户
* @param password
* 密码
* @throws IOException
*/
public void connect(String serverIp, String user, String password) throws Exception
{
try
{
// serverIp:FTP服务器的IP地址;
// user:登录FTP服务器的用户名
// password:登录FTP服务器的用户名的口令;
ftpClient = new FtpClient();
ftpClient.openServer(serverIp);
ftpClient.login(user, password);
// 用二进制传输数据
ftpClient.binary();
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
/**
* 连接ftp服务器,指定端口
* @since
* @param serverIp
* @param port
* 服务器FTP端口号
* @param user
* @param password
* @throws IOException
*/
public void connect(String serverIp, int port, String user, String password) throws Exception
{
try
{
// serverIp:FTP服务器的IP地址;
// post:FTP服务器端口
// user:登录FTP服务器的用户名
// password:登录FTP服务器的用户名的口令;
ftpClient = new FtpClient();
ftpClient.openServer(serverIp, port);
ftpClient.login(user, password);
// 用2进制传输数据
ftpClient.binary();
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
/**
* 断开与ftp服务器的连接
* @since
* @throws IOException
*/
public void disConnect() throws Exception
{
try
{
if (ftpClient != null)
{
ftpClient.sendServer("QUIT\r\n");
ftpClient.readServerResponse();
// ftpClient.closeServer();
}
}
catch (IOException ex)
{
logger.error("DisConnect to FTP server failure! Detail:", ex);
throw new Exception(ex);
}
}
/**
* 上传文件至FTP服务器,保持原文件名
*
* @throws java.lang.Exception
* @return -1 文件不存在或不能读取; >0 成功上传,返回文件的大小
* @param localFile
* 待上传的本地文件
*/
public long upload(File localFile) throws Exception
{
if (localFile == null)
{
return -1;
}
return this.upload(localFile, localFile.getName());
}
/**
* 上传文件至FTP服务器,保持原文件名
*
* @throws java.lang.Exception
* @return -1 文件不存在或不能读取; >0 成功上传,返回文件的大小
* @param localFilePath
* 待上传的本地文件路径
*/
public long upload(String localFilePath) throws Exception
{
return this.upload(new File(localFilePath));
}
/**
* 上传文件至FTP服务器,并重命名文件
*
* @throws java.lang.Exception
* @return -1 文件不存在或不能读取; >0 成功上传,返回文件的大小
* @param localFilePath
* 待上传的本地文件路径
* @param rename
* 远程文件重命名
*/
public long upload(String localFilePath, String rename) throws Exception
{
return this.upload(new File(localFilePath), rename);
}
/**
* 上传文件至FTP服务器,并重命名文件
*
* @throws java.lang.Exception
* @return -1 文件不存在或不能读取; >0 成功上传,返回文件的大小
* @param localFile
* 待上传的本地文件
* @param rename
* 远程文件重命名
*/
public long upload(File localFile, String rename) throws Exception
{
if (localFile == null || !localFile.exists() || !localFile.canRead())
{
return -1;
}
long fileSize = localFile.length();
try
{
if (localFile.isDirectory())
{
ftpClient.sendServer("XMKD " + rename + "\r\n");
ftpClient.readServerResponse();
File[] subFiles = localFile.listFiles();
ftpClient.cd(rename);
try
{
for (int i = 0; i < subFiles.length; i++)
{
fileSize += upload(subFiles[i]);
}
}
finally
{
ftpClient.cdUp();
}
}
else
{
this.writeFileToServer(localFile, rename);
}
return fileSize;
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
/**
* 从ftp下载文件到本地
*
* @throws java.lang.Exception
* @return
* @param localFilePath
* 本地生成的文件名
* @param remoteFilePath
* 服务器上的文件名
*/
public long download(String remoteFilePath, String localFilePath) throws Exception
{
long result = 0;
TelnetInputStream tis = null;
RandomAccessFile raf = null;
DataInputStream puts = null;
try
{
tis = ftpClient.get(remoteFilePath);
raf = new RandomAccessFile(new File(localFilePath), "rw");
raf.seek(0);
int ch;
puts = new DataInputStream(tis);
while ((ch = puts.read()) >= 0)
{
raf.write(ch);
}
}
catch (Exception ex)
{
logger.error("Downloading file failure! Detail:", ex);
throw new Exception(ex);
}
finally
{
try
{
puts.close();
if (tis != null)
{
tis.close();
}
if (raf != null)
{
raf.close();
}
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
return result;
}
/**
* 设置FTP服务器的当前路径<br>
* 可以是绝对路径,也可以是相对路径
* @since
* @param dirPath
* 服务器文件夹路径,空代表ftp根目录
* @throws IOException
*/
public void cd(String dirPath) throws Exception
{
try
{
// path:FTP服务器上的路径,是ftp服务器下主目录的子目录
if (dirPath != null && dirPath.length() > 0)
{
ftpClient.cd(dirPath);
}
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
/**
* 在服务器上创建指定路径的目录,并转到此目录
* @since
* @param dirPath
* @throws Exception
*/
public void mkd(String dirPath) throws Exception
{
try
{
if (dirPath != null && dirPath.length() > 0)
{
StringTokenizer st = new StringTokenizer(dirPath.replaceAll("\\\\", "/"), "/");
String dirName = "";
while (st.hasMoreElements())
{
dirName = (String) st.nextElement();
ftpClient.sendServer("XMKD " + dirName + "\r\n");
ftpClient.readServerResponse();
ftpClient.cd(dirName);
}
}
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
/**
* 删除FTP服务器目录
* @since
* @param directory
* @throws Exception
*/
public void rmd(String directory) throws Exception
{
try
{
if (directory != null && directory.length() > 0)
{
ftpClient.cd(directory);
try
{
this.cld();
}
finally
{
ftpClient.cdUp();
}
ftpClient.sendServer("XRMD " + directory + "\r\n");
ftpClient.readServerResponse();
}
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
/**
* 清空当前目录
* @since
* @throws Exception
*/
public void cld() throws Exception
{
// 删除文件
for (Iterator<String> it = getFileList().iterator(); it.hasNext();)
{
this.delf(it.next());
}
// 删除文件夹
for (Iterator<String> it = getDirList().iterator(); it.hasNext();)
{
this.rmd(it.next());
}
}
/**
* 删除FTP服务器文件
* @since
* @param filePath
* @throws Exception
*/
public void delf(String filePath) throws Exception
{
try
{
if (filePath != null && filePath.length() > 0)
{
ftpClient.sendServer("DELE " + filePath + "\r\n");
ftpClient.readServerResponse();
}
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
/**
* 以指定文件名,将本地文件写到FTP服务器
* @since
* @param localFile
* 待上传的本地文件
* @param fileName
* 写入远程FTP服务器的文件名
* @throws Exception
*/
private void writeFileToServer(File localFile, String fileName) throws Exception {
TelnetOutputStream tos = null;
FileInputStream fis = new FileInputStream(localFile);
try {
tos = ftpClient.put(fileName);
byte[] bytes = new byte[102400];
int c;
while ((c = fis.read(bytes)) != -1) {
tos.flush();
tos.write(bytes, 0, c);
}
} catch (Exception ex) {
throw new Exception(ex);
} finally {
if (fis != null) {
fis.close();
}
if (tos != null) {
tos.flush();
tos.close();
}
}
}
/**
* 取得FTP上某个目录下的所有文件名列表
*
* @since
* @return
* @throws Exception
*/
public List<String> getFileList() throws Exception
{
List<String> fileList = new ArrayList<String>();
BufferedReader br = null;
try
{
String fileItem;
br = new BufferedReader(new InputStreamReader(ftpClient.list()));
while ((fileItem = br.readLine()) != null)
{
if (fileItem.startsWith("-") && !fileItem.endsWith(".") && !fileItem.endsWith(".."))
{
fileList.add(parseFileName(fileItem));
}
}
}
catch (Exception ex)
{
logger.error("Failure to get directory list from ftp server!", ex);
throw new Exception(ex);
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
}
return fileList;
}
/**
* 取得FTP上某个目录下的所有子文件夹名列表
*
* @since
* @return
* @throws Exception
*/
public List<String> getDirList() throws Exception
{
List<String> dirList = new ArrayList<String>();
BufferedReader br = null;
try
{
String fileItem;
br = new BufferedReader(new InputStreamReader(ftpClient.list()));
while ((fileItem = br.readLine()) != null)
{
if (fileItem.startsWith("d") && !fileItem.endsWith(".") && !fileItem.endsWith(".."))
{
dirList.add(parseFileName(fileItem));
}
}
}
catch (Exception ex)
{
logger.info("Failure to get directory list from ftp server!", ex);
throw new Exception(ex);
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
}
return dirList;
}
/**
* 从文件信息中解析出文件(文件夹)名
*
* @since
* @param fileItem
* @return
* @throws Exception
*/
private String parseFileName(String fileItem) throws Exception
{
StringTokenizer st = new StringTokenizer(fileItem);
int index = 0;
while (st.hasMoreTokens())
{
if (index < 8)
{
st.nextToken();
}
else
{
return st.nextToken("").trim();
}
index++;
}
return null;
}
/**
* 上传下载测试
*
* @since
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
FtpUtil ftp = new FtpUtil();
try
{
// 连接ftp服务器
ftp.connect("xx.xx.xx.xx", "ftp", "ftp");
// 上传文件
ftp.cd("/home/sasftp");
// ftp.cld();
// long fileSize = ftp.upload("D:/视频",
// "xufeitewwst");
// if (fileSize == -1) {
// logger.info("Uploading file failure! Because file do not exists!");
// } else if (fileSize == -2) {
// logger.info("Uploading file failure! Because file is empty!");
// } else {
// logger.info("Uploading file success! File size: " + fileSize);
// }
// 取得bbbbbb文件夹下的所有文件列表,并下载到本地保存
List<String> list = new ArrayList<String>();
list.add("czybxw.txt");
list.add("zjyb.txt");
for (int i = 0; i < list.size(); i++)
{
String fileName = (String) list.get(i);
System.out.println(fileName);
ftp.download(fileName, "E:\\text" + File.separator + fileName);
}
}
catch (Exception ex)
{
logger.error("Uploading file failure! Detail:", ex);
}
finally
{
ftp.disConnect();
}
}
}
Java-FtpUtil工具类的更多相关文章
- Java Properties工具类详解
1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...
- Java json工具类,jackson工具类,ObjectMapper工具类
Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...
- Java日期工具类,Java时间工具类,Java时间格式化
Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...
- Java并发工具类 - CountDownLatch
Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...
- MinerUtil.java 爬虫工具类
MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...
- MinerDB.java 数据库工具类
MinerDB.java 数据库工具类 package com.iteye.injavawetrust.miner; import java.sql.Connection; import java.s ...
- 小记Java时间工具类
小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...
- Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie
Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...
- UrlUtils工具类,Java URL工具类,Java URL链接工具类
UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...
- java日期工具类DateUtil-续一
上篇文章中,我为大家分享了下DateUtil第一版源码,但就如同文章中所说,我发现了还存在不完善的地方,所以我又做了优化和扩展. 更新日志: 1.修正当字符串日期风格为MM-dd或yyyy-MM时,若 ...
随机推荐
- 数据检索grep
linux操作中,总是会输出很多的内容.但是有些内容并不是我们重点关注的,所以为了看起来方便,也为了提升效率,就将不需要的内容过滤掉. 只输出想要的东西. grep: 用于搜索 模式参数(给定的字符 ...
- Spring系列四:Bean Scopes作用域
等闲识得东风面,万紫千红总是春. 概述 在Spring框架中,我们可以在六个内置的spring bean作用域中创建bean,还可以定义bean范围.在这六个范围中,只有在使用支持Web的applic ...
- Python学习笔记:运算符
逻辑运算符: + 加 - 减 * 乘 / 除 % 取模-返回除法的余数 ** 幂-返回x的y次方 // 整除 比较运算符: == 等于-比较对象是否相等 != ...
- T100——不绑定数据表字段,做查询条件(待改进)
此例子使用的方法有待改善,不是很好的方法. 范例:aimm200: 作用:查询时默认不显示无效料件:新增参数控制查询是否显示无效料件.(只作用查询,不影响新增.修改等)
- Iview 中 获取 Menu 导航菜单 选中的值
期望效果: 原来,我用的是脚本来控制,然后........,再然后,我再去仔细看官方文档的时候,才发现,Menu组件 有那么两个事件,on-select 和 on-open-change ,好气啊,之 ...
- X86逆向15:OD脚本的编写技巧
本章节我们将学习OD脚本的使用与编写技巧,脚本有啥用呢?脚本的用处非常的大,比如我们要对按钮事件进行批量下断点,此时使用自动化脚本将大大减小我们的工作量,再比如有些比较简单的压缩壳需要脱壳,此时我们也 ...
- 怎样终止(杀掉) Linux 中的进程?
使用 kill -9 进程号 命令, 可是强行终止该进程. 如果使用直接使用 kill 进程号 命令, 则会让进程 "自行了断" . 因此, 一般是 kill -9 进程号 用得较 ...
- 【转】js小数转百分比
转自:js小数和百分数的转换 function toPercent(point){ var str=Number(point*100).toFixed(1); str+="%"; ...
- 你不知道的css各类布局(五)之em布局、rem布局
em布局/rem布局 em和rem的区别 在了解弹性布局前我们需要先知道em和rem rem:font size of the root element,rem是相对于根元素<html>来 ...
- Oracle update 多字段更新
一次性update多个字段 以student表为例: -- 创建学生表 create table student ( id number, name varchar2(40), age number, ...