java ftp client

小 demo


package com.mytool.ftp;

import java.applet.Applet;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import sun.net.ftp.FtpClient;
import netscape.javascript.JSObject;
import netscape.javascript.JSException; //可允许在小程序中处理异常事件

public class FtpTool extends Applet {

	FtpClient ftp = null;

	String user = "";

	String password = "";

	int port = 21;

	String host = "127.0.0.1";

	public FtpTool() {
		ftp = new FtpClient();
	}

	public FtpTool(String ip, int pt, String u, String pwd) {
		host = ip;
		port = pt;
		user = u;
		password = pwd;

		try {
			ftp = new FtpClient();
			ftp.openServer(host, port);
			ftp.login(user, password);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void list() {
		List list = new ArrayList();
		try {

			DataInputStream dis = new DataInputStream(ftp.list());

			String s = "";
			while ((s = dis.readLine()) != null) {
				String str = new String(s.replace(" ", ">").getBytes(
						"iso-8859-1"), "gb2312");
				while (str.indexOf(">>") != -1) {
					str = str.replace(">>", ">");
				}
				String[] item = str.split(">");
				System.out.println(str);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void cd(String dir) {
		try {
			ftp.cd(dir);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void cdUp() {
		try {
			ftp.cdUp();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void disconnect() {
		try {
			ftp.closeServer();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean mkdir(String dir) {

		return false;
	}

	public String readLine() {

		return null;
	}

	public void sendLine(String line) {

	}

	public void callJavaScript(String callBackJavascript) {
		JSObject window = JSObject.getWindow(this); // 获取JavaScript窗口句柄,引用当前文档窗口
		JSObject docment = (JSObject) window.getMember("document");
		JSObject form = (JSObject) docment.getMember("textForm"); // 访问JavaScript
																	// form对象
		JSObject textField = (JSObject) form.getMember("textField");// 访问JavaScript
																	// text对象
		String text = (String) textField.getMember("value"); // 获取文本区的值
		// 调用JavaScript的alert()方法
		window.eval("alert(\"This alert comes from Java!\")");
		window.call(callBackJavascript, null);// 参数用数组的形式表示。
	}

	public static void main(String[] args) {

		FtpTool ftp = new FtpTool("127.0.0.1", 21, "wzj", "121");

		ftp.list();

	}

}


你可能感兴趣的:(client)