java ftpclient quit_java实现ftp的几种方式(转载)

java实现ftp的几种方式(第3方包)

最近在做ssh

,sftp。顺便总结一下java实现ftp的几种方式。

1、sun.net

import

sun.net.ftp.*;

import

sun.net.*;

import

java.io.FileInputStream;

import

java.io.FileOutputStream;

import

java.io.ByteArrayOutputStream;

import

java.util.ArrayList;

import

java.util.StringTokenizer;

public class

FtpUpfile {

private FtpClient ftpclient;

private String ipAddress;

private int ipPort;

private String userName;

private String PassWord;

public FtpUpfile(String ip, int port, String username, String

password) throws

Exception {

ipAddress = new String(ip);

ipPort = port;

ftpclient = new FtpClient(ipAddress, ipPort);

//ftpclient = new FtpClient(ipAddress);

userName = new String(username);

PassWord = new String(password);

}

public FtpUpfile(String ip, String username, String password)

throws

Exception {

ipAddress = new String(ip);

ipPort = 21;

ftpclient = new FtpClient(ipAddress, ipPort);

//ftpclient = new FtpClient(ipAddress);

userName = new String(username);

PassWord = new String(password);

}

public void login() throws Exception {

ftpclient.login(userName, PassWord);

}

public void logout() throws Exception {

//用ftpclient.closeServer()断开FTP出错时用下更语句退出

ftpclient.sendServer("QUIT\r\n");

int reply = ftpclient.readServerResponse(); //取得服务器的返回信息

}

public void buildList(String pathList) throws Exception {

ftpclient.ascii();

StringTokenizer s = new StringTokenizer(pathList, "/"); //sign

int count = s.countTokens();

String pathName = "";

while (s.hasMoreElements()) {

pathName = pathName + "/" + (String) s.nextElement();

try {

ftpclient.sendServer("XMKD " + pathName + "\r\n");

} catch (Exception e) {

e = null;

}

int reply = ftpclient.readServerResponse();

}

ftpclient.binary();

}

public ArrayList fileNames(String fullPath) throws Exception {

ftpclient.ascii(); //注意,使用字符模式

TelnetInputStream list = ftpclient.nameList(fullPath);

byte[] names = new byte[2048];

int bufsize = 0;

bufsize = list.read(names, 0, names.length); //从流中读取

list.close();

ArrayList namesList = new ArrayList();

int i = 0;

int j = 0;

while (i < bufsize ) {

//char bc = (char) names;

//System.out.println(i + " " +

bc + " : " + (int) names);

//i = i + 1;

if (names[i] == 10) { //字符模式为10,二进制模式为13

//文件名在数据中开始下标为j,i-j为文件名的长度,文件名在数据中的结束下标为i-1

//System.out.write(names, j, i - j);

//System.out.println(j +

" " + i +

" " + (i - j));

String tempName = new String(names, j, i - j);

namesList.add(tempName);

//System.out.println(temp);

// 处理代码处

//j = i + 2; //上一次位置二进制模式

j = i + 1; //上一次位置字符模式

}

i = i + 1;

}

return namesList;

}

public void upFile(String source, String destination) throws

Exception {

buildList(destination.substring(0,

destination.lastIndexOf("/")));

ftpclient.binary(); //此行代码必须放在buildList之后

TelnetOutputStream ftpOut = ftpclient.put(destination);

TelnetInputStream ftpIn = new TelnetInputStream(new

FileInputStream(source), true);

byte[] buf = new byte[204800];

int bufsize = 0;

while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {

ftpOut.write(buf, 0, bufsize);

}

ftpIn.close();

ftpOut.close();

}

public void upFile(byte[] sourceData, String destination) throws

Exception {

buildList(destination.substring(0,

destination.lastIndexOf("/")));

ftpclient.binary(); //此行代码必须放在buildList之后

TelnetOutputStream ftpOut = ftpclient.put(destination);

ftpOut.write(sourceData, 0, sourceData.length);

// ftpOut.flush();

ftpOut.close();

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值