springMVC下载FTP上的文件
springMVC下载FTP上的文件
今天没时间写。先上传 一个工具类
工具类
package com.utils; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* ftp 工具类
*
* @date 2016年1月15日
*/
public class FTPUtils { private static final Logger logger = LoggerFactory.getLogger(FTPUtils.class); private static final FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_NT);
public static final String DATE_TIME_FORMAT_TWO = "yyyy-MM-dd HH:mm";
private static final int BUFFER_SIZE = ; static {
config.setDefaultDateFormatStr(DATE_TIME_FORMAT_TWO);
config.setServerTimeZoneId("GMT+8");
config.setServerLanguageCode("zh");
} /**
* 获取ftp 客户端
*
* @param host
* @param port
* @param username
* @param password
*
* @return FTPClient
*/
public static FTPClient client(String host, int port, String username, String password) {
FTPClient client = new FTPClient();
client.configure(config); try {
client.connect(host, port);
client.enterLocalPassiveMode(); int replyCode = client.getReplyCode(); if (FTPReply.isPositiveCompletion(replyCode)) {
client.login(username, password);
replyCode = client.getReplyCode(); if (FTPReply.isPositiveCompletion(replyCode)) {
return client;
} else {
logger.info(String.format("ftp client open faild. replyCode: %d, %s ", replyCode, client.getReplyString()));
client.disconnect();
return null;
}
} else {
logger.info(String.format("ftp client open faild. replyCode: %d, %s ", replyCode, client.getReplyString()));
client.disconnect();
return null;
}
} catch (IOException e) {
logger.error("ftp client open faild", e);
return null;
}
} /**
* ftp 下载远程文件
*
* @param client ftp客户端对象
* @param remoteDirPath 远程目录
* @param remoteFile 远程文件
* @param localDir 本地存储目录(空 使用用户当前目录)
* @param localFile 本地存储名称(空 使用文件原始名称)
*
* @return boolean
*/
public static boolean download(FTPClient client, String remoteDirPath, String remoteFile, String localDir, String localFile) {
try {
boolean dir = client.changeWorkingDirectory(remoteDirPath); boolean result = false; if (dir) { FTPFile[] files = client.listFiles(remoteFile); if (files.length == ) {
FTPFile file = files[]; if (file.isFile()) {
if (StringUtils.isEmpty(localFile))
localFile = file.getName();
if (StringUtils.isEmpty(localDir))
localDir = System.getProperty("user.home"); File localDirFile = new File(localDir); if (!localDirFile.exists())
localDirFile.mkdirs(); StringBuilder sb = new StringBuilder(localDir);
sb.append(File.separator).append(localFile); try (FileOutputStream out = new FileOutputStream(sb.toString())) { client.setBufferSize(BUFFER_SIZE);
client.setFileType(FTPClient.BINARY_FILE_TYPE); result = client.retrieveFile(file.getName(), out);
} catch (IOException e) {
e.printStackTrace();
logger.error("ftp download faild", e);
}
}
}
} return result;
} catch (IOException e) {
e.printStackTrace();
logger.error("ftp download faild", e);
return false;
}
} /**
* ftp 连接断开
*
* @param client ftp 客户端
*/
public static void close(FTPClient client) {
try {
if (!client.isConnected())
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
} }
FTPClient client = FTPUtils.client(ftpHost, ftpPort, ftpUsername, ftpPassword);
FTPUtils.download(client, "/", file + zip, localPath, null);
FTPUtils.close(client);
springMVC下载FTP上的文件的更多相关文章
- Java ftp 上传文件和下载文件
今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...
- FTP上传文件到服务器
一.初始化上传控件. 1.我们这里用dropzone.js作为上传控件,下载地址http://www.dropzonejs.com/ 2.这里我们使用一个div元素作为dropzone载体. < ...
- 再看ftp上传文件
前言 去年在项目中用到ftp上传文件,用FtpWebRequest和FtpWebResponse封装一个帮助类,这个在网上能找到很多,前台使用Uploadify控件,然后在服务器上搭建Ftp服务器,在 ...
- C# FTP上传文件至服务器代码
C# FTP上传文件至服务器代码 /// <summary> /// 上传文件 /// </summary> /// <param name="fileinfo ...
- Ftp上传文件
package net.util.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...
- PHP使用FTP上传文件到服务器(实战篇)
我们在做开发的过程中,上传文件肯定是避免不了的,平常我们的程序和上传的文件都在一个服务器上,我们也可以使用第三方sdk上传文件,但是文件在第三方服务器上.现在我们使用PHP的ftp功能把文件上传到我们 ...
- .net FTP上传文件
FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...
- 通过cmd完成FTP上传文件操作
一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...
- FTP上传文件提示550错误原因分析。
今天测试FTP上传文件功能,同样的代码从自己的Demo移到正式的代码中,不能实现功能,并报 Stream rs = ftp.GetRequestStream()提示远程服务器返回错误: (550) 文 ...
随机推荐
- 第十章:Intent详解
[正文] Intent组件虽然不是四大组件,但却是连接四大组件的桥梁,学习好这个知识,也非常的重要. 一.什么是Intent 1.Intent的概念: Android中提供了Intent机制来协助应用 ...
- Android常见控件— — —Button
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=&qu ...
- HDU1215(筛选法)
题意:求n的所有因子和: 思路:类似于筛选法求素数的思想,只有第一次的时候了解过它的思想,然后就只是用来求素数,思想的运用反而少: 筛选法求素数: int prime() { memset(vis, ...
- Linux线程-互斥锁pthread_mutex_t
在线程实际运行过程中,我们经常需要多个线程保持同步.这时可以用互斥锁来完成任务:互斥锁的使用过程中,主要有pthread_mutex_init,pthread_mutex_destory,pthrea ...
- PyCharm/IntelliJ IDEA Golang开发环境搭建(最方便快捷的GO语言开发环境)
IntelliJ太牛了.为了配置Go语言开发环境,折腾了半天,下IDE(Sublime Text,IntelliJ Idea),然后装Go插件.装Go插件还要下载插件项目源码,编译等等,Sublime ...
- Golang、Php、Python、Java基于Thrift0.9.1实现跨语言调用
目录: 一.什么是Thrift? 1) Thrift内部框架一瞥 2) 支持的数据传输格式.数据传输方式和服务模型 3) Thrift IDL 二.Thrift的官方网站在哪里? 三.在哪里下载?需要 ...
- dubbo 转
http://blog.csdn.net/zhiguozhu/article/details/50517513 背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式 ...
- javascript中的this指向问题
在深入学习JavaScript之后,我们越来越多的会遇到函数或者在对象内部中,对于this的指向问题的疑惑,其实基本上每一个编程语言中都有一个this,这个this的指向都是大同小异,你也可以汉化它的 ...
- c#检测端口是否被占用
当我们要创建一个Tcp/Ip Server connection ,我们需要一个范围在1000到65535之间的端口 . 但是本机一个端口只能一个程序监听,所以我们进行本地监听的时候需要检测端口是否被 ...
- nova boot instance call flow
参考http://www.cnblogs.com/popsuper1982/p/3927390.html