您的位置:首页 > 编程语言 > Java开发

JavaFTP文件传输 简单实现

2016-12-30 15:38 555 查看
简单介绍下win7 上配置FTP服务和java实现FTP小练习。

如果是win7系统首先开启ftp服务 控制面板->程序->打开关闭windows功能如图:



打开ftp服务,然后开始配置ftp服务站点,打开管理服务,如下图:



选择站点右击 添加FTP站点如图:



设置属性按照下面三个步骤就配置好一个本地ftp服务站点非常之简单 如图:







好了 FTP服务配置好了如何测试一下呢,这里先介绍一个FTP客户端软件,叫做FileZilla Client 简称 fz 一个很强大的FTP客户端官网地址

下载安装很简单就不过多介绍了,看一下安装好了之后连接刚才建好的ftp的站点,因为是创建的匿名站点这里不需要密码,实际根据具情况配置站点。



测试下自己给自己电脑传文件,下载文件吧(感觉傻傻的样子…)。

接下来开始写有用的java连接TFP站点和传输文件的代码。

1.首先jar用的是apache 的工具包 请自行下载



2.俩个文件代码 一个FtpConfig.java 和 FtpUtil.java 实现了上传,文件夹下载,和单文件下载 详情如下均已测试。

FtpConfig.java

/**
*
*/
package FTPDemo;

/**
* @date 2016年12月30日
* @author xie
*
*/
public class FtpConfig {

// 主机ip
private String FtpHost;
// 端口号
private Integer FtpPort;
// ftp用户名
private String FtpUser;
// ftp密码
private String FtpPassword;
// ftp中的目录
private String FtpPath;

public String getFtpHost() {
return FtpHost;

}

public Integer getFtpPort() {
return FtpPort;
}

public void setFtpPort(Integer ftpPort) {
FtpPort = ftpPort;
}

public void setFtpHost(String ftpHost) {
FtpHost = ftpHost;
}

public String getFtpUser() {
return FtpUser;
}

public void setFtpUser(String ftpUser) {
FtpUser = ftpUser;
}

public String getFtpPassword() {
return FtpPassword;
}

public void setFtpPassword(String ftpPassword) {
FtpPassword = ftpPassword;
}

public String getFtpPath() {
return FtpPath;
}

public void setFtpPath(String ftpPath) {
FtpPath = ftpPath;
}

}


FtpUtil.java

/**
*
*/
package FTPDemo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Logger;

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 static FTPClient ftp;

/**
* 获取ftp连接
* @param f
* @return
* @throws Exception
*/
public static boolean connectFtp(FtpConfig f) throws Exception{
ftp=new FTPClient();
boolean flag=false;
if (f.getFtpPort()==null) {
ftp.connect(f.getFtpHost(),21);
}else{
ftp.connect(f.getFtpHost(),f.getFtpPort());
}
ftp.login(f.getFtpUser(), f.getFtpPassword());
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return flag;
}
ftp.changeWorkingDirectory(f.getFtpPath());
flag = true;
return flag;
}

/**
* 关闭ftp连接
*/
public static void closeFtp(){
try {
if (ftp!=null && ftp.isConnected()) {
ftp.logout();
ftp.disconnect();
}
}catch (IOException e){
e.printStackTrace();
}
}

/**
* ftp上传文件
* @param f
* @throws Exception
*/
public static void upload(File f) throws Exception{
if (f.isDirectory()) {
ftp.makeDirectory(f.getName());
ftp.changeWorkingDirectory(f.getName());
String[] files=f.list();
for(String fstr : files){
File file1=new File(f.getPath()+File.separator+fstr);
if (file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2=new File(f.getPath()+File.separator+fstr);
FileInputStream input=new FileInputStream(file2);
ftp.storeFile(file2.getName(),input);
input.close();
}
}
}else{
File file2=new File(f.getPath());
FileInputStream input=new FileInputStream(file2);
ftp.storeFile(file2.getName(),input);
input.close();
}
}

/**
* 下载链接配置
* @param f
* @param localBaseDir 本地目录
* @param remoteBaseDir 远程目录
* @throws Exception
*/
public static void startDownDir(FtpConfig f,String localBaseDir,String remoteBaseDir) throws Exception{
if (FtpUtil.connectFtp(f)) {
try {
FTPFile[] files = null;
boolean changedir = ftp.changeWorkingDirectory(remoteBaseDir);
if (changedir) {
ftp.setControlEncoding("UTF-8");
files = ftp.listFiles();
for (int i = 0; i < files.length; i++) {
downloadFile(files[i], localBaseDir, remoteBaseDir);
}
}else{
System.out.println("不存在的相对路径!");
}
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("连接失败");
}

}

public static void startDownFile(FtpConfig f,String localBaseDir,String remoteFilePath) throws Exception{
if (FtpUtil.connectFtp(f)) {
try {
FileOutputStream outputStream = new FileOutputStream(localBaseDir + remoteFilePath);
ftp.retrieveFile(remoteFilePath, outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("连接FTP服务器失败");
}

}

/**
*
* 下载FTP文件
* 当你需要下载FTP文件的时候,调用此方法
* 根据<b>获取的文件名,本地地址,远程地址</b>进行下载
*
* @param ftpFile
* @param relativeLocalPath 下载到本地的绝对路径
* @param relativeRemotePath 要下载的远程ftp服务器相对路径
*/
private  static void downloadFile(FTPFile ftpFile, String relativeLocalPath,String relativeRemotePath) {
if (ftpFile.isFile()) {
if (ftpFile.getName().indexOf("?") == -1) {
OutputStream outputStream = null;
try {
File locaFile= new File(relativeLocalPath+ ftpFile.getName());
//判断文件是否存在,存在则返回  or 直接覆盖
if(locaFile.exists()){
return;
}else{
outputStream = new FileOutputStream(relativeLocalPath+ ftpFile.getName());
ftp.retrieveFile(ftpFile.getName(), outputStream);
outputStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null){
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
String newlocalRelatePath = relativeLocalPath + ftpFile.getName();
String newRemote = relativeRemotePath + ftpFile.getName().toString();
File fl = new File(newlocalRelatePath);
if (!fl.exists()) {
fl.mkdirs();
}
try {
newlocalRelatePath = newlocalRelatePath+File.separator;
newRemote = newRemote+File.separator;
String currentWorkDir = ftpFile.getName().toString();
//System.out.println(currentWorkDir);
boolean changedir = ftp.changeWorkingDirectory(currentWorkDir);
if (changedir) {
FTPFile[] files = null;
files = ftp.listFiles();
for (int i = 0; i < files.length; i++) {
downloadFile(files[i], newlocalRelatePath, newRemote);
}
}
if (changedir){
ftp.changeToParentDirectory();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) throws Exception{
FtpConfig f=new FtpConfig();
f.setFtpHost("192.168.3.100");
f.setFtpPort(21);
f.setFtpUser("anonymous");
f.setFtpPassword("");
// f.setFtpPath("/data1/");//相对路径
FtpUtil.connectFtp(f);
File file = new File("E:\\data1\\physics.txt");

//FtpUtil.upload(file);//把文件上传在ftp上
// FtpUtil.startDownFile(f, "E:/",  "physics.txt");
FtpUtil.startDownDir(f, "E:/data1/",  "/data1/");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 文件传输