JAVA 实现FTP功能_Java实现FTP上传下载功能
public classFtp {private static final Logger logger = Logger.getLogger(FTP.class);/*** FTP客户端*/
privateFtpClient ftpClient;/*** 服务器连接
*
*@paramip
* 服务器IP
*@paramport
* 服务器端口
*@paramuser
* 用户名
*@parampassword
* 密码
*@parampath
* 服务器路径
*@throwsIOException
**/
public void connectServer(String ip, int port, String user, String password, String path) throwsIOException {try{
System.out.println("ftp connect...");
ftpClient= newFtpClient(ip);
ftpClient.login(user, password);//设置成2进制传输
ftpClient.binary();
System.out.println("ftp login success!");if (path.length() != 0) {//把远程系统上的目录切换到参数path所指定的目录
try{
ftpClient.cd(path);
}catch(Exception e) {throw new IOException("FTP server: 550 CWD failed 远程目录未响应", e);
}
}
ftpClient.binary();
}catch(IOException e) {
logger.error("ftp connect err!");
logger.error(e.getMessage());
e.printStackTrace();throw newIOException(e.getMessage());
}
}/*** 关闭连接
*
**/
public voidcloseConnect() {try{
ftpClient.closeServer();
System.out.println("closeConnect success");
}catch(IOException ex) {
System.out.println("not closeConnect");
ex.printStackTrace();throw newRuntimeException(ex);
}
}/*** 上传文件
*
*@paramlocalFile
* 本地文件
*@paramremoteFile
* 远程文件
**/
public voidupload(String localFile, String remoteFile) {
TelnetOutputStream os= null;
FileInputStream is= null;try{//将远程文件加入输出流中
os =ftpClient.put(remoteFile);//获取本地文件的输入流
File file_in = newFile(localFile);
is= newFileInputStream(file_in);//创建一个缓冲区
byte[] bytes = new byte[1024];intc;while ((c = is.read(bytes)) != -1) {
os.write(bytes,0, c);
}
System.out.println("upload success");
}catch(IOException ex) {
System.out.println("not upload");
ex.printStackTrace();throw newRuntimeException(ex);
}finally{try{if (is != null) {
is.close();
}
}catch(IOException e) {
e.printStackTrace();
}finally{try{if (os != null) {
os.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 下载文件
*
*@paramremoteFile
* 远程文件路径(服务器端)
*@paramlocalFile
* 本地文件路径(客户端)
**/
public voiddownload(String remoteFile, String localFile) {
TelnetInputStream is= null;
FileOutputStream os= null;try{//获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
is =ftpClient.get(remoteFile);
File file_in= newFile(localFile);
os= newFileOutputStream(file_in);byte[] bytes = new byte[1024];intc;while ((c = is.read(bytes)) != -1) {
os.write(bytes,0, c);
}
System.out.println("download success");
}catch(IOException ex) {
System.out.println("not download");
ex.printStackTrace();throw newRuntimeException(ex);
}finally{try{if (is != null) {
is.close();
}
}catch(IOException e) {
e.printStackTrace();
}finally{try{if (os != null) {
os.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}/*** 返回FTP目录下的文件列表
*
*@paramftpDirectory
*@return
*/
public ListgetFileNameList(String ftpDirectory) {
List list = new ArrayList();
BufferedReader bf= null;
InputStreamReader isr= null;
DataInputStream ds= null;try{
ds= newDataInputStream(ftpClient.nameList(ftpDirectory));
isr= newInputStreamReader(ds);
bf= newBufferedReader(isr);
String filename= "";while ((filename = bf.readLine()) != null) {
list.add(filename);
}
}catch(Exception e) {
e.printStackTrace();
}finally{try{
ds.close();
}catch(IOException e) {
e.printStackTrace();
}try{
isr.close();
}catch(IOException e) {
e.printStackTrace();
}try{
bf.close();
}catch(IOException e) {
e.printStackTrace();
}
}returnlist;
}public static void main(String agrs[]) throwsException {
Ftp ftp= newFtp();
ftp.connectServer("192.168.1.221", 21, "admin", "123@123", "/FTP Files/temp");
List list = ftp.getFileNameList("/FTP Files/temp");for(String str : list) {try{
Calendar cal= Calendar.getInstance();//使用日历类
int year = cal.get(Calendar.YEAR);//得到年
int month = cal.get(Calendar.MONTH) + 1;//得到月,因为从0开始的,所以要加1
int day = cal.get(Calendar.DAY_OF_MONTH);//得到天
String fileName = "WiMAX_" + year + month +day;
System.out.println("str" + str + "---------" + "fileName:" +fileName);
String regEx= "WiMAX_\\d{8}_\\d{5}_\\s{5}"; //表示a或F
Pattern pat =Pattern.compile(regEx);
System.out.println(pat.matcher(str).find());//if (str.contains(fileName)) {//fu.download("/FTP Files/temp/" + str, "d:/temp/" + str);//System.out.println("download:" + str);//}
}catch(Exception e) {
e.printStackTrace();
logger.error(e.getMessage());throwe;
}
}
ftp.closeConnect();
}
}
JAVA 实现FTP功能_Java实现FTP上传下载功能相关推荐
- FTP协议中的登录 上传 下载 新建目录 删除目录 的wireshark包分析(一文看完TCP包分析,附源文件,ppt,操作视频)
目录 一原理 二.FTP登录 三.FTP下载 四.FTP上传 五.FTP新建目录 六.FTP删除目录 一原理 前言:TCP/IP四层模型和OSI模型对照,以及FTP在模型中的位置. • ...
- ubuntu下搭建FTP服务器并使用FileZilla上传下载
ubuntu下搭建FTP服务器并使用FileZilla上传下载 为了让实验室同学在共享文件时更加方便,我们决定在实验室电脑上搭建一个FTP服务器,ubuntu系统版本为16.04,下面就是我的搭建流程 ...
- java文件流 m.jb51.net_FasfDFS整合Java实现文件上传下载功能实例详解
今天使用Java代码实现文件的上传和下载.对此作者提供了Java API支持,下载fastdfs-client-java将源码添加到项目中.或者在Maven项目pom.xml文件中添加依赖 org.c ...
- java歌曲上传下载功能实现,Java上传下载功能的实现详解
上传下载是很简单的功能,但是每次用的时候还是要查,这里整理一下 前台: 提交 主要注意: enctype="multipart/form-data" method="po ...
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
- 项目_功能模块_基于Spring Boot的文件上传下载功能的设计与实现
文章目录 基于Spring Boot的文件上传下载功能模块的设计与实现 1.前言 2.技术栈 3.关键源码 4.实现效果 4.1.登录 4.2.文件列表 4.3.上传文件测试 4.3.1.测试图片 4 ...
- SpringBoot2.x集成mongoDB4.0实现音频文件的上传下载功能
最近项目要用到文件上传下载功能,但是这些音频文件都很大,放到关系型数据库就不太好了(其实是太不好了),占内存不说还慢的要死,所以考虑使用分布式文件系统或者非关系型数据库来实现.由于分布式文件系统没有用 ...
- Spring Boot项目集成AWS SDK连接到AWS S3,实现上传下载功能
本文主要描写在Spring Boot项目里集成AWS SDK连接到AWS S3,实现上传下载功能的具体代码和注意事项.如有不足和错误之处,欢迎指正. AWS S3相关介绍 AWS S3(官网): ht ...
- 教你如何实现c#文件上传下载功能
简单介绍一下c#文件上传下载功能实现. NuGet 安装SqlSugar Model文件下新建 DbContext 类 public class DbContext {public DbContext ...
- 文件的上传下载功能的实现(包括进度条)[telerik控件]
文件的上传下载功能的实现(包括进度条) 1.准备工作 首先我们需要Telerik控件,数据库,上传文件文件夹. Telerik控件: RadUpload.RadProgressManager.RadP ...
最新文章
- hibernate fetch使用
- android“设置”里的版本号
- erp开发和java开发区别_Java程序员求职必学:Spring boot学习指南!
- 4K屏幕+5500万像素摄像头,以成未来手机的一大趋势
- 破解vs2010番茄插件
- 自学程序员和科班程序员差别到底在哪里?
- 走向ASP.NET架构设计---第二章:设计 测试 代码 (前篇)
- 人脸识别门禁系统设计(一)
- 比较好的java网站
- HP PSC 系列一体机性能比较列表
- 记录Hbuilder项目使用xcode离线打包上传苹果商店踩过的坑
- 【IIOT】欧姆龙PLC数采之CP系列
- 第一次git拉取代码到本地及身份验证失败踩坑
- 【数据集格式】coco格式转txt格式
- 聊一聊:相机篇1基本成像原理
- 【045】国家标准全文公开系统-GB和GB/T国家标准库
- real——表单样式
- ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
- error: ...\Windows Kits\8.1\Include\um\combaseapi.h IUnknown is undefined 问题
- 【技术美术美术部分】AO贴图的烘焙及应用
热门文章
- 30是什么意思_“29+16”变“30+18”是什么意思?独立艺术院校有何优势?报考需要注意什么?...
- python或运算 和 in_Python’的奇怪行为是’运算符,如果与’in’结合使用[复制]
- 上海事业评中级职称还要考计算机,上海职称评审-事业单位如何评职称
- java连接数据库代码写在哪里,jdbc连接数据库怎么做的。代码写哪里呀 求大神
- 用函数实现simulink_VCU/BMS基于模型的开发---Simulink 代码集成
- 强行杀windows服务
- 【刷题】BZOJ 4657 tower
- pringMVC“Ambiguous mapping found. Cannot map 'XXXController' bean method”解决方法
- POJ 2315:Football Game(博弈论)
- tinyhttp源码阅读(注释)