package com.chinasoft.ge.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.StringTokenizer;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class FtpServer {
private FtpClient ftpClient;
private String FtpIP,FtpUser,FtpPwd;
private int FtpPort = 21;
private Boolean bConnected = false;
public FtpServer(String IP, int port, String user, String pwd){
FtpIP = IP;
FtpPort = port;
FtpUser = user;
FtpPwd = pwd;
ConnectServer(FtpIP, FtpPort, FtpUser, FtpPwd);
}
public Boolean ConnectServer(String IP, int port, String user, String pwd){
try{
//System.out.println(IP + " " + port);
ftpClient = new FtpClient(IP,port);
ftpClient.login(user, pwd);
System.out.println(IP +" FTP��¼�ɹ�!");
bConnected = true;
//System.out.println("|" + IP + "bConnected:" + bConnected);
}
catch(IOException e){
e.printStackTrace();
}
return bConnected;
}
// ��FTP�Ͻ��ļ���
public void CreateDirectory(String path){
//System.out.println("bConnected:" + bConnected);
if(!bConnected) {System.out.println("CreateDirectory:FTP������δl��!"); return;}
try{
//System.out.println("00a");
ftpClient.binary();
//System.out.println("00a");
// ���ļ���
ftpClient.ascii();
StringTokenizer s = new StringTokenizer(path,"/");//"abc4/abc2/abc3", "/"); // sign
//System.out.println(path);
int count = s.countTokens();
//System.out.println(count);
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
//System.out.println(pathName);
try {
ftpClient.sendServer("XMKD " + pathName + "\r\n");
} catch (Exception e) {
e.printStackTrace();
}
int reply = ftpClient.readServerResponse();
}
System.out.println(FtpIP + " FTP���ļ��гɹ�!");
}catch(Exception e){}
}
// FileName ȫ·���ļ���
public byte[] ReadFile(String FileName){
byte rbyts[]=null;
try{
ftpClient.binary();
//System.out.println("00");
TelnetInputStream tis = ftpClient.get(FileName);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//System.out.println("001");
byte[] byts = new byte[1024];
int len;
while ((len = tis.read(byts)) != -1) {
//System.out.println(len);
for(int i = 0; i<len; i++){
baos.write(byts[i]);
}
//baos.write(byts);
//tos.write(bytes, 0, c);
}
//System.out.println("002");
tis.close();
rbyts=baos.toByteArray();
}
catch(Exception e){
e.printStackTrace();
}
return rbyts;
}
// FTP��д�ļ�
public void WriteFile(String FileName, byte[] byts) {
try {
ftpClient.binary();
TelnetOutputStream tos = ftpClient.put(FileName);
tos.write(byts);
tos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* �ϴ��ļ�
* @param localFile �����ļ�
* @param remoteFile Զ���ļ�
*/
public void upload(String localFile, String remoteFile) {
TelnetOutputStream os = null;
FileInputStream is = null;
try {
//��Զ���ļ������������
os = ftpClient.put(remoteFile);
//��ȡ�����ļ���������
File file_in = new File(localFile);
is = new FileInputStream(file_in);
//����һ�����
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("upload success");
} catch (IOException ex) {
System.out.println("not upload");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* �����ļ�
* @param remoteFile Զ���ļ�·��(�������)
* @param localFile �����ļ�·��(�ͻ���)
*/
public void download(String remoteFile, String localFile) {
TelnetInputStream is = null;
FileOutputStream os = null;
try {
//��ȡԶ�̻����ϵ��ļ�filename������TelnetInputStream�Ѹ��ļ����͵����ء�
is = ftpClient.get(remoteFile);
File file_in = new File(localFile);
os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("download success");
} catch (IOException ex) {
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void CloseConnect() {
try {
if(bConnected) ftpClient.closeServer();
System.out.println(FtpIP + " FTP�Ͽ��ɹ�!");
} catch (IOException ex) {
System.out.println(FtpIP + " FTP�Ͽ�Pʧ��");
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("ϵͳ��!");
FtpServer ftp1 = new FtpServer("192.168.0.101", 21, "anonymous", "IEUser@");
byte[] byts = ftp1.ReadFile("/12358(v2.0).ppt");
ftp1.CloseConnect();
FtpServer ftp2 = new FtpServer("192.168.0.102", 21, "anonymous", "IEUser@");
String path2 = "abc1/abc2/abc3";
ftp2.CreateDirectory(path2);
ftp2.WriteFile(path2 + "/" + "abc.ppt", byts);
ftp2.CloseConnect();
}
}