java编写ftp下载工具
作者:hebedich 发布时间:2022-03-15 08:15:13
标签:java,ftp,下载工具
需要用到 java 写一个 ftp 的工具,因为只有一点点 java 基础,但是由于好几年不用,几乎算是不会了,只好一点点来搞,还好能捡起来。
不过因为是在 Linux 下使用 javac 编译,不是在 WIN 下使用 IDE 来做这些事情,所以在运行和编译上又费了一些时间,不过正是因为这样对 JAVA 的一些编译、运行的知识又了解了一些。
对于 ftp 下载工具,代码如下:
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 java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpClient {
private String host;
private int port;
private String username;
private String password;
private boolean binaryTransfer = true;
private boolean passiveMode = true;
private String encoding = "UTF-8";
private int clientTimeout = 3000;
private boolean flag=true;
private FTPClient ftpClient = null;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isBinaryTransfer() {
return binaryTransfer;
}
public void setBinaryTransfer(boolean binaryTransfer) {
this.binaryTransfer = binaryTransfer;
}
public boolean isPassiveMode() {
return passiveMode;
}
public void setPassiveMode(boolean passiveMode) {
this.passiveMode = passiveMode;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public int getClientTimeout() {
return clientTimeout;
}
public void setClientTimeout(int clientTimeout) {
this.clientTimeout = clientTimeout;
}
public FtpClient(String Host) {
this.username = "anonymous";
this.encoding = "utf-8";
this.binaryTransfer = true;
this.binaryTransfer = true;
this.port = 21;
this.host = Host;
try {
this.ftpClient = getFTPClient();
} catch (Exception e) {
System.out.println("Create FTPClient error!");
}
}
private FTPClient getFTPClient() throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding(encoding);
connect(ftpClient);
if (passiveMode) {
ftpClient.enterLocalPassiveMode();
}
setFileType(ftpClient);
try {
ftpClient.setSoTimeout(clientTimeout);
} catch (SocketException e) {
throw new IOException("Set timeout error.", e);
}
return ftpClient;
}
private void setFileType(FTPClient ftpClient) throws IOException {
try {
if (binaryTransfer) {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
} else {
ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
}
} catch (IOException e) {
throw new IOException("Could not to set file type.", e);
}
}
public boolean connect(FTPClient ftpClient) throws IOException {
try {
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
if (ftpClient.login(username, password)) {
setFileType(ftpClient);
return true;
}
} else {
this.ftpClient.disconnect();
throw new IOException("FTP server refused connection.");
}
} catch (IOException e) {
if (this.ftpClient.isConnected()) {
try {
this.ftpClient.disconnect();
} catch (IOException e1) {
throw new IOException("Could not disconnect from server.", e);
}
}
throw new IOException("Could not connect to server.", e);
}
return false;
}
private void disconnect() throws IOException {
try {
this.ftpClient.logout();
} catch (IOException e) {
System.out.println("logout may timeout!");
} finally {
if (this.ftpClient.isConnected()) {
this.ftpClient.disconnect();
}
}
}
public InputStream getStream(String serverFile) throws IOException {
InputStream inStream = null;
try {
inStream = this.ftpClient.retrieveFileStream(serverFile);
System.out.println("inStream get over!");
return inStream;
} catch (IOException e) {
System.out.println("get stream exception");
return null;
}
}
public boolean writeStream(InputStream input, String localFile) throws IOException {
FileOutputStream fout = new FileOutputStream(localFile);
int ch = 0;
if(input == null){
System.out.println("input is null");
return false;
}
try {
ch = input.read();
while(ch != -1){
fout.write(ch);
ch = input.read();
}
System.out.println("write over!");
return flag;
} catch (IOException e) {
throw new IOException("Couldn't get file from server.", e);
}
}
public boolean isExist(String remoteFilePath)throws IOException{
try{
File file=new File(remoteFilePath);
String remotePath=remoteFilePath.substring(0,(remoteFilePath.indexOf(file.getName())-1));
String[] listNames = this.ftpClient.listNames(remotePath);
System.out.println(remoteFilePath);
for(int i=0;i<listNames.length;i++){
System.out.println(listNames[i]);
if(remoteFilePath.equals(listNames[i])){
flag=true;
System.out.println("file:"+file.getName()+" existed");
break;
}else {
flag=false;
}
}
} catch (IOException e) {
throw new IOException("FILE EXCEPTION", e);
}
return flag;
}
//main for testing
public static void main(String[] args) throws IOException {
String hostname = "cp01-testing-ps7130.cp01.baidu.com";
String serverFile="/home/work/check_disk.sh";
String localFile="/home/work/workspace/project/dhc2-0/dhc/base/ftp/task_get";
FtpClient ftp = new FtpClient(hostname);
System.out.println(ftp.isExist(serverFile));
ftp.writeStream(ftp.getStream(serverFile), localFile);
ftp.disconnect();
}
}
这个工具是为了配合另外一个 Hadoop 工具做 集群上传用的,所以里面的把 input 和 output 流分开了,也是为了方便另外一个工具使用。
补充一点,如何在 linux 配置运行:
如果这样的代码需要在 linux 下环境运行,首先要配置好响应的包,例如
import org.apache.commons.net.ftp.FTPClient;
这个包在 apache 的网站上直接下载就行,解压后找到对应的 jar 包,在编译的时候进行引用:
export FTPPATH="${路径}/xxx.jar"
javac -classpath $CLASSPATH:$FTPPATH FtpClient.java
同样,在运行的时候也要指定 classpath:
java -classpath $CLASSPATH:$FTPPATH FtpClient
建议不要把$FTPPATH 包含在 CLASSPATH 中,用什么包就引用什么环境变量就行了,没必要一股脑都添加进去,就像我们没必要 import 所有的包一样。
以上所述就是本文的全部内容了,希望能够对大家学习java有所帮助。
请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!


