首页 技术 正文
技术 2022年11月15日
0 收藏 957 点赞 2,267 浏览 5598 个字

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;
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,103
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,579
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,427
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,199
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,834
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,917