1.ftp工具类:
/*
* Copyright 2008 Digital Video Networks (Beijing) Co., Ltd. All rights reserved.
*/
package com.dvnchina.itv.advert.utils.ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import com.dvnchina.itv.advert.utils.config.ConfigureProperties;
/**
*
* <p>
* Title:FTP 工具类
* </p>
* <p>
* Description:用于上传或下载文件
* </p>
*
* @author lester
* @version 1.0
* @since 2012-03-05
*/
public class FtpUtils {
private static Logger logger = Logger.getLogger(FtpUtils.class);
private static ConfigureProperties configureProperties = ConfigureProperties
.getInstance();
// 二进制模式 此种类型在传输过程中大小不会发生变化
public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
// ASCII模式 此种类型在传输过程中大小会发生变化
public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
// 盘符分隔符
private static String SEPARATOR = System.getProperty("file.separator");
// 从配置文件中获取FTP的IP
private String ftpIp = null;
// 从配置文件中读取连接FTP超时信息
private Integer timeout = null;
// ftp用户名
private String ftpUserName = null;
// ftp密码
private String ftpPassWord = null;
// ftp端口
private Integer ftpPort = null;
// 显示执行过程开关
private String showOperationCommandKey = null;
private FTPClient ftpClient = null;
/**
* 初始化一些连接参数
*
* @author lester
*/
public FtpUtils() {
ftpClient = new FTPClient();
// 重新获取配置文件中的参数值
ftpIp = configureProperties.get("ftpIp");
ftpUserName = configureProperties.get("ftpUserName");
ftpPassWord = configureProperties.get("ftpPassWord");
try {
ftpPort = Integer.parseInt(configureProperties.get("ftpPort"));
} catch (Exception e) {
logger.error("从配置文件中获取端口时发生异常,请检查配置", e);
}
try {
timeout = Integer.parseInt(configureProperties.get("ftpTimeout")) * 1000;
} catch (Exception e) {
logger.error("从配置文件中获取超时时间时时发生异常,请检查配置", e);
}
//showOperationCommandKey = getPropertiesFromConfiguration("showOperationCommand");
if (StringUtils.isNotBlank(showOperationCommandKey)
&& showOperationCommandKey.equalsIgnoreCase("yes")) {
// 设置将过程中使用到的命令输出到控制台
/*this.ftpClient.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));*/
}
}
/**
* 构造方法2
* @author lester
* @param ip
* @param port
* @param userName
* @param passWord
* @param timeOut
*/
public FtpUtils(String ip,int port, String userName,String passWord ,int timeOut) {
ftpClient = new FTPClient();
ftpIp = ip;
ftpUserName = userName;
ftpPassWord = passWord;
ftpPort = port;
timeout = timeOut;
}
/**
* 从配置文件中获取相应属性值,适用于参数需要经常修改的情况
*
* @param key
* 待获取属性值的key
* @author lester
*/
@SuppressWarnings("unused")
private String getPropertiesFromConfiguration(String key) {
String localConfigPath = System.getProperty("user.dir") + SEPARATOR
+ "webapp" + SEPARATOR + "WEB-INF" + SEPARATOR + "classes"
+ SEPARATOR + "configuration.properties";
File f = new File(localConfigPath);
Properties properties = null;
FileInputStream fileInputStream = null;
String returnValue = null;
if (StringUtils.isNotBlank(key)) {
try {
fileInputStream = new FileInputStream(f);
properties = new Properties();
properties.load(fileInputStream);
} catch (IOException e) {
logger.error("读取本地" + localConfigPath + "文件失败", e);
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
logger.error("读取配置文件时,关闭输出流出现异常", e);
}
}
returnValue = properties.getProperty(key);
} else {
logger.info("传入的参数为空");
}
return returnValue;
}
/**
* 替换路径头尾特殊字符以及分隔符处理
*
* @param path
* @author lester
*/
public String getRegularPath(String path) {
int length;
if (StringUtils.isNotBlank(path)) {
length = path.length();
if (length <= 0)
return path;
char ch = path.charAt(0);
while (ch == '/' || ch == '\\') {
try {
path = path.substring(1, length);
} catch (Exception e) {
logger.error("字符串截取时发生异常", e);
return path;
}
length = path.length();
if (length <= 0)
return path;
try {
ch = path.charAt(0);
} catch (Exception e) {
logger.error("字符串截取时发生异常", e);
return path;
}
}
try {
ch = path.charAt(length - 1);
} catch (Exception e) {
logger.error("字符串截取时发生异常", e);
}
while (ch == '/' || ch == '\\') {
length = path.length();
if (length <= 0) {
return path;
}
try {
path = path.substring(0, length - 1);
ch = path.charAt(length - 2);
} catch (Exception e) {
logger.error("字符串截取时发生异常", e);
return path;
}
}
if (path.contains("\\")) {
path = path.replace("\\", "/");
}
path = path.replaceAll("//", "/");
path = path.replaceAll("///", "/");
path = path.replaceAll("", "/");
path = path.replaceAll("/", "/");
}
return path;
}
/**
* 创建FTP连接,创建连接相关参数从配置文件中获取
*
* @author lester
*/
public boolean connectionFtp() {
boolean flag = true;
int reply;
try {
ftpClient.setControlEncoding("GBK");
ftpClient.connect(ftpIp);
ftpClient.login(ftpUserName, ftpPassWord);
ftpClient.setDefaultPort(ftpPort);
ftpClient.setBufferSize(1024);
ftpClient.setFileType(FtpUtils.BINARY_FILE_TYPE);
ftpClient.setDataTimeout(timeout);
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
flag = false;
logger.warn("FTP 服务拒绝连接");
}
// 设置被动模式
ftpClient.enterLocalPassiveMode();
} catch (Exception e) {
flag = false;
// logger.error("连接FTP时出现异常", e);
logger.info("连接FTP时出现异常,请检查ftp的连接配置");
}
return flag;
}
/**
* 创建FTP连接
*
* @param ftpIp
* FTP ip地址
* @param port
* FTP 端口号
* @param username
* FTP 用户名
* @param password
* FTP 密码
* @param timeout
* FTP 超时时间 单位秒
* @return true- 成功 false-失败
*/
public boolean connectionFtp(String ftpIp, int port, String username,
String password, Integer timeout) {
boolean flag = false;
this.ftpIp = ftpIp;
this.ftpPort = port;
this.ftpUserName = username;
this.ftpPassWord = password;
this.timeout = timeout;
flag = connectionFtp();
if (flag) {
logger.info("FTP连接成功");
} else {
logger.info("FTP连接失败");
}
return flag;
}
/**
* 关闭FTP连接
* @author lester
*/
public void closedFtp() {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
logger.error("关闭FTP连接时出现异常", e);
}
}
}
/**
* 进入到服务器的某个目录下
* @param directory
* @author lester
*/
public boolean changeWorkingDirectory(String directory) {
boolean flag = false;
try {
flag = ftpClient.changeWorkingDirectory(directory);
} catch (IOException e) {
logger.error("切换目录失败", e);
}
return flag;
}
/**
* 创建或者切换目录
* @param ftpDirectory
* 远程ftp路径
* @author lester
*/
public boolean changeAndCreateWorkingDirectory(String ftpDirectory) {
String[] remoteDirectory = null;
boolean createFlag = false;
boolean channelFlag = false;
if (StringUtils.isNotBlank(ftpDirectory)) {
ftpDirectory = getRegularPath(ftpDirectory);
if (ftpDirectory.contains("/")) {
remoteDirectory = ftpDirectory.split("/");
}
try {
if (!ftpClient.changeWorkingDirectory(ftpDirectory)) {
if (remoteDirectory != null) {
for (int i = 0; i < remoteDirectory.length; i++) {
if (!ftpClient
.changeWorkingDirectory(remoteDirectory[i])) {
createFlag = ftpClient
.makeDirectory(remoteDirectory[i]);
channelFlag = ftpClient
.changeWorkingDirectory(remoteDirectory[i]);
}
}
} else {
createFlag = ftpClient.makeDirectory(ftpDirectory);
channelFlag = ftpClient
.changeWorkingDirectory(ftpDirectory);
}
if (createFlag) {
logger.warn("创建目录" + ftpDirectory + "成功");
} else {
logger.warn("创建目录" + ftpDirectory + "失败");
}
} else {
channelFlag = true;
}
} catch (Exception e) {
logger.error("创建或者切换目录" + ftpDirectory + "失败", e);
}
} else {
logger.info("所传入的ftp路径为空");
}
return channelFlag;
}
/**
* 上传单个文件
* @param localFilePath
* 待上传本地文件
* @param remoteDirectoryPath
* 远程目录
* @param remoteFileName
* 上传后的文件名称 若没有则取原有文件名称
* @author lester
*/
public boolean uploadSimpleFile(String localFilePath,
String remoteDirectoryPath, String remoteFileName) {
@SuppressWarnings("unused")
Integer flag = null;
File localFile = null;
FileInputStream fis = null;
boolean uploadFlag = false;
boolean renameFlag = false;
String localFileName = null;
// 1、检查传入参数是否为空
if (StringUtils.isNotBlank(localFilePath)) {
localFile = new File(localFilePath);
if (localFile.isFile()) {
try {
localFileName = localFile.getName();
fis = new FileInputStream(localFile);
if (StringUtils.isNotBlank(remoteDirectoryPath)) {
remoteDirectoryPath = getRegularPath(remoteDirectoryPath);
if (!changeAndCreateWorkingDirectory(remoteDirectoryPath)) {
logger.warn("创建或者切换目录失败,请检查ftp用户访问权限");
}
} else {
logger.info("远程目录为空,文件将被传到用户宿主目中");
}
if (StringUtils.isNotBlank(remoteFileName)) {
uploadFlag = ftpClient.storeFile(remoteFileName
+ ".bak", fis);
if (uploadFlag) {
logger.info(remoteFileName + ".bak文件上传成功");
renameFlag = ftpClient.rename(remoteFileName
+ ".bak", remoteFileName);
if (renameFlag) {
logger.info(remoteFileName + "文件重命名成功");
}
}
} else {
uploadFlag = ftpClient.storeFile(
localFileName + ".bak", fis);
if (uploadFlag) {
renameFlag = ftpClient.rename(localFileName
+ ".bak", localFileName);
if (renameFlag) {
logger.info(localFileName + "文件重命名成功");
}
}
}
ftpClient.changeWorkingDirectory("~/");
} catch (Exception e) {
logger.error("读取本地文件失败!", e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
logger.error("读取本地文件后,关闭IO时发生异常", e);
}
}
}
} else {
logger.info("传入不合法的本地文件地址");
}
} else {
logger.info("传入的本地文件地址为空");
}
return renameFlag;
}
/**
* 上传远程http文件到ftp指定目录中
* @param remoteFileLinkPath 远程文件链接
* @param remoteDirectory 远程FTP目录
* @author lester
*/
public boolean uploadHttpFile(String remoteFileLinkPath,
String remoteDirectory) {
String fileName = null;
URL url = null;
InputStream inputStream = null;
boolean flag = false;
if (StringUtils.isNotBlank(remoteFileLinkPath)
&& (remoteFileLinkPath.startsWith("http://") || remoteFileLinkPath
.startsWith("HTTP://"))) {
try {
url = new URL(remoteFileLinkPath);
inputStream = url.openStream();
} catch (Exception e) {
logger.error("从" + remoteFileLinkPath + "读取文件失败", e);
}
fileName = remoteFileLinkPath.substring(remoteFileLinkPath
.lastIndexOf("/") + 1);
if (StringUtils.isNotBlank(remoteDirectory)) {
remoteDirectory = getRegularPath(remoteDirectory);
if (!changeAndCreateWorkingDirectory(remoteDirectory)) {
logger.info("创建或者切换目录" + remoteDirectory
+ "失败,请检查ftp用户访问权限");
}
} else {
logger.info("远程目录为空,文件将被传到用户宿主目中");
}
if (inputStream != null) {
try {
flag = ftpClient.storeFile(fileName + ".bak", inputStream);
flag = ftpClient.rename(fileName + ".bak", fileName);
} catch (Exception e) {
flag = false;
logger.error("上传文件失败", e);
}
} else {
logger.info("从连接地址" + remoteFileLinkPath + "获取的输入流为空,文件将不予上传");
}
} else {
logger.info("传入的" + remoteFileLinkPath + "地址有误,将不予上传");
}
return flag;
}
/**
* 上传单个文件夹
*
* @param localDirectoryPath
* 本地文件夹地址
* @param localDirectoryRootPath
* 本地文件夹的主参考地址
* @param remoteFilePath 文件要存放的远程FTP路径
* @author lester
*/
public void uploadSimpleFolder(String localDirectoryPath,
String localDirectoryRootPath, String remoteFilePath) {
logger.debug("uploadSimpleFolder");
File localDirectoryPathFile = null;
String remoteFilePathBackup = null;
String localFilePath = null;
File[] localDirectorylistFile = null;
if (StringUtils.isNotBlank(localDirectoryPath)
&& StringUtils.isNotBlank(localDirectoryRootPath)) {
remoteFilePathBackup = remoteFilePath;
localDirectoryPathFile = new File(localDirectoryPath);
if (localDirectoryPath.contains(localDirectoryRootPath)) {
localDirectorylistFile = localDirectoryPathFile.listFiles();
if (localDirectorylistFile != null
&& localDirectorylistFile.length > 0) {
for (int i = 0; i < localDirectorylistFile.length; i++) {
if (localDirectorylistFile[i].isDirectory()) {
uploadSimpleFolder(localDirectorylistFile[i]
.getAbsolutePath(), localDirectoryRootPath,
remoteFilePath);
} else {
localFilePath = localDirectorylistFile[i]
.getAbsolutePath();
remoteFilePathBackup = localDirectorylistFile[i]
.getAbsolutePath();
remoteFilePathBackup = remoteFilePathBackup
.substring(remoteFilePathBackup
.indexOf(localDirectoryRootPath)
+ localDirectoryRootPath.length()
+ 1);
remoteFilePathBackup = getRegularPath(remoteFilePathBackup);
remoteFilePathBackup = remoteFilePathBackup
.substring(0, remoteFilePathBackup
.lastIndexOf("/"));
remoteFilePathBackup = remoteFilePath + "/"
+ getRegularPath(remoteFilePathBackup);
uploadSimpleFile(localFilePath,
remoteFilePathBackup,
localDirectorylistFile[i].getName());
}
}
}
} else {
logger.info("所给文件夹路径有误或为文件路径");
}
} else {
logger.info("所需【上传本地文件夹地址】或【本地文件夹的主参考地址】为空");
}
logger.debug("uploadSimpleFolder");
}
/**
* 删除一个FTP服务器上的目录(如果为空)
*
* @param path
* 目录路径
* @author lester
*/
public boolean removeDirectory(String path) {
boolean flag = false;
if (ftpClient == null) {
connectionFtp();
}
try {
flag = ftpClient.removeDirectory(path);
} catch (Exception e) {
logger.error("删除目录时出现异常,具体目录路径为:" + path, e);
}
return flag;
}
/**
* 删除一个FTP服务器上的目录
* @param path
* 目录路径
* @param isAll
* 是否递归删除所有子目录和文件 true 递归 false 不递归
* @author lester
*/
public boolean removeDirectory(String path, boolean isAll) {
// 遍历子目录和文件
FTPFile[] ftpFileArr = null;
if (StringUtils.isNotBlank(path)) {
logger.info("递归参数为true,将递归删除文件夹中内容");
path = getRegularPath(path);
if (!isAll) {
return removeDirectory(path);
}
try {
ftpFileArr = ftpClient.listFiles(path);
} catch (Exception e) {
logger.error("获取路径下文件列表时出现异常,具体路径为:" + path, e);
}
if (ftpFileArr == null || ftpFileArr.length == 0) {
return removeDirectory(path);
}
for (int i = 0; i < ftpFileArr.length; i++) {
FTPFile ftpFile = ftpFileArr[i];
if (ftpFile != null) {
String name = ftpFile.getName();
if (ftpFile.isDirectory() && !".".equals(name)
&& !"..".equals(name)) {
removeDirectory(path + "/" + name, true);
} else if (ftpFile.isFile() && !".".equals(name)
&& !"..".equals(name)) {
deleteFile(path + "/" + name);
} else if (ftpFile.isSymbolicLink()) {
} else if (ftpFile.isUnknown()) {
}
}
}
} else {
logger.info("传入的待删除路径为空!");
}
return removeDirectory(path);
}
/**
* 删除一个文件
* @param filename 待删除的文件名称
* @author lester
*/
public boolean deleteFile(String filename) {
boolean flag = true;
try {
flag = ftpClient.deleteFile(filename);
if (flag) {
logger.info("删除文件成功!");
} else {
logger.info("删除文件失败!");
}
} catch (IOException ioe) {
logger.error("", ioe);
}
return flag;
}
/**
* 返回远程FTP给定目录下的文件列表--所有的
* @param path FTP 文件目录
* @param resultList 文件夹树下的所有叶子(文件)
* @author lester
* @return
*/
public List<String> getFileList(String path, List<String> resultList) {
FTPFile[] ftpFiles = null;
List<String> list = null;
try {
if (StringUtils.isNotBlank(path)) {
path = getRegularPath(path);
logger.info("上传到FTP服务的目录:###"+path);
if (changeWorkingDirectory(path)) {
ftpFiles = ftpClient.listFiles();
if (resultList == null) {
list = new LinkedList<String>();
}
if (ftpFiles == null || ftpFiles.length == 0) {
return resultList;
}
for (int i = 0; i < ftpFiles.length; i++) {
FTPFile ftpFile = ftpFiles[i];
if (ftpFile != null && ftpFile.isFile()
&& !".".equals(ftpFile.getName())
&& !"..".equals(ftpFile.getName())) {
if (resultList!=null) {
resultList.add(ftpFile.getName());
}else{
list.add(ftpFile.getName());
}
} else if (ftpFile != null && ftpFile.isDirectory()
&& !".".equals(ftpFile.getName())
&& !"..".equals(ftpFile.getName())) {
changeWorkingDirectory("~/");
if (resultList!=null) {
getFileList(path + SEPARATOR
+ ftpFile.getName(), resultList);
}else{
getFileList(path + SEPARATOR
+ ftpFile.getName(), list);
}
}
}
} else {
logger.info("切换目录失败,请检查路径");
}
} else {
logger.info("所查询的条件为空,将不予查询");
}
} catch (Exception e) {
logger.error("获取路径" + path + "时出现异常!", e);
}
return list;
}
/**
* 下载文件至本地目录中
* @param remoteFileName
* 远程文件名
* @param localFileName
* 本地文件名
* @param remoteDirectory 远程FTP目录
* @param localDirectory 本地目录
* @return 返回操作结果
* @author lester
*/
public boolean download(String remoteFileName, String localFileName,String remoteDirectory,String localDirectory) {
boolean flag = false;
File outfile = null;
OutputStream outputStream = null;
if (StringUtils.isNotBlank(remoteFileName)&&remoteFileName.contains(".")) {
if (StringUtils.isNotBlank(localDirectory)) {
if (StringUtils.isNotBlank(remoteDirectory)) {
remoteDirectory = getRegularPath(remoteDirectory);
if (!changeWorkingDirectory(remoteDirectory)) {
logger.info("切换远程目录失败,请检查所传路径是否正确,具体路径为"
+ remoteDirectory);
return flag;
}
} else {
logger.info("传入的远程路径为空,将在FTP宿主目录中寻找文件");
}
if (StringUtils.isNotBlank(localFileName)) {
outfile = new File(localDirectory+SEPARATOR+localFileName);
} else {
outfile = new File(localDirectory+SEPARATOR+remoteFileName);
}
try {
outputStream = new FileOutputStream(outfile);
flag = ftpClient.retrieveFile(remoteFileName, outputStream);
} catch (IOException e) {
logger.error("下载文件时出现IO异常,远程路劲为:" + remoteFileName, e);
flag = false;
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
logger.error("关闭输出流时出现异常!", e);
flag = false;
}
}
}else{
logger.info("传入的本地路径为空,将不予下载");
}
}else{
logger.info("远程文件名为空或者不是文件类型,将不予下载");
}
return flag;
}
public static void main(String[] args) {
FtpUtils ftpUtil = new FtpUtils();
//ftpUtil.connectionFtp();
ftpUtil.connectionFtp("127.0.0.1", 21, "cnd", "cnd", 21000);
// ftpUtil.uploadSimpleFile("E:\\小图片\\upload\\1\\1.txt",
// "1234/123213","3.txt");
// ftpUtil.uploadHttpFile("http://www.google.com.hk/images/nav_logo105.png","uploadHttpFile");
// ftpUtil.uploadHttpFile("http://www.google.com.hk/images/nav_logo105.png","");
// ftpUtil.uploadSimpleFolder("E:\\小图片\\upload\\1","E:\\小图片\\upload","uploadSimpleFolder");
// ftpUtil.deleteFile("/1/01/logo.png");
// ftpUtil.removeDirectory("test",true);
ftpUtil.removeDirectory("/test");
List<String> list = ftpUtil.getFileList("/test",null);
for (String string : list) {
System.out.println(string);
}
// ftpUtil.download("5.txt","22.txt", "start/1/2/3/4/5","J:\\");
ftpUtil.closedFtp();
}
}
2.Freemarker 模版
alert("now in adConfig.js");
var advConfig = true;
var isHD = true ;
if(System.isHDPlatform == 4 || System.isHDPlatform == 3 || System.isHDPlatform == 1 || System.isHDPlatform == false)isHD = false ;
alert("in adConfig.js System.isHDPlatform is " + System.isHDPlatform) ;
var advInfo = [
<#list channelList as channelItem >
{serviceId:${channelItem},locationVar:"locationVar_${channelItem}"}<#if channelItem_has_next>,
<#else> </#if></#list>
];
<#assign index = 0>
<#list channelList as item>
<#assign key = channelList[index]>
<#assign datas = resultMap[key]>
<#assign positionlist = datas> <#-- 通过频道的内广告位列表 -->
var locationVar_${key}=[
<#list datas as posItem>
{position:${posItem.flag?default('')},imgSrc:${posItem.materials?default('')}<#if posItem_has_next>},
<#else>}</#if>
</#list>
];
<#assign index = index+1 >
</#list>
alert("now out adConfig.js");
3.工具类还包括:FTP文件上传、Freemarker、文件操作等等工具类日期类型和String与Long之间的转换、汉字转拼音、解析xml、json字符串的转换、假分页、Freemarker模版的工具类,字符串处理等等.
真是懒得拷贝了 ,还是放到压缩包内吧。