FtpClient
来源:互联网 发布:淘宝动漫周边网店 编辑:程序博客网 时间:2024/05/18 01:01
依赖jar:commons-net-3.3.jar
package com.xxx.utils;import net.sf.json.JSONObject;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;import java.io.*;public class FtpClientUtil { /** * 上传文件(可供Action/Controller层使用) * @param path FTP服务器完整路径 * @param file 待上传的文件 * @return */ public static JSONObject upload(File file,String path) throws Exception{ FTPClient ftpClient = new FTPClient(); ftpClient.setControlEncoding("UTF-8"); try { String hostname = PropertiesUtil.getProperty("ftp_hostname");//FTP服务器地址 int port = Integer.parseInt(PropertiesUtil.getProperty("ftp_post"));//FTP服务器端口号 String username = PropertiesUtil.getProperty("ftp_username");//FTP登录帐号 String password = PropertiesUtil.getProperty("ftp_password");//FTP登录密码 String filePath = path.substring(0, path.lastIndexOf("/")); String fileName = path.substring(path.lastIndexOf("/") + 1, path.length()); //连接FTP服务器 ftpClient.connect(hostname, port); //登录FTP服务器 ftpClient.login(username, password); //是否成功登录FTP服务器 int replyCode = ftpClient.getReplyCode(); if(!FTPReply.isPositiveCompletion(replyCode)){ return ResultUtil.error("上传失败"); } ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.makeDirectory(filePath); ftpClient.changeWorkingDirectory(filePath); InputStream inputStream = new FileInputStream(file); ftpClient.storeFile(fileName, inputStream); inputStream.close(); ftpClient.logout(); return ResultUtil.success("上传成功"); } catch (Exception e) { throw e; } finally{ if(ftpClient.isConnected()){ try { ftpClient.disconnect(); } catch (IOException e) { throw e; } } } } /** * 下载文件 * @param path 服务器上存放文件的完整路径 * @param localPath 下载后的文件路径 * @return */ public static JSONObject download(String path,String localPath) throws Exception{ FTPClient ftpClient = new FTPClient(); try { String hostname = PropertiesUtil.getProperty("ftp_hostname");//FTP服务器地址 int port = Integer.parseInt(PropertiesUtil.getProperty("ftp_post"));//FTP服务器端口号 String username = PropertiesUtil.getProperty("ftp_username");//FTP登录帐号 String password = PropertiesUtil.getProperty("ftp_password");//FTP登录密码 String filePath = path.substring(0, path.lastIndexOf("/")); String fileName = path.substring(path.lastIndexOf("/") + 1, path.length()); //连接FTP服务器 ftpClient.connect(hostname, port); //登录FTP服务器 ftpClient.login(username, password); //验证FTP服务器是否登录成功 int replyCode = ftpClient.getReplyCode(); if(!FTPReply.isPositiveCompletion(replyCode)){ return ResultUtil.error("下载失败"); } //切换FTP目录 ftpClient.changeWorkingDirectory(filePath); FTPFile[] ftpFiles = ftpClient.listFiles(); for(FTPFile file : ftpFiles){ if(fileName.equalsIgnoreCase(file.getName())){ File localFile = new File(localPath + "/" + file.getName()); OutputStream os = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), os); os.close(); } } ftpClient.logout(); return ResultUtil.success("下载成功"); } catch (Exception e) { throw e; } finally{ if(ftpClient.isConnected()){ try { ftpClient.logout(); } catch (IOException e) { throw e; } } } }}
阅读全文
0 0
- FTPClient
- FTPClient
- FtpClient
- FTPClient
- FTPClient
- FTPClient
- ftpclient
- FTPClient
- ftpClient
- FtpClient
- FTPClient类
- ftpclient(java)
- ftpClient.binary()
- FTPClient 简介
- ftpClient 记录
- FTPClient相关
- ftpClient ,httpClient
- FTPClient 上传
- 解决关闭Hadoop时no namenode to stop异常
- 排序算法之快速排序
- 开篇
- 深入理解Dijkstra(迪杰斯特拉)算法(正确思路+优化+原理)
- this指针
- FtpClient
- 极飞P20植保无人机测评
- Socket send函数和recv函数详解
- a 超链接
- 笔记:centOS 7修改ip地址
- 泛型字典
- 使用StaggeredGridLayoutManager瀑布流,Glide显示图片
- 单节点mongodb3.4.3转换成复制集(带账号密码)
- 《代码整洁之道》摘录