猜你喜欢
- 最小二乘法拟合曲线成直线效果拟合前拟合后传入X轴和Y轴的数据,得到新的Y轴数据。将X值数据和拟合后的Y轴数据绑带即可。/// <sum
- 实际的项目开发当中,经常需要根据实际的需求来自定义AlertDialog。最近在开发一个WIFI连接的功能,点击WIFI需要弹出自定义密码输
- Struts2 * Struts2 * 的概念和Spring Mvc * 一样。1.Struts2 * 是在访问某个Action或Actio
- 本文实例讲述了C#记录消息到日志文件的方法。分享给大家供大家参考。具体实现方法如下:public void LogMessageToFile
- 前言制作无边框窗口时,系统自带阴影会消失,这时就需要我自己给窗口添加阴影以防止窗口融入背景。添加阴影的方法很简单,直接用effect就可以了
- 背景最近让我做一个大数据的系统,分析了一下,麻烦的地方就是多数据源切换抽取数据。考虑到可以跨服务器跨数据库抽数,再整理数据,就配置了这个动态
- String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要
- 前言什么时候需要重处理?在实际工作中,重处理是一个非常常见的场景,比如:发送消息失败,调用远程服务失败,争抢锁失败,等等,这些错误可能是因为
- 公司有一个需求,实现一个多级的树形菜单,并且支持多选功能,实现这个功能之前,我在网上找了找,树形菜单很好找,但是支持多选功能并没有很合适的,
- 前言之前做移动端开发,都不清楚WebService是啥东东,现在接触c#,项目中有三处WebService调用,就不得不与其打交道了,最近碰
- 嵌套滚动介绍我们知道 NestedScrolling(Parent/Child) 这对接口是用来实现嵌套滚动的,一般实现这对接口的 Pare
- 代码很简单,功能也很简单,这里就不多废话了#include<stdio.h>int main(){ char ku[16]={&
- 简介:任务并行库(Task Parellel Library)是BCL的一个类库,极大的简化了并行编程。使用任务并行库执行循环C#当中我们一
- 有很多应用场景,用到了接口动态实现,下面举几个典型的应用:1、mybatis / jpa 等orm框架,可以在接口上加注解进行开发,不需要编
- 苹果的iphone有语音识别用的是Google的技术,做为Google力推的Android 自然会将其核心技术往Android 系统里面植入
- 关于MouseWheelListener的鼠标滚轮事件Java中JPanel面板中对鼠标滚轮事件的处理。一、MouseWheelListen
- 当数据量比较大的时候,我们就需要考虑读写分离了,也就是动态切换数据库连接,对指定的数据库进行操作。在spring中实现动态的切换无非就是利用
- 在项目开发中,经常会碰到日期处理。比如查询中,可能会经常遇到按时间段查询,有时会默认取出一个月的数据。当我们提交数据时,会需要记录当前日期,
- XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(St
- 前言现在APP中用到H5页面的越来越多,而如何正确获取WebView的网页title是必须要考虑的。最近做项目的时候,老大让我把之前做的we