最近在使用ftp进行保存文件的时候碰到下载文件能切换到指定目录 ,但获取不到目录底下的文件问题,作如下改动,分享一下,包括3个方法,
1. 上传
2. 下载
3. 切换到指定目录,如不存在则生成
import com.XXX.res.frame.web.util.ResMessageSourceUtil;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
/**
* Discript:
* @author <a href="yang.bo17@XXX.com">杨波</a>
* @version 创建时间:2017年7月6日 下午2:10:42
*/
public class FtpFileStoreUtils{
private static Logger logger= LoggerFactory.getLogger(FtpFileStoreUtils.class);
/**
* 向FTP服务器上传文件
* @param url
* @param port
* @param username
* @param password
* @param ftpUploadDir
* @param filename
* @param input
* @param msg
* @return
*/
public static boolean uploadFile(String url, int port, String username,
String password, String ftpUploadDir, String filename, InputStream input, Map<String, String> msg) {
boolean result = false;
FTPClient ftpClient = new FTPClient();
try {
int reply;
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftpClient.connect(url, port);// 连接FTP服务器
// 登录
if(ftpClient.login(username, password)){
// 转移工作目录至指定目录下
changWorkingDirectory(ftpUploadDir, ftpClient, false);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
result = ftpClient.storeFile(filename, input);
if (result) {
msg.put("msg", ResMessageSourceUtil.getMessage("frmweb.FILE.UPLOAD.SUCCESSFUL"));
msg.put("code", "201");
}
}else{
logger.debug("uploadFile 连接ftp失败, URL:[{}], 用户名:[{}], 密码:[{}], 端口:[{}]", url, username, password, port);
ftpClient.disconnect();
msg.put("msg",ResMessageSourceUtil.getMessage("frmweb.FAILED.CONN.FTP"));
msg.put("code", "101");
return result;
}
ftpClient.logout();
} catch (IOException e) {
logger.debug("uploadFile 程序执行异常, [{}]", e.getMessage());
msg.put("msg",ResMessageSourceUtil.getMessage("frmweb.UPLOAD.FAILED"));
msg.put("code", "301");
} finally {
try {
if(input != null){
input.close();
}
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
}catch (Exception e){
}
}
return result;
}
/**
* 从FTP服务器下载文件到输出流中
* @param url
* @param port
* @param username
* @param password
* @param remotePath
* @param fileName
* @param oStream
* @param msg
* @return
*/
public static boolean downFile(String url, int port, String username,
String password, String remotePath, String fileName, OutputStream oStream, Map<String, String> msg) {
boolean result = false;
FTPClient ftpClient = new FTPClient();
try {
ftpClient = new FTPClient();
ftpClient.connect(url, port);
// 登陆
if (ftpClient.login(username, password)) {
ftpClient.enterLocalPassiveMode();
// 改变工作目录
changWorkingDirectory(remotePath, ftpClient, false);
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("UTF-8");
// 获取FTP登陆目录下的所有文件
FTPFile[] fs = ftpClient.listFiles();
if(fs != null){
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
ftpClient.retrieveFile(ff.getName(), oStream);
break;
}
}
result = true;
ftpClient.logout();
}else{
logger.debug(ResMessageSourceUtil.getMessage("frmweb.UNABLE.READ.FILE"));
msg.put("msg", ResMessageSourceUtil.getMessage("frmweb.UNABLE.READ.FILE"));
msg.put("code", "201");
result = false;
ftpClient.logout();
}
}else{
logger.debug("downFile() FTP server refused connection.");
ftpClient.disconnect();
msg.put("msg",ResMessageSourceUtil.getMessage("frmweb.FAILED.CONN.FTP"));
msg.put("code", "101");
return result;
}
} catch (Exception e) {
logger.debug("downFile 程序执行异常, [{}]", e);
msg.put("msg", ResMessageSourceUtil.getMessage("frmweb.DOWMLOADS.FAILED"));
msg.put("code", "301");
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
/**
* 切换目录
* @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]);
}
}
}
}
}
}
在此分享,欢迎各位看客们围观!!!