FTPUtil

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply; public class FTPUtil {
// public static final String platform_charset = "utf-8";
public static final String platform_charset = "gb2312"; public static boolean uploadFile(String url, int port, String username,
String password, String path, String filename, InputStream input)
throws IOException {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
path = path.replaceAll("//", "/");
if (path.startsWith("/"))
path = path.substring(1);
if (filename.startsWith("/"))
filename = filename.substring(1); String dir = new String(path.getBytes(platform_charset),"iso-8859-1");
String destName = new String(filename.getBytes(platform_charset),"iso-8859-1");
buildList(ftp, dir); ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory(dir);
boolean flag = ftp.storeFile(destName, input);
input.close();
ftp.logout(); if (flag)
success = true;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return success;
} public static void sendFile(String url, int port,
String username, String password, String path, String filename,
InputStream input) throws IOException {
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return;
} if (path.startsWith("/"))
path = path.substring(1);
if (filename.startsWith("/"))
filename = filename.substring(1); String dir = new String(path.getBytes(platform_charset),
"iso-8859-1");
String destName = new String(filename.getBytes(platform_charset),
"iso-8859-1");
buildList(ftp, dir); ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory(dir); int n = -1;
long trans = 0;
int bufferSize = ftp.getBufferSize();
byte[] buffer = new byte[bufferSize];
OutputStream outputstream = ftp.storeFileStream(destName);
while ((n = input.read(buffer)) != -1) {
outputstream.write(buffer);
trans += n;
}
input.close();
ftp.logout(); } finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
} } public static void buildList(FTPClient ftpclient, String pathList)
throws IOException {
StringTokenizer s = new StringTokenizer(pathList, "/"); String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement(); if (pathName.startsWith("/"))
pathName = pathName.substring(1);
ftpclient.makeDirectory(pathName);
}
} public static boolean downFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) throws IOException {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
} remotePath = new String(remotePath.getBytes(platform_charset),
"iso-8859-1");
ftp.changeWorkingDirectory(remotePath);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
String name = ff.getName();
name = new String(name.getBytes("iso-8859-1"), platform_charset);
if (name.equals(fileName)) {
File localPathFile = new File(localPath);
//判断路径是否存在 ,不存在则创建
if(!localPathFile.exists()){
localPathFile.mkdirs();
}
File localFile = new File(localPath + "/" + name); OutputStream os = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), os);
os.close();
}
}
ftp.logout();
success = true;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
} public static List<FTPFile> list(String url, int port, String username,
String password, String remotePath) throws Exception {
FTPClient ftp = new FTPClient();
List<FTPFile> list = new ArrayList<FTPFile>();
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return list;
}
remotePath = new String(remotePath.getBytes(platform_charset),
"iso-8859-1");
ftp.changeWorkingDirectory(remotePath);
FTPFile[] files = ftp.listFiles();
for (FTPFile f : files) {
String name = f.getName();
if (name.equals(".") || name.equals("..")
|| name.equals("Thumbs.db"))
continue; list.add(f);
}
return list;
} catch (IOException e) {
e.printStackTrace();
return list;
} finally {
ftp.logout();
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
} public static boolean hasSubFolder(FTPClient ftp, String parent)
throws IOException {
FTPFile[] files = ftp.listFiles(parent);
for (FTPFile f : files) {
String name = f.getName();
if (name.equals(".") || name.equals("..")
|| name.equals("Thumbs.db"))
continue; if (f.isDirectory()) {
return true;
} else {
continue;
}
}
return false; } public static boolean deleteFile(String url, int port, String username,
String password, String remotePath) throws IOException {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
} remotePath = new String(remotePath.getBytes(platform_charset),
"iso-8859-1");
if (remotePath.startsWith("/"))
remotePath = remotePath.substring(1); ftp.changeWorkingDirectory(remotePath);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
String name = ff.getName();
ftp.deleteFile(name);
}
ftp.changeToParentDirectory();
int p = remotePath.lastIndexOf("/");
String folderName = remotePath.substring(p + 1);
ftp.removeDirectory(folderName); ftp.logout();
success = true;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
} }

