/**
* 从ftp下载文件到本地
*
* @param ip
* IP
* @param port
* 端口
* @param userName
* 用户名
* @param password
* 密码
* @param remotePath
* 远程文件目录
* @param remoteFileName
* 远程文件
* @param localFilePath
* 本地文件目录
* @throws Exception
*/
public static void downloadFtp(String ip, int port, String userName,
String password, String remotePath, String remoteFileName,
String localFilePath) throws Exception {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip, port);
// 登陆
if (ftpClient.login(userName, password)) {
ftpClient.enterLocalPassiveMode();
// 改变工作目录
changWorkingDirectory(remotePath, ftpClient, false);
ftpClient.setControlEncoding("GBK");
// 获取FTP登陆目录下的所有文件
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
if (remoteFileName.equalsIgnoreCase(file.getName())) {
BufferedOutputStream out = null;
try {
// IO流下载文件到本地
out = new BufferedOutputStream(
new FileOutputStream(new File(
localFilePath, file.getName())));
// 开始下载
//文件下载时使用的文件名采用ISO-8859-1编码,否则中文下载的内容为空
ftpClient.retrieveFile(new String(file.getName().getBytes("GBK"),"ISO-8859-1"), out);
if (log.isDebugEnabled()) {
log.debug("下载文件:" + file.getName() + "到本地路径:"
+ localFilePath);
}
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
}
}
}
}
}
} finally {
if (ftpClient != null) {
try {
ftpClient.disconnect();
ftpClient = null;
} catch (IOException e) {
throw e;
}
}
}
}
/**
* 改变ftp的工作目录
*
* @param remotePath
* @param ftpClient
* @param isMakeDir
* @throws IOException
*/
private static void changWorkingDirectory(String remotePath,
FTPClient ftpClient, boolean isMakeDir) throws IOException {
if (remotePath != null) {
String rps[] = remotePath.split("/");
for (int i = 0; i < rps.length; i++) {
if (!ftpClient.changeWorkingDirectory(rps[i])) {
if(isMakeDir){
ftpClient.makeDirectory(rps[i]);
ftpClient.changeWorkingDirectory(rps[i]);
}else{
throw new RuntimeException("找不到该目录:" + rps[i]);
}
}
}
}
}
/**
* 上传文件至ftp服务器
*
* @param ip
* IP
* @param port
* 端口
* @param userName
* 用户名
* @param password
* 密码
* @param localFile
* 本地文件
* @param remotePath
* 远程文件目录
* @throws Exception
*/
public static void uploadFtp(String ip, int port, String userName,
String password, File localFile, String remotePath)
throws Exception {
InputStream in = null;
FTPClient ftpClient = null;
if (localFile != null) {
try {
ftpClient = new FTPClient();
in = new FileInputStream(localFile);
ftpClient.connect(ip, port);
if (ftpClient.login(userName, password)) {
ftpClient.enterLocalPassiveMode();
// 改变工作目录
changWorkingDirectory(remotePath, ftpClient, true);
// 设置为二进制传输模式
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GBK");
boolean flag = ftpClient.storeFile(new String(localFile
.getName().getBytes("GBK"), "ISO-8859-1"), in);
if (!flag) {
throw new BusinessException("上传文件["
+ localFile.getName() + "]失败");
}
}
} catch (SocketException e) {
log.error(e.getMessage(), e);
throw e;
} catch (IOException e) {
log.error(e.getMessage(), e);
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
} finally {
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e) {
throw e;
}
}
if (ftpClient != null) {
try {
ftpClient.disconnect();
ftpClient = null;
} catch (IOException e) {
throw e;
}
}
}
}
}
/**
* 从ftp下载文件到本地
*
* @param ip
* IP
* @param port
* 端口
* @param userName
* 用户名
* @param password
* 密码
* @param remotePath
* 远程文件目录
* @throws Exception
*/
public static List<String> listFtpFileNames(String ip, int port,
String userName, String password, String remotePath)
throws Exception {
FTPClient ftpClient = null;
List<String> fileNameList = new ArrayList<String>();
try {
ftpClient = new FTPClient();
ftpClient.connect(ip, port);
// 登陆
if (ftpClient.login(userName, password)) {
ftpClient.enterLocalPassiveMode();
// 改变工作目录
changWorkingDirectory(remotePath, ftpClient, false);
ftpClient.setControlEncoding("GBK");
// 获取FTP登陆目录下的所有文件
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
if (file.isFile()) {
fileNameList.add(file.getName());
}
}
}
} finally {
if (ftpClient != null) {
try {
ftpClient.disconnect();
ftpClient = null;
} catch (IOException e) {
throw e;
}
}
}
return fileNameList;
}
Oracle语法
http://blog.sina.com.cn/s/blog_b5d14e2a0101c56z.html
http://blog.chinaunix.net/uid-21187846-id-3288525.html