package com.application;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.StringTokenizer;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class FtpUploadSound_2 {
private FtpClient ftpclient;
private String ipAddress;
private int ipPort;
private String userName;
private String PassWord;
/**
* 构造函数
*
* @param ip
* String 机器IP
* @param port
* String 机器FTP端口号
* @param username
* String FTP用户名
* @param password
* String FTP密码
* @throws Exception
*/
public FtpUploadSound_2(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);
}
/**
* 构造函数
*
* @param ip
* String 机器IP,默认端口为21
* @param username
* String FTP用户名
* @param password
* String FTP密码
* @throws Exception
*/
public FtpUploadSound_2(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);
}
/**
* 登录FTP服务器
*
* @throws Exception
*/
public void login() throws Exception {
ftpclient.login(userName, PassWord);
}
/**
* 退出FTP服务器
*
* @throws Exception
*/
public void logout() throws Exception {
// 用ftpclient.closeServer()断开FTP出错时用下更语句退出
ftpclient.sendServer("QUIT\r\n");
int reply = ftpclient.readServerResponse(); // 取得服务器的返回信息
}
/**
* 在FTP服务器上建立指定的目录,当目录已经存在的情下不会影响目录下的文件,这样用以判断FTP
* 上传文件时保证目录的存在目录格式必须以"/"根目录开头
*
* @param pathList
* String
* @throws Exception
*/
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();
}
/**
* 取得指定目录下的所有文件名,不包括目录名称 分析nameList得到的输入流中的数,得到指定目录下的所有文件名
*
* @param fullPath
* String
* @return ArrayList
* @throws Exception
*/
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 /* names.length */) {
// 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