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上的文件的更多相关文章

  1. Java ftp 上传文件和下载文件

    今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...

  2. FTP上传文件到服务器

    一.初始化上传控件. 1.我们这里用dropzone.js作为上传控件,下载地址http://www.dropzonejs.com/ 2.这里我们使用一个div元素作为dropzone载体. < ...

  3. 再看ftp上传文件

    前言 去年在项目中用到ftp上传文件,用FtpWebRequest和FtpWebResponse封装一个帮助类,这个在网上能找到很多,前台使用Uploadify控件,然后在服务器上搭建Ftp服务器,在 ...

  4. C# FTP上传文件至服务器代码

    C# FTP上传文件至服务器代码 /// <summary> /// 上传文件 /// </summary> /// <param name="fileinfo ...

  5. Ftp上传文件

    package net.util.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...

  6. PHP使用FTP上传文件到服务器(实战篇)

    我们在做开发的过程中,上传文件肯定是避免不了的,平常我们的程序和上传的文件都在一个服务器上,我们也可以使用第三方sdk上传文件,但是文件在第三方服务器上.现在我们使用PHP的ftp功能把文件上传到我们 ...

  7. .net FTP上传文件

    FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...

  8. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  9. FTP上传文件提示550错误原因分析。

    今天测试FTP上传文件功能,同样的代码从自己的Demo移到正式的代码中,不能实现功能,并报 Stream rs = ftp.GetRequestStream()提示远程服务器返回错误: (550) 文 ...

随机推荐

  1. spirng线程池的配置与使用

    1.在xml中配置线程池 <!-- 配置线程池 --> <bean id="taskExecutor" class="org.springframewo ...

  2. Bubble Sort_树状数组

    Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is th ...

  3. As 和 Is的区别

    首先来说说As是干什么的: 代码: void OnMouseEnter(object sender, MouseEventArgs e){ Ellipse ell = sender as Ellips ...

  4. ctype.h / cctype 中的字符函数

    函数名称 返回值 isalnum() 字母或数字 isalpha() 字母 iscntrl() 控制字符 isdigit() 数字(1 ~ 9) isgraph() 除空格之外的打印字符 islowe ...

  5. IOS学习笔记34—EGOTableViewPullRefresh实现下拉刷新

    移动应用开发中有这么一种场景,就是在列表中显示的数据刷新,有点击刷新按钮刷新的,也有现在最流行的由Twitter首先推出的下拉刷新功能,在IOS中,使用下拉刷新更新UITableView中的数据也用的 ...

  6. php部分---创建连接数据库类

    class DBDA { public $host="localhost"; public $uid="root"; public $pwd="123 ...

  7. java文件下载

    /** * zip 导出 * @param response * @param zipName * @throws Exception */ private void outZip(HttpServl ...

  8. phpwind之关闭账号通

    phpwind的账号通功能早就失效了,但是首页的链接一直存在,造成了很不好的影响 但是后台打开账号通功能又打不开,所以想到了在前端的模板中通过屏蔽这部分代码的方法隐藏掉这个功能在首页的显示 1.打开/ ...

  9. 2016HUAS_ACM暑假集训2J - 今年暑假不AC

    简单的贪心题.把节目的结束时间按升序排列,从第一个节目开始,寻找可以完全看完的节目. 这里用了结构体,所以要自己写排序方式. 总之,贪心的第一要义就是——排序! 本人新手,请体谅. #include& ...

  10. 自动选中div中的文字

    <html> <head> <title></title> <script type="text/javascript" de ...