Java实现FTP上传与下载功能
本文实例为大家分享了Java实现FTP上传与下载的具体代码,供大家参考,具体内容如下
JAVA操作FTP服务器,只需要创建一个FTPClient即可,所有的操作都封装在FTPClient中,JDK自带的有FTPClient(sun.net.ftp.FtpClient),也可以用第三方的FTPClient,一般使用apache的FTPClient(org.apache.commons.net.ftp.FTPClient),本文将使用apache的FTPClient,API都大同小异
关键依赖:commons-net
对常用操作(上传、下载)封装成工具类
package com.day0322; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /** * FTP工具类 * 文件上传 * 文件下载 */ public class FTPUtil { private static final Logger log = LoggerFactory.getLogger(FTPUtil.class); /** * 设置缓冲区大小4M **/ private static final int BUFFER_SIZE = 1024 * 1024 * 4; /** * 本地字符编码 **/ private static String LOCAL_CHARSET = "GBK"; /** * UTF-8字符编码 **/ private static final String CHARSET_UTF8 = "UTF-8"; /** * OPTS UTF8字符串常量 **/ private static final String OPTS_UTF8 = "OPTS UTF8"; /** * FTP协议里面,规定文件名编码为iso-8859-1 **/ private static final String SERVER_CHARSET = "ISO-8859-1"; private static FTPClient ftpClient = null; /** * 连接FTP服务器 */ private static void login(OaFtp oaFtp) { ftpClient = new FTPClient(); try { ftpClient.connect(oaFtp.getIp(), Integer.valueOf(oaFtp.getPort())); ftpClient.login(oaFtp.getName(), oaFtp.getPwd()); ftpClient.setBufferSize(BUFFER_SIZE); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { closeConnect(); } } catch (Exception e) { log.error("",e); throw new RuntimeException(e); } } /** * 关闭FTP连接 */ private static void closeConnect() { if (ftpClient != null && ftpClient.isConnected()) { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { log.error("",e); } } } /** * FTP服务器路径编码转换 * * @param ftpPath FTP服务器路径 * @return String */ private static String changeEncoding(String ftpPath) { String directory = null; try { if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(OPTS_UTF8, "ON"))) { LOCAL_CHARSET = CHARSET_UTF8; } directory = new String(ftpPath.getBytes(LOCAL_CHARSET), SERVER_CHARSET); } catch (Exception e) { log.error("",e); } return directory; } /** * 改变工作目录 * 如果没有,则创建工作目录 * @param path */ private static void changeAndMakeWorkingDir(String path) { try { ftpClient.changeWorkingDirectory("/"); path = path.replaceAll("\\\\","/"); String[] path_array = path.split("/"); for (String s : path_array) { boolean b = ftpClient.changeWorkingDirectory(s); if (!b) { ftpClient.makeDirectory(s); ftpClient.changeWorkingDirectory(s); } } } catch (IOException e) { log.error("",e); throw new RuntimeException(e); } } /** * 上传 * @param oaFtp * @param filename * @param dirPath * @param in * @return */ public static boolean upload (OaFtp oaFtp, String filename, String dirPath, InputStream in) { login(oaFtp); if (!ftpClient.isConnected()) { return false; } boolean isSuccess = false; if (ftpClient != null) { try { if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(OPTS_UTF8, "ON"))) { LOCAL_CHARSET = CHARSET_UTF8; } ftpClient.setControlEncoding(LOCAL_CHARSET); String path = changeEncoding(dirPath); changeAndMakeWorkingDir(path); isSuccess = ftpClient.storeFile(new String(filename.getBytes(), SERVER_CHARSET), in); } catch (Exception e) { log.error("",e); } finally { closeConnect(); } } return isSuccess; } /** * 下载 * @param oaFtp * @param filename * @param dirPath * @param out * @return */ public static void download (OaFtp oaFtp, String filename, String dirPath, FileOutputStream out) { // 登录 login(oaFtp); if (ftpClient != null) { try { String path = changeEncoding(dirPath); changeAndMakeWorkingDir(path); String[] fileNames = ftpClient.listNames(); if (fileNames == null || fileNames.length == 0) { return; } for (String fileName : fileNames) { String ftpName = new String(fileName.getBytes(SERVER_CHARSET), LOCAL_CHARSET); if (StringUtils.equals(ftpName,filename)) { InputStream in = ftpClient.retrieveFileStream(fileName); IOUtils.copy(in,out); } } } catch (IOException e) { log.error("",e); } finally { closeConnect(); } } } }
测试
1.上传
2.下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?