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. kernel source reading notepad

    __init ,标记内核启动时所用的初始化代码,内核启动完成后就不再使用.其所修饰的内容被放到.init.text section中 __exit,标记模块退出代码,对非模块无效 to be cont ...

  2. Data storage on the batch layer

    4.1 Storage requirements for the master dataset To determine the requirements for data storage, you ...

  3. poj2502 最短路

    //Accepted 504 KB 16 ms //spfa最短路 //把n个地铁站作为n个顶点,边权为从一个站到另一个站的时间 //注意:地铁在相邻的两站之间是直线行驶,但其他的就不是了 #incl ...

  4. SVG DOM常用属性和方法介绍

    将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析器所特有的.SVG支持DOM2标准. 12.2 ...

  5. linux命令:head

    1.命令介绍: head用来显示文件的开头的一部分. 2.命令格式: head [选项] 文件 3.命令参数: -q 隐藏文件名 -v 显示文件名 -c<字节> 显示字节数 -n<行 ...

  6. MVC的过滤器

    过滤器分类: Action过滤器    View结果渲染过滤器     全局错误异常过滤器    身份验证过滤器 1.Action过滤器:在Action执行之前和执行之后分别干一些事   接口:(IA ...

  7. 制作.frameWork的最全最真实的解决办法

    这个制作流程 本博主 已经完全测试成功 我这边 制作的.frameWork 要接入游戏 我们游戏已经上架 所以这个东西完全可以用 http://www.cocoachina.com/bbs/read. ...

  8. 从零开始学习Node.js例子七 发送HTTP客户端请求并显示响应结果

    wget.js:发送HTTP客户端请求并显示响应的各种结果 options对象描述了将要发出的请求.data事件在数据到达时被触发,error事件在发生错误时被触发.HTTP请求中的数据格式通过MIM ...

  9. LPTHW 笨方法学习python 16章

    根据16章的内容作了一些扩展. 比如,判断文件如果存在,就在文件后追加,如不存在则创建. 同时借鉴了shell命令中类似 cat <<EOF > test的方法,提示用户输入一个结尾 ...

  10. Android 开发之拦截EditText的输入内容,定制输入内容

    1.EditText作为一个比较成熟的View,在Android的应用开发中得到极为广泛的使用.在某些特殊情况下,我们可能需要定制EditText的输入内容, 只允许指定功能的输入,例如输入一个”dd ...