首先在电脑上创建一个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("172.25.69.14");
f.setFtpPort(21);
f.setFtpUser("anyone");
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/"); }
}
 

JavaFTP文件传输上传和下载文件的更多相关文章

  1. selenium-Python之进行文件的上传和下载文件

    在利用Selenium进行批量上传文件时,遇到如下的Windows窗口进行上传.下载操作时,可以通过pywinauto进行操作.上传窗口如下 使用pywinauto,需知Windows窗口控件的cla ...

  2. SecureCRT上传和下载文件

    SecureCRT上传和下载文件(下载默认目录) SecureCR 下的文件传输协议有ASCII .Xmodem .Ymodem .Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. ...

  3. 每天一个linux命令(26):用SecureCRT来上传和下载文件

    用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...

  4. Linux--用SecureCRT来上传和下载文件

    SecureCRT下的文件传输协议有以下几种:ASCII.Xmodem.Ymodem.Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. Xmodem:这种古老的传输协议速度较慢,但由 ...

  5. 每天一个linux命令(26)--用SecureCRT来上传和下载文件

    用SSH管理Linux 服务器时经常需要远程与本地之间交互文件,而直接使用 SecureCRT 自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmod ...

  6. 每天一个linux命令(26):用SecureCRT来上传和下载文件(转载自竹子)

    用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...

  7. 用SecureCRT来上传和下载文件

    用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...

  8. 初学Java Web(7)——文件的上传和下载

    文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...

  9. Linux - 通过SecureCRT的rz、sz和sftp实现文件的上传和下载

    目录 1 通过rz/sz命令上传/下载 1.1 安装lrzsz软件 1.2 rz - 上传文件 1.3 sz - 下载文件 2 通过sftp上传/下载文件 2.1 关于SFTP的简介 2.2 SFTP ...

随机推荐

  1. 滚动锚定(Scroll Anchoring)- 让视口内容不再因视口上方 DOM 元素的高度变化而产生跳动

    不知道你有没有经历过这样的场景:当你打开一张“多图杀猫”的页面后,正一张图一张图边滚边看,在你刚准备定睛看某一张图的时候,这张图突然被它上面的内容挤到了视口下方,然后你赶紧把滚动条往下拉,试图追赶这张 ...

  2. DUMP101 企业级电商FE

    需求拆分原则 1. 单个迭代不能太大 2. 需求可交付,功能闭环 3. 成本意识 二八法则 4.  预期价值体现 ……………………………………………………………………………… 做 [直接 git cl ...

  3. java8 按对象属性值排序

    //按id从小到大 List<User> sortUser = list.stream().sorted((u1, u2) -> u1.getId().compareTo(u2.ge ...

  4. 新加坡100M带宽,国内延迟70ms,仅800元

    ▇ 新加坡100M带宽,延迟80msE3_8G_1TB_100M_5IP_800元促:E3_32G_1TB SSD_1200元 ▇ 马来西亚,独享带宽,延迟70msL5630_16G_1TB_15M_ ...

  5. C++设计模式——访问者模式

    访问者模式 在GOF的<设计模式:可复用面向对象软件的基础>一书中对访问者模式是这样说的:表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的 ...

  6. resultset 查询时返回多个相同值

    背景 做个简单的接口开发,拿到的平台比较不理想,好久没重新搭建一个了,正好练练手.用了基础的servlet,maven,logback(log4j不支持格式化,比较烦人),fastjson,druid ...

  7. Exif格式分析

    转载链接: http://blog.csdn.net/simonhehe/article/details/8593354 http://blog.csdn.net/lsiyun/article/det ...

  8. es6 filter() 数组过滤方法总结

    1.创建一个数组,判断数组中是否存在某个值 var newarr = [ { num: , val: 'ceshi', flag: 'aa' }, { num: , val: 'ceshi2', fl ...

  9. js小方法,获取知道公历生日 (‘1992-01-19’),获取阴历生日日期,属相,非简单根据年份判断-----------声明:整理自网络!!

    let lunar = { tg: '甲乙丙丁戊己庚辛壬癸', dz: '子丑寅卯辰巳午未申酉戌亥', number: '一二三四五六七八九十', year: '鼠牛虎兔龙蛇马羊猴鸡狗猪', mont ...

  10. Python判断水仙花数

    水仙花数 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿姆斯特朗数( ...