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) 文 ...
随机推荐
- 读取配置文件 PropertyPlaceholderConfigurer 的配置与使用
public class SpringPropertyConfigurer extends PropertyPlaceholderConfigurer { private static Map< ...
- Psp个人软件开发软件需求分析及用例分析
一.需求分析 1. 业务需求 1.1 应用背景 开发项目进度计划总是那么不明确,延期经常出现,甚至无法给出一个相对比较明确的延迟时间.这样给市场的推广会带来很大的影响,不确定因素使得应对十分困难. ...
- python模块之time
Python中的时间模块. 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素. 2.UTC(Coordinated U ...
- Python 基礎 - 字符編碼
Python 解釋器在加載 .py 文件中的代碼時,會對內容進行編碼 (默認 ascill) ASCII (American Standard Code for Information Interch ...
- Windows 托盘区域显示图标
NOTIFYICONDATA structure 这个结构体包含了向通知区域(底部任务栏右下角区域,下面都称为托盘)显示的信息.需要使用函数Shell_NotifyIcon. 结构体成员 typede ...
- Jena TDB 102
1 Introduction TDB is a RDF storage of Jena. official guarantees and limitations TDB support full ra ...
- codeforces194a
link:http://codeforces.com/contest/334/problem/A 很有意思的一道构造题.发现CF上经常出这种不难但是很不错的构造题. #include <iost ...
- iwork 09 可以用的序列号
E4RJ-YKHP-9ER4-WK9Y-H4UM-5S2G9MU-9U9J-KN4M-4PAE-DYNA-5S2EGMU-3PKP-NN9G-3JW4-3RAD-5S2
- 029. aps.net中DataView中详细信息的跳转显示
点击当前页面的连接, 跳转到另一个页面, 然后进行更新删除等一系列操作 主要代码: <%@ Page Language="C#" AutoEventWireup=" ...
- Squid
事件:由于我们在运维过程中需要升级或安装新的开源软件或组件时,相关的依赖包或基础包非常非常多. 因安全限制,对于没有访问internet权限的服务器,在执行安装或升级过程中就非常容易出错. 所以我们需 ...