Java怎么实现FTP的上传与下载功能
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要讲解了"Java怎么实现FTP的上传与下载功能",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java怎么实现FTP的上传与下载功能"吧!J
千家信息网最后更新 2025年11月07日Java怎么实现FTP的上传与下载功能
这篇文章主要讲解了"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.下载
感谢各位的阅读,以上就是"Java怎么实现FTP的上传与下载功能"的内容了,经过本文的学习后,相信大家对Java怎么实现FTP的上传与下载功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
功能
服务器
编码
服务
字符
文件
学习
内容
工具
目录
路径
UTF-8
封装
工作
大同小异
关键
大小
字符串
就是
常用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
华容县网络安全局
互联网高科技发展
通信传输线路网络安全保障措施
世界第一通信网络技术
实现数据库安全控制的方法有
地方贸易数据库
魔兽世界奥金斧服务器职业人数
南平公文机器人rpa软件开发
单机软件数据库选型
数据库如何更改网址
计算机网络技术的优势劣势
石景山区综合软件开发怎么样
多个服务器管理工具
软件开发人员 资质
青少年安全网络安全知识竞赛
丽水app软件开发工具
广东大数据时间同步服务器
搜索软件网络安全
MySQL数据库默认的用户为
计算机三级网络技术不能考
网络安全保卫大队工作流程
服务器如何增强内存
韩国u数据库
软件开发热门工具
lol艾欧尼亚服务器维护
常见的非关系型数据库包括
深圳常见的分布式存储服务器
网络安全和保密责任
IBM服务器装lunx系统
软件数据库开发视频教学视频