package com.sinosoft.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.zving.framework.Config;
import com.zving.framework.collection.Mapx;
/**
* ftp工具类 版权:(C) 版权所有 2015-2018 中科软科技股份有限公司 <功能名称> <详细描述>
*
* @author gongsf
* @version 1.0
* @创建时间 2016年5月25日下午12:20:33
* @修改时间 2016年5月25日
*/
public class FtpUtil {
/**
* 获取ftp配置文件信息 〈简述〉 〈详细描述〉
*
* @author gongsf
* @return Map key-value集合
* @throws DocumentException 异常
*/
@SuppressWarnings("unchecked")
public static Mapx<String, String> getFtpInfo()
throws DocumentException {
SAXReader reader = new SAXReader();
// E:/work/dalian/dlzx/UI/ftpConfig.xml
File file = new File(Config.getContextRealPath() + "ftpConfig.xml");
Document document = reader.read(file);
Element root = document.getRootElement();
@SuppressWarnings("rawtypes")
Mapx<String, String> ftpMap = new Mapx();
ftpMap.put("serverIp", root.elementText("serverIp"));
ftpMap.put("userName", root.elementText("userName"));
ftpMap.put("passWord", root.elementText("passWord"));
ftpMap.put("port", root.elementText("port"));
return ftpMap;
}
/**
* 获取服务Ip 〈简述〉 〈详细描述〉
*
* @author gongsf
* @return serverIp ip地址
* @throws DocumentException 异常
*/
public static String getServerIp()
throws DocumentException {
Mapx<String, String> ftpMap = getFtpInfo();
return ftpMap.get("serverIp");
}
/**
* 获取服务userName 〈简述〉 〈详细描述〉
*
* @author gongsf
* @return userName 服务名
* @throws DocumentException 异常
*/
public static String getUserName()
throws DocumentException {
Mapx<String, String> ftpMap = getFtpInfo();
return ftpMap.get("userName");
}
/**
* 获取服务passWord 〈简述〉 〈详细描述〉
*
* @author gongsf
* @return passWord 密码
* @throws DocumentException 异常
*/
public static String getPassWord()
throws DocumentException {
Mapx<String, String> ftpMap = getFtpInfo();
return ftpMap.get("passWord");
}
/**
* 获取服务port 〈简述〉 〈详细描述〉
*
* @author gongsf
* @return port 端口
* @throws DocumentException 异常
*/
public static String getPort()
throws DocumentException {
Mapx<String, String> ftpMap = getFtpInfo();
return ftpMap.get("port");
}
/**
* 获取服务地址 〈简述〉 〈详细描述〉
*
* @author gongsf
* @return port 端口
* @throws DocumentException 异常
*/
public static String getFtpAddress()
throws DocumentException {
return "ftp://" + getServerIp() + "/";
}
/**
* FTP上传文件 〈简述〉 〈详细描述〉
*
* @author gongsf
* @param localUrl 本地文件url地址
* @param remoteUrl 服务器文件url地址
* @param newFileName 上传至服务器的文件名
* @throws DocumentException 异常
*/
public static void ftpUpload(String localUrl, String remoteUrl, String newFileName)
throws DocumentException {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
try {
ftpClient.connect(getServerIp());
ftpClient.login(getUserName(), getPassWord());
// File srcFile = new File("e:/1.txt");
File srcFile = new File(localUrl);
fis = new FileInputStream(srcFile);
// 设置上传目录
// ftpClient.changeWorkingDirectory("zdy/upload/courseHour");
ftpClient.changeWorkingDirectory(remoteUrl);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// ftpClient.storeFile("yes.txt", fis);
ftpClient.storeFile(newFileName, fis);
System.out.println("*******************ok");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
/**
* ftp下载转化为文件流 〈简述〉 〈详细描述〉
*
* @author gongsf
* @param ftpPath ftp主目录
* @param fileName 文件路径
* @return InputStream 服务器文件流
* @throws DocumentException 异常
*/
public static InputStream ftpDownload(String ftpPath, String fileName)
throws DocumentException {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
InputStream inputStream = null;
try {
ftpClient.connect(getServerIp());
ftpClient.login(getUserName(), getPassWord());
System.out.println("下载,开始读取绝对路径" + ftpPath + "文件!");
try {
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
inputStream = ftpClient.retrieveFileStream(fileName);
} catch (FileNotFoundException e) {
System.out.println("没有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (SocketException e) {
System.out.println("连接FTP失败.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取错误。");
e.printStackTrace();
}
if (inputStream != null) {
System.out.println("读取服务器文件成功");
} else {
System.out.println("InputStream为空,不能读取。");
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return inputStream;
}
/**
* 创建文件目录(支持创建多级目录) 〈简述〉 〈详细描述〉
*
* @author gongsf
* @param remote 远程文件路径
* @return Boolean true/false
* @throws DocumentException 异常
*/
public static Boolean createDir(String remote)
throws DocumentException {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
Boolean b = true;
try {
ftpClient.connect(getServerIp());
ftpClient.login(getUserName(), getPassWord());
String directory = remote.substring(0, remote.lastIndexOf("/") + 1);
if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(
new String(directory.getBytes("GBK"), "iso-8859-1"))) {
// 如果远程目录不存在,则递归创建远程服务器目录
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
while (true) {
String subDirectory = new String(remote.substring(start, end).getBytes("GBK"),
"iso-8859-1");
if (!ftpClient.changeWorkingDirectory(subDirectory)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
} else {
System.out.println("创建目录失败");
b = false;
}
}
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
b = false;
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
b = false;
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return b;
}
public static void main(String[] args)
throws DocumentException {
// ftpUpload("e:/jdk-7u7-windows-i586.exe", "Zdy/upload/course",
// "jdk-7u7-windows-i586.exe");
// ftpDownload("Zdy", "upload/22.txt");
// System.out.println(createDir("/Zdy/upload/111/1111/11111/"));
}
}
**************************************************************后加(创建多级目录)
public boolean createDirectory(String path) {
try {
String[] dirArray = path.split("/");
for (int i = 0; i < dirArray.length; i++) {
if (dirArray[i] != null)
if (!ftp.changeWorkingDirectory(dirArray[i])) {
ftp.makeDirectory(dirArray[i]);
ftp.changeWorkingDirectory(dirArray[i]);
}
}