JAVA中使用FTPClient实现文件上传下载实例代码
作者:chuquan.ou 发布时间:2021-08-17 20:45:05
标签:java,ftpclient,文件上传,下载
在java程序开发中,ftp用的比较多,经常打交道,比如说向FTP服务器上传文件、下载文件,本文给大家介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件。
一、上传文件
原理就不介绍了,大家直接看代码吧
/**
* Description: 向FTP服务器上传文件
* @Version1.0 Jul 27, 2008 4:31:09 PM by 崔红保(cuihongbao@d-heaven.com)创建
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
publicstaticboolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}<pre></pre>
/**
* Description: 向FTP服务器上传文件
* @Version1.0 Jul 27, 2008 4:31:09 PM by 崔红保(cuihongbao@d-heaven.com)创建
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
下面我们写两个小例子:
1.将本地文件上传到FTP服务器上,代码如下:
@Test
publicvoid testUpLoadFromDisk(){
try {
FileInputStream in=new FileInputStream(new File("D:/test.txt"));
boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}<pre></pre>
@Test
public void testUpLoadFromDisk(){
try {
FileInputStream in=new FileInputStream(new File("D:/test.txt"));
boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
2.在FTP服务器上生成一个文件,并将一个字符串写入到该文件中
@Test
publicvoid testUpLoadFromString(){
try {
InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8"));
boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", input);
System.out.println(flag);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}<pre></pre>
@Test
public void testUpLoadFromString(){
try {
InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8"));
boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", input);
System.out.println(flag);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
二、下载文件
从FTP服务器下载文件的代码也很简单,参考如下:
/**
* Description: 从FTP服务器下载文件
* @Version. Jul , :: PM by 崔红保(cuihongbao@d-heaven.com)创建
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
publicstaticboolean downFile(String url, int port,String username, String password, String remotePath,String fileName,String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
if(ff.getName().equals(fileName)){
File localFile = new File(localPath+"/"+ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}<pre></pre>
0
投稿
猜你喜欢
- engine的实现结构elasticsearch对于索引中的数据操作如读写get等接口都封装在engine中,同时engine还封装了索引的
- Pager.javapackage pers.kangxu.datautils.common;import java.io.Serializ
- 1. 初始 Spring Boot1.1 什么是Spring BootSpring 的诞生是为了简化 Java 程序的开发的Spring B
- 单行文本的输入存在严重的缺陷,也不适合实际的运用,本节通过一个无功能的记事本来介绍可以进行多行输入的JTextArea:JTextArea(
- android中提供了4中动画: AlphaAnimation 透明度动画效果 ScaleAnimation 缩放动画效果 Translat
- 本文实例讲述了Spring实战之ResourceLoader接口资源加载用法。分享给大家供大家参考,具体如下:一 代码package lee
- 结束firefox的进程,一句代码就够了,如下:Runtime.getRuntime().exec("taskkill /F /I
- 项目场景:适用于接口数据敏感信息,比如 明文传输姓名、居住地址、手机号等信息,如果存在明文传输敏感数据问题、及数据泄漏风险,则可使用此方法加
- 这篇文章主要介绍了Java通过Scanner了解if...else if语句,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- 1. 前言Spring的核心技术IOC(Intorol of Converse控制反转)的实现途径是DI(dependency Insert
- Profile多环境配置我们在开发项目时,通常同一套程序会被发布到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、red
- @Autowired注入依赖失败的问题1、现象描述在Spring Boot项目中使用@Autowired注解,程序启动时发现服务启动失败,提
- 这篇文章主要介绍了java获取当前时间的四种方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 以下摘自胖哥分享的 2022开工福利教程。在学习Spring Security的时候你有没有下面这两个疑问:Spring Security的
- 记录一下使用IDEA创建servlet并使用Tomcat本地部署的过程。需要安装好的软件首先IDEA社区版不支持Java EE,因此要使用U
- 前言如今多线程编程已成为了现代软件开发中的重要部分,而并发编程中的线程同步问题更是一道难以逾越的坎。在Java语言中,synchronize
- 1.组装查询条件组装查询其实很简单,可以支持条件的链式编程:查询用户名包含a,年龄在 10 - 20 之间并且邮箱不为空的用户:@Testv
- 需求:有些时候,我们需要连接多个数据库,但是,在方法调用前并不知道到底是调用哪个。即同时保持多个数据库的连接,在方法中根据传入的参数来确定。
- 前言我们知道在 Java 中读取一些配置文件信息,是在开发中十分常用的要求。例如:这里我们使用 JDBC 实例:连接My
- 综述在Android系统中,出于对性能优化的考虑,对于Android的UI操作并不是线程安全的。也就是说若是有多个线程来操作UI组件,就会有