当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现FTP文件的上传和下载功能的实例代码

Java实现FTP文件的上传和下载功能的实例代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

苏州交友,央视知名主持人李平,暗黑破坏神2修改器udietoo

ftp 是file transfer protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于internet上的控制文件的双向传输。同时,它也是一个应用程序(application)。基于不同的操作系统有不同的ftp应用程序,而所有这些应用程序都遵守同一种协议以传输文件。在ftp的使用当中,用户经常遇到两个概念:"下载"(download)和"上传"(upload)。"下载"文件就是从远程主机拷贝文件至自己的计算机上;"上传"文件就是将文件从自己的计算机中拷贝至远程主机上。用internet语言来说,用户可通过客户机程序向(从)远程主机上传(下载)文件。

  首先下载了serv-u将自己的电脑设置为了ftp文件服务器,方便操作。

1.ftp文件的下载(从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 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 ftpapche {
private static ftpclient ftpclient = new ftpclient();
private static string encoding = system.getproperty("file.encoding");
/**
* description: 从ftp服务器下载文件
* 
* @version1.0
* @param url
* ftp服务器hostname
* @param port
* ftp服务器端口
* @param username
* ftp登录账号
* @param password
* ftp登录密码
* @param remotepath
* ftp服务器上的相对路径
* @param filename
* 要下载的文件名
* @param localpath
* 下载后保存到本地的路径
* @return
*/
public static boolean downfile(string url, int port, string username,
string password, string remotepath, string filename,
string localpath) {
boolean result = false;
try {
int reply;
ftpclient.setcontrolencoding(encoding);
/*
* 为了上传和下载中文文件,有些地方建议使用以下两句代替
* new string(remotepath.getbytes(encoding),"iso-8859-1")转码。
* 经过测试,通不过。
*/
// ftpclientconfig conf = new ftpclientconfig(ftpclientconfig.syst_nt);
// conf.setserverlanguagecode("zh");
ftpclient.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接ftp服务器
ftpclient.login(username, password);// 登录
// 设置文件传输类型为二进制
ftpclient.setfiletype(ftpclient.binary_file_type);
// 获取ftp登录应答代码
reply = ftpclient.getreplycode();
// 验证是否登陆成功
if (!ftpreply.ispositivecompletion(reply)) {
ftpclient.disconnect();
system.err.println("ftp server refused connection.");
return result;
}
// 转移到ftp服务器目录至指定的目录下
ftpclient.changeworkingdirectory(new string(remotepath.getbytes(encoding),"iso-8859-1"));
// 获取文件列表
ftpfile[] fs = ftpclient.listfiles();
for (ftpfile ff : fs) {
if (ff.getname().equals(filename)) {
file localfile = new file(localpath + "/" + ff.getname());
outputstream is = new fileoutputstream(localfile);
ftpclient.retrievefile(ff.getname(), is);
is.close();
}
}
ftpclient.logout();
result = true;
} catch (ioexception e) {
e.printstacktrace();
} finally {
if (ftpclient.isconnected()) {
try {
ftpclient.disconnect();
} catch (ioexception ioe) {
}
}
}
return result;
}
/**
* 将ftp服务器上文件下载到本地
* 
*/
public void testdownfile() {
try {
boolean flag = downfile("10.0.0.102", 21, "admin",
"123456", "/", "ip.txt", "e:/");
system.out.println(flag);
} catch (exception e) {
e.printstacktrace();
}
}
public static void main(string[] args) {
ftpapche fa = new ftpapche();
fa.testdownfile();
}
}

2.ftp文件的上传(从本机上传到ftp服务器)

import java.io.file;
import java.io.fileinputstream;
import org.apache.commons.net.ftp.ftpclient;
import org.apache.commons.net.ftp.ftpreply;
public class ftptest_05 {
private ftpclient ftp;
/** 
* 
* @param path 上传到ftp服务器哪个路径下 
* @param addr 地址 
* @param port 端口号 
* @param username 用户名 
* @param password 密码 
* @return 
* @throws exception 
*/ 
private boolean connect(string path,string addr,int port,string username,string password) throws exception {
boolean result = false;
ftp = new ftpclient();
int reply;
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setfiletype(ftpclient.binary_file_type);
reply = ftp.getreplycode();
if (!ftpreply.ispositivecompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeworkingdirectory(path);
result = true;
return result;
}
/** 
* 
* @param file 上传的文件或文件夹 
* @throws exception 
*/ 
private void upload(file file) throws exception{
if(file.isdirectory()){
ftp.makedirectory(file.getname());
ftp.changeworkingdirectory(file.getname());
string[] files = file.list();
for (int i = 0;i < files.length;i++) {
file file1 = new file(file.getpath()+"\\"+files[i] );
if(file1.isdirectory()){
upload(file1);
ftp.changetoparentdirectory();
}else{
file file2 = new file(file.getpath()+"\\"+files[i]);
fileinputstream input = new fileinputstream(file2);
ftp.storefile(file2.getname(), input);
input.close();
}
}
}else{
file file2 = new file(file.getpath());
fileinputstream input = new fileinputstream(file2);
ftp.storefile(file2.getname(), input);
input.close();
}
}
public static void main(string[] args) throws exception{
ftptest_05 t = new ftptest_05();
boolean connflag = t.connect("/", "10.0.0.105", 21, "ls", "123456");
system.out.println("connflag : " + connflag);
file file = new file("d:\\test02");//本机被传文件的地址
system.out.println("file : " + file);
t.upload(file);
system.out.println("upload : " + "ok");
}
}

以上所述是小编给大家介绍的java实现ftp文件的上传和下载功能的实例代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网