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. VMware虚拟机无法识别U盘解决方案

    1. 本机情况: Win7操作系统,VMware虚拟机,虚拟机版本:VMware 7.1,安装Ubuntu10.10,现要求在主机上插入U盘,在虚拟机中显示.   2. 遇到问题: U盘只在Win7主 ...

  2. Android那些事儿之LBS定位,实践测试lbs

    最近一朋友让我了解下安卓LBS获取位置信息,于是动手实践了一把.搜了一圈发现有篇博文可以参考:Android那些事儿之LBS定位,但是原文作者没有提供源码下载,于是动手实现了,现记录下来备忘,代码附在 ...

  3. nginx+lua_nginx+GraphicsMagick生成实时缩略图

    暂做笔记,带后续验证通过后,再补充 1.2.3 步. 一.安装 lua 首先确认是否安装 readline yum -y install readline-devel ncurses-devel 进入 ...

  4. textView字体颜色根据不同状态显示不同颜色

    XML file saved at res/color/button_text.xml: <?xml version="1.0" encoding="utf-8&q ...

  5. ruby -- 进阶学习(十三)解说ckeditor在production环境下如何完整显示

    将ROR项目从development环境改为production环境时,运行rake assets:precompile后, ckeditor的界面就无法完整显示?! @_@?? 出现 ActionC ...

  6. JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)

    编写高质量JS代码的68个有效方法(一) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...

  7. Cool!12幅由小图片组成的创意图像重组作品

    这里分享15幅创意插图作品,这些创意插图作品都是有成千上万的小图片组成的,很多创意广告会采用这个形式的设计.下面这组创意作品的作者是 Charis Tsevis,来自希腊的视觉设计师,擅长图像重组的创 ...

  8. [编辑器]走上atom之路1

    祝大家新年快乐 我就是来卖个萌,逃- 正文 我最开始用atom是因为它看起来比较酷,我工作中主力还是使用pycharm,毕竟atom只是一个编辑器.我一 般只是用atom来写Markdown的文件.随 ...

  9. Direct3D11学习:(八)Effects介绍

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Effects框架是一组用于管理着色器程序和渲染状态的工具代码.例如,你可能会使用不同的effect绘制水.云. ...

  10. Python函数解析

    对于Python的函数,我们需要记住的是: 1. 函数的默认返回值是None. 2. python是一个自上而下逐行解释并执行的语言.因此,函数的定义必须在函数被调用之前.同名的函数,后定义的会覆盖前 ...