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("上传异常~"); } } }
相关文章推荐
- Apache FTPClient操作文件上传下载及公共类
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- org.apache.commons.net.ftp.FTPClient上传、下载、修改文件等功能
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- Apache FTPClient操作文件上传下载及公共类
- FTP文件的上传和下载---org.apache.commons.net.ftp.FTPClient
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- java基于Apache FTP实现文件上传、下载、修改文件名、删除
- 利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能
- Apache FTP文件上传、下载、修改文件名、删除
- Java FTP(Apache)文件上传、下载
- FTP文件的上传和下载---org.apache.commons.net.ftp.FTPClient
- Java FTP(Apache)文件上传、下载
- 基于Apache FTP点断续传的文件上传和下载
- org.apache.commons.net.ftp.FTPClient文件上传下载,中文乱码
- 基于Apache FTP实现的文件上传下载工具
- Apache FTPClient上传下载文件
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- apache 的 commons-net-3.3.jar 的 ftp java代码上传下载文件
- java基于Apache FTP点断续传的文件上传和下载