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. asp.net导出word(word2007)

    1.只能导出成word2007格式(.docx),可直接导出到客户端 2.服务器上不需要装任何东西,也没有权限限制,比较适合导出表格(支持图片) 3.需要一个国外的DocX.dll插件 4.需要添加引 ...

  2. Jmeter—7 测试中使用到的定时器和逻辑控制器

    1 测试中提交数据有延时1min,所以查询数据是否提交成功要设置定时器. 固定定时器页面:单位是毫秒 [dinghanhua] 2 集合点.Synchronizing Timer 集合点编辑:集合用户 ...

  3. 利用Scrollow写一个下拉刷新

    利用scrollView滑动的2个监听方法实现 //滑动结束时候 出发的方法 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView ...

  4. Objective C类方法load和initialize的区别

    Objective C类方法load和initialize的区别   过去两个星期里,为了完成一个工作,接触到了NSObject中非常特别的两个类方法(Class Method).它们的特别之处,在于 ...

  5. UIImagePickerController 获取相片视频

    1.UIImagePickerController的静态方法: imagepicker = [[UIImagePickerController alloc]init];    //UIImagePic ...

  6. 如何解决xx列不在表中

    在连接数据库的程序中常会出现xx列不在表中的问题?那么应该怎么解决呢? 产生此问题的原因有三种: 1.数据表没这个字段2.sql查询没将这个字段查出来3.字段名写错了 还有重要的是一定要检查你的数据库 ...

  7. Map/Reduce 工作机制分析 --- 作业的执行流程

    前言 从运行我们的 Map/Reduce 程序,到结果的提交,Hadoop 平台其实做了很多事情. 那么 Hadoop 平台到底做了什么事情,让 Map/Reduce 程序可以如此 "轻易& ...

  8. 数据库update死锁

    比较常见的死锁场景,并发批量update时的一个场景: update cross_marketing set gmtModified = NOW(), pageview = pageview+ #ex ...

  9. Android Studio Lint 自动检查清除冗余资源

    (Android Lint) 辛苦的花了很长的周期 做完了项目. 但是打包完发现 APK 的大小让你瞠目结舌 是不是甚至连自己不知道哪里拷贝过来了代码 和 代码相关的布局文件 资源等, 哪些被使用 哪 ...

  10. Toolkit.getImage获取图片

    public class Img { private static final Toolkit tk = Toolkit.getDefaultToolkit(); public static fina ...