import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

import org.apache.commons.net.ftp.FTPReply;

/**

* ftp上传下载工具类

* <p>Title: FtpUtil</p>

* <p>Description: </p>

* <p>Company: www.itcast.com</p>

* @author    入云龙

* @date 2015年7月29日下午8:11:51

* @version 1.0

*/

public class FtpUtil {

/**

* Description: 向FTP服务器上传文件

* @param host FTP服务器hostname

* @param port FTP服务器端口

* @param username FTP登录账号

* @param password FTP登录密码

* @param basePath FTP服务器基础目录

* @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath

* @param filename 上传到FTP服务器上的文件名

* @param input 输入流

* @return 成功返回true,否则返回false

*/

public static boolean uploadFile(String host, int port, String username, String password, String basePath,

String filePath, String filename, InputStream input) {

boolean result = false;

FTPClient ftp = new FTPClient();

try {

int reply;

ftp.connect(host, port);// 连接FTP服务器

// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器

ftp.login(username, password);// 登录

reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) {

ftp.disconnect();

return result;

}

//切换到上传目录

if (!ftp.changeWorkingDirectory(basePath+filePath)) {

//如果目录不存在创建目录

String[] dirs = filePath.split("/");

String tempPath = basePath;

for (String dir : dirs) {

if (null == dir || "".equals(dir)) continue;

tempPath += "/" + dir;

if (!ftp.changeWorkingDirectory(tempPath)) {

if (!ftp.makeDirectory(tempPath)) {

return result;

} else {

ftp.changeWorkingDirectory(tempPath);

}

}

}

}

//设置上传文件的类型为二进制类型

ftp.setFileType(FTP.BINARY_FILE_TYPE);

//上传文件

if (!ftp.storeFile(filename, input)) {

return result;

}

input.close();

ftp.logout();

result = true;

} catch (IOException e) {

e.printStackTrace();

} finally {

if (ftp.isConnected()) {

try {

ftp.disconnect();

} catch (IOException ioe) {

}

}

}

return result;

}

/**

* Description: 从FTP服务器下载文件

* @param host FTP服务器hostname

* @param port FTP服务器端口

* @param username FTP登录账号

* @param password FTP登录密码

* @param remotePath FTP服务器上的相对路径

* @param fileName 要下载的文件名

* @param localPath 下载后保存到本地的路径

* @return

*/

public static boolean downloadFile(String host, int port, String username, String password, String remotePath,

String fileName, String localPath) {

boolean result = false;

FTPClient ftp = new FTPClient();

try {

int reply;

ftp.connect(host, port);

// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器

ftp.login(username, password);// 登录

reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) {

ftp.disconnect();

return result;

}

ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录

FTPFile[] fs = ftp.listFiles();

for (FTPFile ff : fs) {

if (ff.getName().equals(fileName)) {

File localFile = new File(localPath + "/" + ff.getName());

OutputStream is = new FileOutputStream(localFile);

ftp.retrieveFile(ff.getName(), is);

is.close();

}

}

ftp.logout();

result = true;

} catch (IOException e) {

e.printStackTrace();

} finally {

if (ftp.isConnected()) {

try {

ftp.disconnect();

} catch (IOException ioe) {

}

}

}

return result;

}

public static void main(String[] args) {

try {

FileInputStream in=new FileInputStream(new File("D:\\temp\\image\\gaigeming.jpg"));

boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in);

System.out.println(flag);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

FTP上传-封装工具类的更多相关文章

  1. ftp上传下载工具类

    package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...

  2. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...

  3. java ftp上传文件 工具类

    package com.learning.spboot.utils; import com.jcraft.jsch.*; import org.apache.commons.net.ftp.FTPCl ...

  4. FTP上传下载工具(FlashFXP) v5.5.0 中文版

    软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...

  5. (实用篇)PHP ftp上传文件操作类

    <?php /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) */ class class_ftp { public $off; // 返回操作状态(成功/失败) publi ...

  6. JSP图片上传 公共工具类

    需要jsmartcom_zh_CN.jar支持. 下载地址: http://files.cnblogs.com/simpledev/jsmartcom_zh_CN.rar <%@page imp ...

  7. Spring MVC文件上传下载工具类

    import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import ...

  8. java上传文件工具类

    这个是之前整理之前所学时与使用java向邮箱发送邮件一块找到的,一起贴出来供大家参考: import java.awt.image.BufferedImage; import java.io.File ...

  9. SFTP 文件上传下载工具类

    SFTPUtils.java import com.jcraft.jsch.*; import com.jcraft.jsch.ChannelSftp.LsEntry; import lombok.e ...

随机推荐

  1. hihoCoder#1135

    刚开始学习C语言,准备在做hiho的题目的过程中来学习,在此进行记录,如果代码中有错误或者不当的地方还请指正. 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 The c ...

  2. python 中的高级函数reduce()

    reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收 ...

  3. AngularJs的UI组件ui-Bootstrap分享(一)

    最近几个月学习了AngularJs和扩展的UI组件,并在公司小组内做了一次分享交流,感觉很有收获,在此记录下个人的学习心得. 目录: AngularJs的UI组件ui-Bootstrap分享(一) A ...

  4. (转发)centos,redhat 系统为php安装memcached扩展

    转自:http://www.itnose.net/detail/6111623.html 1. 通过yum安装 yum -y install memcached #安装完成后执行: memcached ...

  5. ubuntu 13.04 telnet 详细配置

    1. sudo vi /etc/xinetd.d/telnet并加入以下内容:# default: on# description: The telnet server serves telnet s ...

  6. centos上libreoffice+unoconv安装步骤,实现word转pdf

    一.libreoffice安装 1.yum search  libreoffice查询一下系统自带的安装包 安装libreoffice.x86_64这个就可以了   2.yum install lib ...

  7. oracle 用户创建这个挺靠谱

    CREATE TEMPORARY TABLESPACE test_tempTEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test_temp01.dbf ...

  8. boost和std中的thread的引用参数

    boost 1.60.0 先上代码: #include <boost/thread.hpp> #include <iostream> void add(int &i) ...

  9. 设置table距离顶部位置

    //UIEdgeInsetsMake———— UIEdgeInsets insets = {top, left, bottom, right} self.tableView.contentInset ...

  10. Ubuntu下libpcap安装

    1.首先按下面的博客教程下载和安装四个软件包: 点击打开链接 2.这四个软件都安装好之后按下面教程新建Makefile文件和test.c文件: 点击打开链接 Makefie: all: test.c ...