ftp(テスト)

5722 ワード

詳細
本文は完備を待たなければならない.
 
1、主なコード
package com.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;

public class FTPUtil {
	/**
	 *  ftp  JDK 1.7
	 * 
	 * @param url
	 * @param port
	 * @param username
	 * @param password
	 * @return FtpClient
	 * @throws FtpProtocolException
	 * @throws IOException
	 */
	public static FtpClient connectFTP(String url, int port, String username, String password) { //  ftp
		FtpClient ftp = null;
		try {
			//  
			SocketAddress addr = new InetSocketAddress(url, port);
			//  
			ftp = FtpClient.create();
			ftp.connect(addr);
			//  
			ftp.login(username, password.toCharArray());
			ftp.setBinaryType();
			System.out.println(ftp.getWelcomeMsg());
		} catch (FtpProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ftp;
	}

	/**
	 *  
	 * 
	 * @param ftp
	 * @param path
	 */
	public static void changeDirectory(FtpClient ftp, String path) {
		try {
			ftp.changeDirectory(path);
			System.out.println(ftp.getWorkingDirectory());
		} catch (FtpProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 *  ftp
	 * 
	 * @param ftp
	 */
	public static void disconnectFTP(FtpClient ftp) {
		try {
			ftp.close();
			System.out.println("disconnect success!!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 *  
	 * 
	 * @param localFile
	 * @param ftpFile
	 * @param ftp
	 */
	public static void upload(String localFile, String ftpFile, FtpClient ftp) {
		OutputStream os = null;
		FileInputStream fis = null;
		try {
			//  ftp 。 ftp 
			os = ftp.putFileStream(ftpFile);
			File file = new File(localFile);
			//  
			fis = new FileInputStream(file);
			byte[] bytes = new byte[1024];
			int c;
			while ((c = fis.read(bytes)) != -1) {
				os.write(bytes, 0, c);
			}
			System.out.println("upload success!!");
		} catch (FtpProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (os != null)
					os.close();
				if (fis != null)
					fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 *  
	 * 
	 * @param localFile
	 * @param ftpFile
	 * @param ftp
	 */
	public static void download(String localFile, String ftpFile, FtpClient ftp) {
		InputStream is = null;
		FileOutputStream fos = null;
		try {
			//  ftp 
			is = ftp.getFileStream(ftpFile);
			File file = new File(localFile);
			byte[] bytes = new byte[1024];
			int i;
			fos = new FileOutputStream(file);
			while ((i = is.read(bytes)) != -1) {
				fos.write(bytes, 0, i);
			}
			System.out.println("download success!!");
		} catch (FtpProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fos != null)
					fos.close();
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

 
2、テストコード
package com.testCode;

import java.net.InetAddress;
import java.net.UnknownHostException;
import sun.net.ftp.FtpClient;
import com.ftp.FTPUtil;


public class Test2 {
	public static void main(String[] args) {
		try {
			InetAddress addr = InetAddress.getLocalHost();
			System.out.println(addr.getHostAddress());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String ip = "10.10.10.10";
		int port = 21;
		String username = "root";
		String password = "root";
		String path = "/home";
		//  ftp
		FtpClient ftp = FTPUtil.connectFTP(ip, port, username, password);
		System.out.println(ftp.getWelcomeMsg());
		//  
		FTPUtil.changeDirectory(ftp, path);
		System.out.println("----- ----");
		FTPUtil.upload("D:\\aaa.txt", "/home/aaa.txt", ftp);
		System.out.println("----- ----");
		FTPUtil.download("D:\\aaa.txt", "/home/aaa.txt", ftp);
	}
}