package com.hailong.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.IOUtils;
import org.apache.struts.upload.FormFile;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/**
* FTP处理文件上传下载
* @author xzh
*
*/
public class FtpFileManager {
private FtpClient ftpClient;
private static final String MAX_SIZE = "1M";
//上传文件重命名随机数
private static final int RANDOMINT = 999999;
// 模块名
private String moduleName;
/**
* 构造方法
*
* @param moduleName
* 模块名
*/
public FtpFileManager(String moduleName) {
this.moduleName = moduleName;
}
/**
* 链接FTP服务器
* @throws IOException IOException
* @throws ThirdgsException ThirdgsException
*/
private void connectServer() throws IOException, ThirdgsException {
Configuration config = this.getConfig();
if (config == null) {
throw new ThirdgsException("未找到相关的配置!");
}
//server:FTP服务器的IP地址
String server = config.getString("ftp.server");
int port = 21;
try {
//port:FTP端口
port = new Integer(config.getString("ftp.port")).intValue();
} catch (Exception e) {
//若端口配置不合法,默认21
}
//user:登录FTP服务器的用户名
String user = config.getString("ftp.user");
//password:登录FTP服务器的密码
String password = config.getString("ftp.password");
//path:FTP服务器上的路径(FTP服务下主目录的子目录)
String path = config.getString("ftp.path");
ftpClient = new FtpClient();
try {
ftpClient.openServer(server, port);
ftpClient.login(user, password);
} catch (Exception e) {
throw new ThirdgsException("FTP登录失败,原因:" + e.getMessage());
}
// path是FTP服务下主目录的子目录
if (path.length() != 0) {
ftpClient.cd(path);
}
try {
//在FTP上创建目录 : XMKD 命令
ftpClient.sendServer("XMKD " + moduleName + "\r\n");
} catch (Exception e) {
//若存在则忽略
}
//使用sendserver发送命令后,需要接收服务器的相应后才能继续下面的操作.
ftpClient.readServerResponse();
ftpClient.cd(moduleName);
// 用2进制上传
ftpClient.binary();
}
/**
* 退出FTP服务器
* @throws IOException IOException
*/
private void logout() throws IOException {
if (null != ftpClient) {
ftpClient.closeServer();
}
}
/**
*
* @param formFile 文件对象
* @return String
* @throws IOException IOException
* @throws ThirdgsException ThirdgsException
*/
public String uploadFile(FormFile formFile) throws IOException, ThirdgsException {
try {
this.validate(formFile);
this.connectServer();
String filename = this.saveFileToServer(formFile);
return filename;
} finally {
this.logout();
}
}
/**
* 下载文件
* @param fileName 下载文件名
* @param response response
* @throws Exception Exception
*/
public void downFile(String fileName, HttpServletResponse response) throws Exception {
try {
this.connectServer();
this.download(fileName, response);
} finally {
this.logout();
}
}
/**
* 查看图片
* @param fileName 查看图片名
* @param response response
* @throws Exception Exception
*/
public void showPic(String fileName, HttpServletResponse response) throws Exception {
try {
this.connectServer();
this.show(fileName, response);
} finally {
this.logout();
}
}
/**
* 得到文件服务基地址
* @return String
* @throws ConfigurationException ConfigurationException
*/
public String getBaseUrl() throws ConfigurationException {
Configuration config = new PropertiesConfiguration("adc_web_config.properties");
String baseUrl = config.getString("file.root.url") + "/" + moduleName;
return baseUrl;
}
/**
* 上传文件;并返回上传文件的名称
* @param formFile formFile
* @return String
* @throws ThirdgsException ThirdgsException
* @throws IOException IOException
*/
private String saveFileToServer(FormFile formFile) throws IOException, ThirdgsException {
TelnetOutputStream os = null;
InputStream is = null;
String filename = "ftpup_" + getName() + "." + getExtName(formFile.getFileName());
try {
os = ftpClient.put(filename);
is = formFile.getInputStream();
IOUtils.copy(is, os);
} catch (IOException e) {
throw new ThirdgsException("上传文件失败,原因:" + e.getMessage());
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return filename;
}
/**
* 下载文件
*
* @param fileName
* FormFile对象
* @param response
* HTTP响应
* @throws Exception Exception
*/
private void download(String fileName, HttpServletResponse response) throws Exception {
TelnetInputStream ftpIn = ftpClient.get(fileName);
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream out = null;
try {
out = response.getOutputStream();
IOUtils.copy(ftpIn, out);
} catch (Exception e) {
throw new ThirdgsException("下载文件失败,原因:" + e.getMessage());
} finally {
if (ftpIn != null) {
ftpIn.close();
}
}
}
/**
* 显示图片
*
* @param fileName
* FormFile对象
* @param response
* HTTP响应
* @throws IOException IOException
*/
private void show(String fileName, HttpServletResponse response) throws IOException {
TelnetInputStream ftpIn = ftpClient.get(fileName);
OutputStream out = null;
try {
out = response.getOutputStream();
IOUtils.copy(ftpIn, out);
} finally {
if (ftpIn != null) {
ftpIn.close();
}
}
}
/**
*
* @return Configuration
*/
private Configuration getConfig() {
Configuration config;
try {
config = new PropertiesConfiguration("adc_web_config.properties");
} catch (ConfigurationException e) {
return null;
}
return config;
}
/**
* 验证上传文件的合法性(类型,大小等)
* @param formFile formFile
* @throws ThirdgsException ThirdgsException
*/
private voi