java ftp的更多相关文章

  1. 用edtftpj实现Java FTP客户端工具

    edtftpj是一个java FTP工具包,使用非常方便,感觉比Apache的好用,但Apache更灵活.edtftpj有多种版本,分别是java..net和js版本.对于Java版的有一个免费版本. ...

  2. 关于Java FTP SFTP的相关实际问题

    第一个: java ftp使用的是Apache common-net,但是FTP服务侧提供的FTP服务器只支持SFTP,结果报 java.net.ConnectException: Connectio ...

  3. Java ftp上传文件方法效率对比

    Java ftp上传文件方法效率对比 一.功能简介: txt文件采用ftp方式从windows传输到Linux系统: 二.ftp实现方法 (1)方法一:采用二进制流传输,设置缓冲区,速度快,50M的t ...

  4. (转)【Java FTP及FTP服务器搭建】

    转至 http://blog.csdn.net/studyvcmfc/article/details/8147052 目录(?)[+] -[Java FTP及FTP服务器搭建] 一:本文采用apach ...

  5. Java FTP 基本操作

    最近工作中用到了 FTP 相关的操作,所以借此机会了解了下具体内容. FTP基础 关于 FTP 基础推荐阅读<使用 Socket 通信实现 FTP 客户端程序>,其中需要特别注意的是主动模 ...

  6. Java FTP客户端开源类库 edtFTPj

    edtFTPj/Free是免费的流行的Java FTP库,全球公司依靠edtFTPj /Free 为它们的Java应用程序添加FTP客户端功能. (收费的支持SFTP.FTPS的edtFTPj/PRO ...

  7. docker 部署vsftpd服务、验证及java ftp操作工具类

    docker部署vsftpd服务 新建ftp文件存储目录/home/ftp cd /home mkdir ftp 创建一个组,用于存放ftp用户 groupadd ftpgroups 创建ftp用户, ...

  8. Java ftp断点续传

    FtpTransFile类 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...

  9. Java ftp 上传文件和下载文件

    今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...

随机推荐

  1. worksteal thread pool

    worksteal的场景 对于一个线程池,每个线程有一个队列,想象这种场景,有的线程队列中有大量的比较耗时的任务堆积,而有的线程队列却是空的,现象就是有的线程处于饥饿状态,而有的线程处于消化不良的状态 ...

  2. Linux下php5.3编译oracle客户端

    因项目需要在linux下进行php5.3的oracle客户端编译,简要介绍一下步骤及走过的弯路. 1.下载Oracle客户端程序包,其中包含OCI.OCCI和JDBC-OCI等相关文件. 1.1下载文 ...

  3. 关于MySQL redo log,挖些坑,慢慢填

    1. 为什么可以设置为多个redo log ? (innodb_log_files_in_group,默认值和推荐值都是2,我们线上设的统一为4): 2. 什么条件下会触发刷脏?除了master_th ...

  4. JAVA IO NIO

    http://www.cnblogs.com/handsome1013/p/4882862.html http://www.cnblogs.com/dolphin0520/ http://www.cn ...

  5. WPF常用控件样式集锦

    1.不规则形状按钮(通过更改path实现) <Style x:Key="ButtonStyleForPath" TargetType="{x:Type Button ...

  6. C# WinForm程序打印条码 Code39码1

    做WinForm程序需要打印条码,为了偷懒不想自己写生成条码的程序在网上下载一个标准的39码的字体,在程序里面换上这个条码字体即可打印条码了. 最重要的一点作为记录: 如果想把“123456789”转 ...

  7. UNIX环境高级编程笔记之高级I/O

    本章说明了很多高级I/O功能: 非阻塞I/O——发一个I/O操作,不使其阻塞,记录锁,STREAMS机制 I/O多路转接——select和poll函数 readv和writev函数,以及存储映射I/O ...

  8. 使用Merge Into 语句实现 Insert/Update

    网址: http://www.eygle.com/digest/2009/01/merge_into_insertupdate.html 动机: 想在Oracle中用一条SQL语句直接进行Insert ...

  9. `cocos2dx非完整` 开始自己的FW模块

    上一篇的文章中说到了一些个人习惯的东西以及一些简单的项目配置,这一篇文章我们来进一步完善一些东西.首先,打开编译以后的客户端执行,会看到一大堆的fileutils加载luac文件的提示,在终端显示一大 ...

  10. Linux 时钟与计时器

    对 Linux 系统来说,时钟和计时器是两个十分重要的概念.时钟反应的是绝对时间,也可认为是实时时间.计时器反应的则是相对时间,即相对于系统启动后的计时.操作系统内核需要管理运行时间(uptime)和 ...