package xxxx;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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; public class FtpUtil { private final static String host = "192.168.xxx.xxxx"; private final static String basePath = "/auth"; private final static String username = ""; private final static String password = ""; public static boolean uploadFile(String localPath) throws IOException {
File file = new File(localPath);
FileInputStream fileInputStream = null;
boolean result = false;
FTPClient ftp = new FTPClient();
try {
//----------------------
//尝试添加超时时间 解决阻塞问题
ftp.setDefaultTimeout(60 * 1000);
ftp.setConnectTimeout(60 * 1000);
ftp.setDataTimeout(60 * 1000);
//----------------------
fileInputStream = new FileInputStream(file);
String fileName = file.getName();
int reply;
ftp.connect(host);// 连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
//230代表登录成功
System.out.println(reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(basePath);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//ftp.setBufferSize(1024);
ftp.setControlEncoding("GBK");
//设置被动模式
ftp.enterLocalPassiveMode();
//设置上传文件的类型为二进制类型
//上传文件 result = ftp.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), fileInputStream);
fileInputStream.close();
ftp.logout();
// result = true;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) { }
}
}
return result; } public static boolean uploadFile(File file) throws IOException {
FileInputStream fileInputStream = null;
boolean result = false;
FTPClient ftp = new FTPClient();
try {
//----------------------
//尝试添加超时时间 解决阻塞问题
ftp.setDefaultTimeout(60 * 1000);
ftp.setConnectTimeout(60 * 1000);
ftp.setDataTimeout(60 * 1000);
//----------------------
fileInputStream = new FileInputStream(file);
String fileName = file.getName();
int reply;
ftp.connect(host);// 连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
//230代表登录成功
System.out.println(reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(basePath);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//ftp.setBufferSize(1024);
ftp.setControlEncoding("GBK");
//设置被动模式
ftp.enterLocalPassiveMode();
//设置上传文件的类型为二进制类型
//上传文件 result = ftp.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), fileInputStream);
fileInputStream.close();
ftp.logout();
// result = true;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) { }
}
}
return result; } public static boolean downloadFile(String remotePath, String fileName, String localPath) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host);
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
System.out.println(reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
ftp.enterLocalPassiveMode();
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) {
String localpath = "D:/ftp测试.txt";
System.out.println(uploadFile(localpath)); System.out.println(downloadFile("/auth", "ftp测试.txt", "D:/log"));
}*/
}

使用的jar包

<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>2.0</version>
</dependency>

FtpUtil 工具类的更多相关文章

  1. FTPUtil工具类

    package com.xxx.common.util; import java.io.File; import java.io.FileOutputStream; import java.io.IO ...

  2. FTP工具类开发

    正所谓工欲善其事必先利其器,熟悉了下一套流程,以此铭记. 1.FTP服务搭建 由于本人使用wondiow系统,所以针对window的童鞋们可以查看.至于windowX这里配置类似,所以不要纠结于win ...

  3. FTP上传-封装工具类

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  4. java中常用的工具类(二)

    下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil           Java   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

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

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

  6. Java Utils工具类大全(转)

    源码和jar见:https://github.com/evil0ps/utils #Java Utils --- 封装了一些常用Java操作方法,便于重复开发利用. 另外希望身为Java牛牛的你们一起 ...

  7. Java操作FTP工具类(实例详解)

    这里使用Apache的FTP jar 包 没有使用Java自带的FTPjar包  工具类 package com.zit.ftp; import java.io.File; import java.i ...

  8. ftp上传下载工具类

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

  9. Java Utils工具类大全

    源码和jar见:https://github.com/evil0ps/utils #Java Utils --- 封装了一些常用Java操作方法,便于重复开发利用. 另外希望身为Java牛牛的你们一起 ...

随机推荐

  1. Asp.Net北大青鸟总结(五)-数据绑定控件

        在前面的博客我已经介绍了关于一个特殊控件也是我们经经常使用到的控件gridview的使用实现真假分页.这也是属于绑定控件的一种使用.那么我们接下来来介绍一下数据绑定这门技术吧!  一.数据绑定 ...

  2. iOSQuart2D绘图之UIImage简单使用

    代码地址如下:http://www.demodashi.com/demo/11609.html 人生得意须尽欢,莫使金樽空对月. 天生我材必有用,千金散尽还复来. 前记 说到UIImage大家都不会感 ...

  3. Java 创建用户异常类、将异常一直向上抛、 throw和throws的区别

    如果java提供的系统异常类型不能满足程序设计的需求,那么可以设计自己的异常类型. 从java异常类的结构层次可以看出,java类型的公共父类为Throwable.在程序运行中可能出现俩种问题:一种是 ...

  4. 用Darwin开发RTSP级联server(拉模式转发)(附源代码)

    源代码下载地址:https://github.com/EasyDarwin orwww.easydarwin.org 在博客 在Darwin进行实时视频转发的两种模式 中,我们描写叙述了流媒体serv ...

  5. CentOS-6.*安装配置SVN

    安装说明 系统环境:CentOS-6.3 安装方式:yum install (源码安装容易产生版本兼容的问题) 安装软件:系统自动下载SVN软件 检查已安装版本 #检查是否安装了低版本的SVN [ro ...

  6. linux中的dd复制命令

    dd命令用于复制文件并对原文件的内容进行转换和格式化处理.dd命令功能很强大的,对于一些比较底层的问题,使用dd命令往往可以得到出人意料的效果.用的比较多的还是用dd来备份裸设备.但是不推荐,如果需要 ...

  7. unity, windows: Unhandled Exception: System.UnauthorizedAccessException: Access to the path "XXX\Temp\Assembly-CSharp.dll.mdb" is denied

    的windows上使用unity,修改过脚本或inspector中的数值后运行编辑器报错: Unhandled Exception: System.UnauthorizedAccessExceptio ...

  8. Ireport常用操作汇总

    1.四则运算 new java.lang.Double(($F{fincome}.doubleValue())/($F{fhomePopulation}.intValue()))

  9. awk 数组

    Arrays        Arrays are subscripted with an expression between square brackets ([ and ]).   If the ...

  10. 封装ShareSDK中的分享功能封以及对类似第三方功能封装的心得【原创】

    本篇的主题有三个: 1.封装思想的介绍 2.我的封装代码 3.我在封装sharesdk(采用的是简洁版本)分享功能是碰到的问题,以及解决方法. PS:其实这个我之前封装过一次,不过最近在重构项目时发现 ...