java ftp 详解_java ftp文件上传详解

先我们来看两段简单的代码

将本地文件上传到ftp服务器上,代码如下:

@test

public void testuploadfromdisk(){

try {

fileinputstream in=new fileinputstream(new file("d:/test.txt"));

boolean flag = uploadfile("127.0.0.1", 21, "test", "test", "d:/ftp", "test.txt", in);

system.out.println(flag);

} catch (filenotfoundexception e) {

e.printstacktrace();

}

}

在ftp服务器上生成一个文件,并将一个字符串写入到该文件中

@test

public void testuploadfromstring(){

try {

inputstream input = new bytearrayinputstream("test ftp".getbytes("utf-8"));

boolean flag = uploadfile("127.0.0.1", 21, "test", "test", "d:/ftp", "test.txt", input);

system.out.println(flag);

} catch (unsupportedencodingexception e) {

e.printstacktrace();

}

}

完整实例

import java.io.file;

import it.sauronsoftware.ftp4j.ftpclient;

import it.sauronsoftware.ftp4j.ftpfile;

public class javaftp {

private ftpclient client =null;

public void init(){

try {

client = new ftpclient();

client.connect("192.168.1.248", 21);

client.login("db2inst1", "db2inst1");

} catch (exception e) {

e.printstacktrace();

}

}

public static void main(string[] args) {

try {

javaftp javaftp=new javaftp();

javaftp.init();

javaftp.download("e://test", "/test/");

javaftp.close();

} catch (exception e) {

e.printstacktrace();

}

}

public void download(string localpath,string clientpath){

try {

if(clientpath!=null&&clientpath.length()>=0){

client.changedirectory(clientpath);

}

ftpfile[] list = client.list();

for(int i=0;i

ftpfile file = list[i];

system.out.println(file.gettype());

if(file.gettype()==ftpfile.type_directory){

file temp=new file(localpath+file.separator+file.getname());

if(!temp.exists()){

temp.mkdir();

}

download(localpath+file.separator+file.getname(),file.getname());

}

else if(file.gettype()==ftpfile.type_file){

file localfile=new file(localpath+file.separator+file.getname());

client.download(file.getname(), localfile);

}

}

} catch (exception e) {

e.printstacktrace();

}

}

public void close(){

try {

client.disconnect(true);

} catch (exception e) {

e.printstacktrace();

}

}

}

从ftp服务器下载文件

* @version1.0 jul 27, 2008 5:32:36 pm by 崔红保(cuihongbao@d-heaven.com)创建

* @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 success = false;

ftpclient ftp = new ftpclient();

try {

int reply;

ftp.connect(url, port);

//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接ftp服务器

ftp.login(username, password);//登录

reply = ftp.getreplycode();

if (!ftpreply.ispositivecompletion(reply)) {

ftp.disconnect();

return success;

}

ftp.changeworkingdirectory(remotepath);//转移到ftp服务器目录

ftpfile[] fs = ftp.listfiles();

for(ftpfile ff:fs){

if(ff.getname().equals(filename)){

file localfile = new file(localpath+"/"+ff.getname());

outputstream is = new fileoutputstream(localfile);

ftp.retrievefile(ff.getname(), is);

is.close();

}

}

ftp.logout();

success = true;

} catch (ioexception e) {

e.printstacktrace();

} finally {

if (ftp.isconnected()) {

try {

ftp.disconnect();

} catch (ioexception ioe) {

}

}

}

return success;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值