您的位置:首页 > 运维架构 > Apache

Apache FTP 文件上传和下载

2017-01-17 00:00 405 查看
import com.yjhl.yqb.common.CommonResponse;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.io.*;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.TimeZone;

/**
* Created by wanghaigen on 2016/12/26.
*/
public class ApacheFTPUtil {

private static FTPClient ftpClient;
// 远程服务器IP地址
private static String strIp;
// 服务器端口
private static int intPort;
// 服务器登陆用户名
private static String user;
// 服务器密码
private static String password;

private static final Logger logger = LoggerFactory.getLogger(ApacheFTPUtil.class);

static{
ResourceBundle bean = ResourceBundle.getBundle("environment", Locale.getDefault());
strIp = bean.getString("ftp.ip");
intPort = Integer.parseInt(bean.getString("ftp.port"));
user = bean.getString("ftp.name");
password = bean.getString("ftp.password");
ftpClient = new FTPClient();

}

public ApacheFTPUtil(){

}

/**
* 登录连接
*
* @return
*/
public static boolean ftpLogin() {
boolean isLogin = false;
FTPClientConfig ftpClientConfig = new FTPClientConfig();
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
ftpClient.setControlEncoding("GBK");
ftpClient.configure(ftpClientConfig);
try {
if (intPort > 0) {
ftpClient.connect(strIp, intPort);
} else {
ftpClient.connect(strIp);
}
// FTP服务器连接回答
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
logger.error("登录FTP服务失败!");
return isLogin;
}
ftpClient.login(user, password);
// 设置传输协议
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
logger.info("恭喜" + user + "成功登陆FTP服务器");
isLogin = true;
} catch (Exception e) {
e.printStackTrace();
logger.error(user + "登录FTP服务失败!" + e.getMessage());
}
ftpClient.setBufferSize(1024 * 2);
ftpClient.setDataTimeout(30 * 1000);
return isLogin;
}

/**
* 登出
*/
public static void ftpLogOut() {
if (null != ftpClient && ftpClient.isConnected()) {
try {
boolean reuslt = ftpClient.logout();// 退出FTP服务器
if (reuslt) {
logger.info("成功退出服务器");
}
} catch (IOException e) {
e.printStackTrace();
logger.warn("退出FTP服务器异常!" + e.getMessage());
} finally {
try {
ftpClient.disconnect();// 关闭FTP服务器的连接
} catch (IOException e) {
e.printStackTrace();
logger.warn("关闭FTP服务器的连接异常!");
}
}
}
}

/**
* 上传Ftp文件
*
* @param localFile        本地文件
* @param romotUpLoadePath 上传服务器路径 - 应该以/结束
* @return
*/
public static boolean uploadFile(File localFile, String romotUpLoadePath) {
BufferedInputStream inStream = null;
boolean success = false;
try {
ftpClient.changeWorkingDirectory(romotUpLoadePath);// 改变工作路径
inStream = new BufferedInputStream(new FileInputStream(localFile));
logger.info(localFile.getName() + "开始上传.....");
success = ftpClient.storeFile(localFile.getName(), inStream);
if (success == true) {
logger.info(localFile.getName() + "上传成功");
return success;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error(localFile + "未找到");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}

/**
* 下载文件
* @param remoteFileName 待下载文件名称
* @param localDires 下载到当地那个路径下
* @param remoteDownLoadPath 所在的路径
* @return
*/
public static boolean downloadFile(String remoteFileName, String localDires,
String remoteDownLoadPath) {
String strFilePath = localDires + remoteFileName;
BufferedOutputStream outStream = null;
boolean success = false;
if(!new File(localDires).exists())   {//创建文件夹-查看本地是否有存放文件夹
new File(localDires).mkdirs();
}
try {
ftpClient.changeWorkingDirectory(remoteDownLoadPath);
outStream = new BufferedOutputStream(new FileOutputStream(
strFilePath));
logger.info(remoteFileName + "开始下载....");
success = ftpClient.retrieveFile(remoteFileName, outStream);
if (success == true) {
logger.info(remoteFileName + "成功下载到" + strFilePath);
return success;
}
} catch (Exception e) {
e.printStackTrace();
logger.error(remoteFileName + "下载失败");
} finally {
if (outStream != null ) {
try {
outStream.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (success == false) {
logger.error(remoteFileName + "下载失败!!!");
}
return success;
}

public static void main(String[] args) {
ApacheFTPUtil.ftpLogin();
//      File file = new File("D:\\order.dsv");
//      ApacheFTPUtil.uploadFile(file,"upload/");
String fileName = "DJ_025_20160923_03.txt";
String localPath = "D:\\";
String servicePath = "upload/";
ApacheFTPUtil.ftpLogin();
boolean uploadBl = ApacheFTPUtil.downloadFile(fileName,localPath,servicePath+"backup/");
if(!uploadBl){
logger.error("上传异常~");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  FTP上传 下载 apache