《急!!!》用java实现FTP服务器上文件下载,上传。《急!!!》

具体要求:下载之后把文件重命名(加个后缀,便于区分,不会重复下载),以后下载的时候要对这个后缀进行过滤。求实现源码。

package com.icss.oa.impdoc.file.quartz;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPListParseException;

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.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
* Ftp客户端工具,提供ftp上传、下载、查看文件列表功能
*/
public class FtpClientUtil {
private FTPClient ftpClient;

public FtpClientUtil(String host, String user, String password, int port) {
ftpClient = new FTPClient();
try {
ftpClient.connect(host, port);
ftpClient.login(user, password);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
}

public FtpClientUtil() {
// TODO Auto-generated constructor stub
}

/**
* 验证是否登录成功
*
* @return
*/
public boolean isAuthenticated() {
return ftpClient.isAuthenticated();
}

/**
* 下载文件
* @param resFilePath 下载的文件原来所在的路径
* @param targetFilePath 下载的文件准备存放的目的路径
*/
public InputStream download(String resFilePath, File file) {
InputStream in = null;
try {
if(isAuthenticated()){
ftpClient.setType(FTPClient.TYPE_BINARY);
ftpClient.download(resFilePath, file);
}
} catch (Exception e) {
System.err.println("从FTP下载文件失败:" + e.getMessage());
} finally {
disconnect();
}
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return in;
}

/**
* 获取文件列表
* path 代表查询该路径下的文件或目录
* @return
*/
public FTPFile[] getFileList(String path) {
FTPFile[] list = null;
FTPFile[] rtulist = null; //要返回的文件的列表
int filenum = 0; //记录文件的个数
try {
if(isAuthenticated()){
list = ftpClient.list(path);
if(list==null || list.length==0)
return null;
//首先确定要找的内容的个数
for(int i=0;i<list.length;i++){
if(list[i].getType()==0)
filenum++;
}
rtulist = new FTPFile[filenum];
int j=0;
for(int i=0;i<list.length;i++){
if(list[i].getType()==0){
rtulist[j]=list[i];
j++;
}
}
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
} catch (FTPDataTransferException e) {
e.printStackTrace();
} catch (FTPAbortedException e) {
e.printStackTrace();
} catch (FTPListParseException e) {
e.printStackTrace();
}finally{
disconnect();
}
return rtulist;
}
/**
* 获取当前文件夹下的文件夹列表
* path 代表查询该路径下的文件或目录
* @return
*/

public FTPFile[] getDirectoryList(String path) {
FTPFile[] list = null;
FTPFile[] rtulist = null; //要返回的文件夹的列表
int filenum = 0; //记录文件夹的个数
try {
if(isAuthenticated()){
list = ftpClient.list(path);
if(list==null || list.length==0)
return new FTPFile[0] ;
//首先确定要找的内容的个数
for(int i=0;i<list.length;i++){
if(list[i].getType()==1)
if(!".".equals(list[i].getName()))
if(!"..".equals(list[i].getName()))
filenum++;
}
rtulist = new FTPFile[filenum];
int j=0;
for(int i=0;i<list.length;i++){
if(list[i].getType()==1)
if(!".".equals(list[i].getName()))
if(!"..".equals(list[i].getName())){
rtulist[j]=list[i];
j++;
}
}
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
} catch (FTPDataTransferException e) {
e.printStackTrace();
} catch (FTPAbortedException e) {
e.printStackTrace();
} catch (FTPListParseException e) {
e.printStackTrace();
}finally{
disconnect();
}
return rtulist;
}

public String getCurrentDirectory() {
try {
return ftpClient.currentDirectory();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
return null;
}

public void disconnect(){
try {
ftpClient.disconnect(true);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
}

/**
* 删除文件
*
* @param fileName
*/
public void delete(String fileName) {
try {
ftpClient.deleteFile(fileName);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
} finally {
disconnect();
}
}

public static void main(String[] args) {

DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yy HH:mm");
System.out.println(DATE_FORMAT.format(new Date()));
try {
System.out.println(DATE_FORMAT.parse("09/01/10 03:28 PM"));
} catch (ParseException e) {
e.printStackTrace();
}

FtpClientUtil ftpClientUtil = new FtpClientUtil("127.0.0.1", "verycd", "verycd", 21);
System.out.println(ftpClientUtil.isAuthenticated());
System.out.println(ftpClientUtil.getCurrentDirectory());
System.out.println("文件查询:");
System.out.println("=======================================");
FTPFile[] files = ftpClientUtil.getFileList("/");
if(files!=null){
for (int i = 0; i < files.length; i++) {
FTPFile ftpFile = files[i];
ftpClientUtil = new FtpClientUtil("127.0.0.1", "verycd", "verycd", 21);
ftpClientUtil.download("/" + ftpFile.getName(), new File("d:\\catalogRoot\\test.zip"));
System.out.println(ftpFile.getName() + ":" + ftpFile.getType()+":"+ftpFile.getSize()+":"+ftpFile.getModifiedDate());
}
}else{
System.out.println("该目录下的文件为空!");
}
}
}追问

请问是否支持jdk1.4,第三方jar包有没有?
我的邮箱:jianqiangking@163.com谢谢了。

追答

就是1.4的,遍历什么的都不是1.5方式的。jar有啊。我发给你,采纳吧!

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答