您的位置:首页 > 编程语言 > PHP开发

FTP-上传下载工具类

2017-07-18 19:20 393 查看
package com.xxx.patchgen.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

/**
* ftp上传下载工具类
*/
public class FtpUtils {

private static FTPClient ftpClient = new FTPClient();

private static String encoding = "GBK";

/**
* Description: 向FTP服务器上传文件
*
* @Version1.0
* @param url
*            FTP服务器hostname
* @param port
*            FTP服务器端口
* @param username
*            FTP登录账号
* @param password
*            FTP登录密码
* @param remoteFile
*            上传到FTP服务器上的目录及文件名,例:"/路径/file2.txt"
* @param localFile
*            需要上传的本地文件的路径,例:"E:\\file.txt"
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url, int port, String username, String password,
String remoteFile, String localFile) throws Exception{
boolean result = false;
try {
FileInputStream input = new FileInputStream(new File(localFile));
int reply;
ftpClient.connect(url, port);// 连接FTP服务器
// 登录
ftpClient.login(username, password);
ftpClient.setControlEncoding(encoding);
// 检验是否连接成功
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
System.out.println(ftpClient.getReplyString());
ftpClient.disconnect();
throw new Exception("ftp 连接失败, " + url + ":" + port + ftpClient.getReplyString());
}
ftpClient.changeWorkingDirectory("/");

int lastFirst = remoteFile.lastIndexOf('/');
String path = remoteFile.substring(0, lastFirst);
String fileName = remoteFile.substring(lastFirst + 1);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 转移工作目录至指定目录下
boolean change = ftpClient.changeWorkingDirectory(new String(path.getBytes(encoding), "iso-8859-1"));
// 没有目录
if (!change) {
boolean isMade = Mkdirs(ftpClient, path);
if (!isMade) {
throw new Exception("ftp创建目录失败, " + ftpClient.getReplyString());
}
}
result = ftpClient.storeFile(new String(fileName.getBytes(encoding), "iso-8859-1"), input);
if (!result) {
throw new Exception("上传ftp文件失败," + ftpClient.getReplyString());
}
input.close();
ftpClient.logout();
} catch (Exception e) {
throw e;
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return result;
}

/**
* Description: 从FTP服务器下载文件
*
* @Version1.0
* @param url
*            FTP服务器hostname
* @param port
*            FTP服务器端口
* @param username
*            FTP登录账号
* @param password
*            FTP登录密码
* @param remoteFile
*            FTP服务器上的文件路径,例:"/file.zip"
* @param localFile
*
4000
要下载到本地的目录及文件名,例:"D:\\file.zip"
* @return 成功返回true,否则返回false
*/
public static boolean downFile(String url, int port, String username, String password, String remoteFile,
String localFile) throws Exception{
boolean result = false;
try {
int reply;
ftpClient.setControlEncoding(encoding);
ftpClient.connect(url, port);
ftpClient.login(username, password);// 登录
// 设置文件传输类型为二进制
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 获取ftp登录应答代码
reply = ftpClient.getReplyCode();
// 验证是否登陆成功
if (!FTPReply.isPositiveCompletion(reply)) {
System.out.println(ftpClient.getReplyString());
ftpClient.disconnect();
throw new Exception("ftp 连接失败, " + url + ":" + port + ftpClient.getReplyString());
}
int lastFirst = localFile.lastIndexOf(File.separator);
String path = localFile.substring(0, lastFirst);
File localPath = new File(path);
if (!localPath.exists()) {// 路径不存在则创建本地目录
localPath.mkdirs();
}
OutputStream out = new FileOutputStream(localFile);
result = ftpClient.retrieveFile(remoteFile, out);
if (!result) {
throw new Exception("下载ftp文件失败," + ftpClient.getReplyString());
}
out.close();
ftpClient.logout();
} catch (Exception e) {
throw e;
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return result;
}

/**
* 创建路径,并切换到该路径下
*
* @param ftpClient
* @param path
*            路径
* @return 成功返回true,否则返回false
*/
public static Boolean Mkdirs(FTPClient ftpClient, String path) throws Exception{
Boolean success = false;
String[] subDirs = path.split("/");
boolean tmpMkdirs = false;
ftpClient.setControlEncoding(encoding);
for (String subDir : subDirs) {
if (subDir == null || subDir.length() == 0) {
continue;
}
String strSubDir = new String(subDir.getBytes(encoding), "iso-8859-1");
boolean isChange = ftpClient.changeWorkingDirectory(strSubDir);
// 已有目录跳过
if (isChange) {
continue;
}
tmpMkdirs = ftpClient.makeDirectory(strSubDir);
boolean tmpDoCommand = ftpClient.sendSiteCommand("chmod 755 " + strSubDir);
ftpClient.changeWorkingDirectory(strSubDir);
success = tmpDoCommand || tmpMkdirs;
if (!success) {
throw new Exception("FTP服务器端尝试创建目录失败," + ftpClient.getReplyString());
}
}
return success;
}

@SuppressWarnings("unused")
public static void main(String[] args) {
String url = "192.168.12.182"; //sftp://192.168.12.182
int port = 22;
String username = "abc";
String password = "abc@1";
String remoteFile = "/aaa/bbb/vvv-new/cc.war";
String localFile = "F:/ftp-test/cc.war";
try {
downFile(url, port, username, password, remoteFile, localFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: