FTP进行上传下载文件
1、需要引入外部jar包:commons-net-2.0.jar(或者是子包commons-net-ftp-2.0.jar)
2、需下载ftp服务器
3、 本地电脑访问ftp服务器格式:ftp://用户名:密码@ftp服务器ip
4、以下用api访问
STEP1:
Java代码
/**
* 连接并登录FTP
* @param hostname:ftp地址(不带ftp://) 即ip
* @param username:登录用户名
* @param password:登录密码
**/
public int openFtp(String hostname, String username, String password) {
int result = 0;
// 第一步:实例化FTPClient
ftpClient = new FTPClient();
try {
// 第二步:连接FTP
ftpClient.connect(hostname);
// 第三步:登录FTP
ftpClient.login(username, password);
} catch (SocketException e) {
// 连接错误时捕捉此异常
result = 1;
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
STEP2:
Java代码
/**
* 下载单个文件
*
* @param remoteFile
* :需要下载的文件,格式为ftp://xx.xx.xx.xx/remoteFile,如:ftp://10.10.10.10/dir1
* /dir2/file.txt,则remoteFile为dir1/dir2/file.txt
* @param localFile:下载的文件保存到本地的文件,为完整绝对路径。
* @return
*/
public int ftpDownload(String remoteFile, String localFile) {
FileOutputStream fos = null;
InputStream is = null;
try {
// 第一步:设置基本属性
ftpClient.setBufferSize(1024);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 第二步:获取远程文件的输入流
is = ftpClient.retrieveFileStream(remoteFile);
if (is == null) {
// 如果输入流为空,则表示要下载的远程文件不存在
return 0;
} else {
// 如果输入流不为空,则将远程文件的输入流写到本地
fos = new FileOutputStream(localFile);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = is.read(buffer)) != -1) {
fos.write(buffer, 0, i);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭输入输出流
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(fos);
}
return 1;
}
STEP3:
STEP4:
Java代码
/**
* 退出登录ftp,并断开连接
*/
public void closeFtp() {
try {
if (ftpClient != null) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
// 断开连接错误时捕捉此异常
}
}
原文来自:雨枫技术教程网 http://www.fengfly.com
原文网址:http://www.fengfly.com/plus/view-190551-1.html
FTP进行上传下载文件的更多相关文章
- linux下常用FTP命令 上传下载文件【转】
1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...
- Linux 终端访问 FTP 及 上传下载 文件
今天同事问我一个问题,在Linux 下访问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上.google 一下. 方 ...
- shell通过ftp实现上传/下载文件
直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...
- Linux 终端訪问 FTP 及 上传下载 文件
今天同事问我一个问题,在Linux 下訪问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上. google 一下. ...
- Linux 终端访问 FTP 及 上传下载 文件[转]
1. Linux 终端连接FTP [oracle@Dave ~]$ ftp 10.85.7.97 Connected to 10.85.7.97. 220 Serv-U FTP Server ...
- 使用批处理文件在FTP服务器 上传下载文件
1.从ftp服务器根目录文件夹下的文件到指定的文件夹下 格式:ftp -s:[配置文件] [ftp地址] 如:ftp -s:c:\vc\ftpconfig.txt 192.168.1.1 建立一个 ...
- Spring学习---Spring中利用组件实现从FTP服务器上传/下载文件
FtpUtil.java import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcepti ...
- HCNA管理设置文件系统FTP服务上传下载文件
1.拓扑图 2.R2配置 The device is running! ###################################### <Huawei>sys Enter s ...
- 使用ftp软件上传下载php文件时换行丢失bug
正 文: 在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了, ...
随机推荐
- 自定义 cell 自适应高度
#import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation Commo ...
- sizeof与类,继承,virtual的种种
对虚继承层次的对象的内存布局,在不同编译器实现有所区别. 首先,说说GCC的编译器. 它实现比较简单,不管是否虚继承,GCC都是将虚表指针在整个继承关系中共享的,不共享的是指向虚基类的指针. clas ...
- 索引列上的统计 <第一篇>
一.索引在查询优化中的角色 SQL Server的查询优化器是基于开销的优化器.它通过确认选择性.数据的唯一性以及过滤数据(通过WHERE或JOIN子句)所使用的列来决定最佳的数据访问机制.统计与索引 ...
- HashMap非线程安全分析
通过各方资料了解,HashMap不是线程安全的,但是为什么不是线程安全的,在什么情况下会出现问题呢? 1. 下面对HashMap做一个实验,两个线程,并发写入不同的值,key和value相同,最后再看 ...
- javascript遍历Json对象个数
var data={}; for (var d in data) { $(data[d]).each(function (i, e) { aler ...
- BindService总结
一.整体工程图 二.activity_bind.xml <?xml version="1.0" encoding="utf-8"?> <Lin ...
- poj 2305(指定进制,大数取模)
题意:输入一个进制b,在输入两个基于b进制的大整数 x,y ,求x%y的b进制结果. http://162.105.81.212/JudgeOnline/problem?id=2305 函数: Str ...
- 游标的使用实例(Sqlserver版本)
游标,如果是之前给我说这个概念,我的脑子有二个想法:1.你牛:2.我不会 不会不是理由,更不是借口,于是便要学习,本人属性喜欢看代码,不喜欢看书的人,所以嘛,文字对我没有吸引力:闲话少说啊,给大家提供 ...
- Python 异步IO、IO多路复用
事件驱动模型 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 基于RSA的加密/解密示例C#代码
using System;using System.Security.Cryptography;using System.Text; class RSACSPSample{ static void M ...