java ftp
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的更多相关文章
- 用edtftpj实现Java FTP客户端工具
edtftpj是一个java FTP工具包,使用非常方便,感觉比Apache的好用,但Apache更灵活.edtftpj有多种版本,分别是java..net和js版本.对于Java版的有一个免费版本. ...
- 关于Java FTP SFTP的相关实际问题
第一个: java ftp使用的是Apache common-net,但是FTP服务侧提供的FTP服务器只支持SFTP,结果报 java.net.ConnectException: Connectio ...
- Java ftp上传文件方法效率对比
Java ftp上传文件方法效率对比 一.功能简介: txt文件采用ftp方式从windows传输到Linux系统: 二.ftp实现方法 (1)方法一:采用二进制流传输,设置缓冲区,速度快,50M的t ...
- (转)【Java FTP及FTP服务器搭建】
转至 http://blog.csdn.net/studyvcmfc/article/details/8147052 目录(?)[+] -[Java FTP及FTP服务器搭建] 一:本文采用apach ...
- Java FTP 基本操作
最近工作中用到了 FTP 相关的操作,所以借此机会了解了下具体内容. FTP基础 关于 FTP 基础推荐阅读<使用 Socket 通信实现 FTP 客户端程序>,其中需要特别注意的是主动模 ...
- Java FTP客户端开源类库 edtFTPj
edtFTPj/Free是免费的流行的Java FTP库,全球公司依靠edtFTPj /Free 为它们的Java应用程序添加FTP客户端功能. (收费的支持SFTP.FTPS的edtFTPj/PRO ...
- docker 部署vsftpd服务、验证及java ftp操作工具类
docker部署vsftpd服务 新建ftp文件存储目录/home/ftp cd /home mkdir ftp 创建一个组,用于存放ftp用户 groupadd ftpgroups 创建ftp用户, ...
- Java ftp断点续传
FtpTransFile类 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- Java ftp 上传文件和下载文件
今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...
随机推荐
- 一张Windows版本发展图——纪念XP服役13你年
88年的人,接触PC十几年.第一次真正开始学习PC是在小学四年级的电脑兴趣班上,那时候好多事情还历历在目.那些年,神秘的DOS,向里面输入一些自己都不懂得命令,出现的场景让一个少年内心砰砰直跳.一个& ...
- sql:pivot unpivot
pivot 行转列 unpivot 列转行 源码跑步起来 这是能跑起来的 源码转自 http://www.cnblogs.com/zhangzt/archive/2010/07/29/178782 ...
- [PaPaPa][需求说明书][V0.3]
PaPaPa软件需求说明书V0.3 前 言 不好意思,本文是没有前言的. 别说是前言了,其实关于界面的内容我也是不打算写!! 因为我知道你们想要的界面是这样的: 再不济也应该是这样的: 但是我 ...
- 使用SQL联合查询来构建临时vo对象的应用
联合查询: 表1: team球队表 表2:schedule 赛程表 需要数据: 球队名称.主队ID.主队名称.客队ID.客队名称.胜负情况 方法1. Object数组取出列和数值 import jav ...
- Ext.js中的tip事件实际使用
Ext.onReady(function () { // Init the singleton. Any tag-based quick tips will start working. Ext.ti ...
- How ADB works
ADB (Android Debug Bridge): How it works? 2012.2.6 early draft Tetsuyuki Kobayashi What is ADB? If y ...
- MeshCombineUtility.cs method `GetTriangleStrip' of type `UnityEngine.Mesh' could be found
1) Assets/Standard Assets/Scripts/MeshCombineUtility.cs(27,74): error CS1061: Type `UnityEngine.Mesh ...
- tcp为什要三次握手
准备知识: 单工:信息只能单向传递.发送-->接收,单向,不能返回响应. 双工:指的是信息可双向发送. 全双工:信息可同时双向传递. 半双工:不能同时,单行道,一边传输完了,另一边才能发起传输. ...
- 转iOS中delegate、protocol的关系
iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...
- Linux bash - 常用操作命令
一.终端基础 本文摘录一些本人在学习Linux(CentOS 6.6) bash命令,并且会不定期保持更新. 在此先介绍一下Linux shell终端的常规命令输入格式,如下图: 上图中root是用户 ...