package com.zhirong.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtil{
/**
* Description: 从FTP服务器下载文件
*
* @Version1.0
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
*
* @param remotePath
* FTP服务器上的相对路径
*
* @param fileName
* 要下载的文件名
* @return
*/
private static String LOCAL_CHARSET = "GBK";
public static boolean downFtpFile(String url, int port, String username,
String password, String remotePath, String fileName,OutputStream os,
HttpServletResponse response) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
//开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
if (FTPReply.isPositiveCompletion(ftp.sendCommand("OPTS UTF8", "ON"))) {
LOCAL_CHARSET = "UTF-8";
}
ftp.setControlEncoding(LOCAL_CHARSET);
FTPClientConfig conf;
if("UTF-8".equals(LOCAL_CHARSET)) {
conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
}else{
conf = new FTPClientConfig( FTPClientConfig.SYST_NT);
}
conf.setServerLanguageCode("zh");
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//判断连接是否成功
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
//设置访问被动模式
ftp.setRemoteVerificationEnabled(false);
ftp.enterLocalPassiveMode();
//转移到FTP服务器目录
ftp.changeWorkingDirectory(remotePath);
FTPFile[] fs = ftp.listFiles();
for (int i = 0; i < fs.length; i++) {
FTPFile ff = fs[i];
if (ff.getName().equals(fileName)) {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition","attachment;filename="
+ URLEncoder.encode(fileName, "utf-8"));
if("UTF-8".equals(LOCAL_CHARSET)) {
ftp.retrieveFile(new String(ff.getName().getBytes("utf-8"),"ISO-8859-1"), os);
}else{
ftp.retrieveFile(new String(ff.getName().getBytes("GBK"),"ISO-8859-1"), os);
}
os.flush();
os.close();
ftp.logout();
success = true;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
}
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtil{
/**
* Description: 从FTP服务器下载文件
*
* @Version1.0
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
*
* @param remotePath
* FTP服务器上的相对路径
*
* @param fileName
* 要下载的文件名
* @return
*/
private static String LOCAL_CHARSET = "GBK";
public static boolean downFtpFile(String url, int port, String username,
String password, String remotePath, String fileName,OutputStream os,
HttpServletResponse response) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
//开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
if (FTPReply.isPositiveCompletion(ftp.sendCommand("OPTS UTF8", "ON"))) {
LOCAL_CHARSET = "UTF-8";
}
ftp.setControlEncoding(LOCAL_CHARSET);
FTPClientConfig conf;
if("UTF-8".equals(LOCAL_CHARSET)) {
conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
}else{
conf = new FTPClientConfig( FTPClientConfig.SYST_NT);
}
conf.setServerLanguageCode("zh");
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//判断连接是否成功
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
//设置访问被动模式
ftp.setRemoteVerificationEnabled(false);
ftp.enterLocalPassiveMode();
//转移到FTP服务器目录
ftp.changeWorkingDirectory(remotePath);
FTPFile[] fs = ftp.listFiles();
for (int i = 0; i < fs.length; i++) {
FTPFile ff = fs[i];
if (ff.getName().equals(fileName)) {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition","attachment;filename="
+ URLEncoder.encode(fileName, "utf-8"));
if("UTF-8".equals(LOCAL_CHARSET)) {
ftp.retrieveFile(new String(ff.getName().getBytes("utf-8"),"ISO-8859-1"), os);
}else{
ftp.retrieveFile(new String(ff.getName().getBytes("GBK"),"ISO-8859-1"), os);
}
os.flush();
os.close();
ftp.logout();
success = true;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
}