⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpclientconn.java

📁 用java开发的ftp工具
💻 JAVA
字号:
package com.hp.gdcc.jddc.dl;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class FTPclientConn {
	public final String host;

	public final String user;

	protected final String password;

	protected URLConnection urlc;

	public FTPclientConn(String _host, String _user, String _password) {
		host = _host;
		user = _user;
		password = _password;
		urlc = null;
	}

	protected URL makeURL(String targetfile) throws MalformedURLException {
		if (user == null)
			return new URL("ftp://" + host + "/" + targetfile + ";type=i");
		else
			return new URL("ftp://" + user + ":" + password + "@" + host + "/"
					+ targetfile + ";type=i");
	}

	protected InputStream openDownloadStream(String targetfile)
			throws Exception {
		URL url = makeURL(targetfile);
		urlc = url.openConnection();
		InputStream is = urlc.getInputStream();
		return is;
	}

	protected OutputStream openUploadStream(String targetfile) throws Exception {
		URL url = makeURL(targetfile);
		urlc = url.openConnection();
		OutputStream os = urlc.getOutputStream();
		return os;
	}

	protected void close() {
		urlc = null;
	}
	public static void main(String args[]) {
		// Usage: FTPupload host, user, password, file
		new FTPupload(args[0], args[1], args[2], args[3], args[4]);
	}
	
}

class FTPupload {
	protected FTPclientConn cconn;

	public final String localfile;

	public final String targetfile;

	public FTPupload(String _host, String _user, String _password,
			String _localfile, String _targetfile) {
		cconn = new FTPclientConn(_host, _user, _password);
		localfile = _localfile;
		targetfile = _targetfile;
		doit();
	}

	public FTPupload(String _host, String _user, String _password, String _file) {
		cconn = new FTPclientConn(_host, _user, _password);
		localfile = _file;
		targetfile = _file;
		doit();
	}

	protected void doit() {
		try {
			OutputStream os = cconn.openUploadStream(targetfile);
			FileInputStream is = new FileInputStream(localfile);
			byte[] buf = new byte[16384];
			int c;
			while (true) {
				//System.out.print(".");
				c = is.read(buf);
				if (c <= 0)
					break;
				//System.out.print("[");
				os.write(buf, 0, c);
				//System.out.print("]");
			}
			os.close();
			is.close();
			cconn.close(); // section 3.2.5 of RFC1738
		} catch (Exception E) {
			System.err.println(E.getMessage());
			E.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -