JAva ftpアップロードダウンロード(apacheプラグイン)



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

/**
 * ftp Client
 * 
 * @author cuiyh
 * @version $Revision: 1.1 $ $Date: 2006/06/02 02:50:54 $
 */

public class FtpClient {
	private String server;

	private String username;

	private String password;

	private FTPClient ftp;

	private boolean binaryTransfer = false;

	private final static Logger log = Logger.getLogger(FtpClient.class);

	/**
	 * @param server
	 *            ftp     
	 * @param username
	 *            ftp       
	 * @param password
	 *            ftp    
	 */
	public FtpClient(String server, String username, String password) {

		this.server = server;
		this.username = username;
		this.password = password;
		ftp = new FTPClient();
		/*
		 * if(Configuration.PrintFTPCommandLog){ //  FTP  
		 * ftp.addProtocolCommandListener(new PrintCommandListener()); }
		 */
	}

	/**
	 *           FtpClient
	 */
	public FtpClient() {
		this(Configuration.FtpServer, Configuration.FtpUser,
				Configuration.FtpPassword);
	}

	public boolean connect() {
		try {
			int reply;
			ftp.connect(server);

			//                  
			reply = ftp.getReplyCode();

			if (FTPReply.isPositiveCompletion(reply)) {
				if (ftp.login(username, password)) {
					//    passive  
					ftp.enterLocalPassiveMode();
					return true;
				}
			} else {
				ftp.disconnect();
				log.error("FTP server refused connection.");
			}
		} catch (IOException e) {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException f) {
				}
			}
			log.error("Could not connect to server.", e);
		}
		return false;
	}

	/**
	 *                
	 * 
	 * @param fileName
	 *                (    )
	 * @param delFile
	 *                      
	 * @return
	 */
	public boolean get(String fileName, boolean delFile) {
		String remote = Configuration.RemoteDownPath + fileName;
		String local = Configuration.LocalDownPath + fileName;
		return get(remote, local, delFile);
	}

	/**
	 *                
	 * 
	 * @param fileName
	 *                (    )
	 * @param delFile
	 *                      
	 * @return
	 */
	public boolean put(String fileName, boolean delFile) {
		String remote = Configuration.RemoteUpPath + fileName;
		String local = Configuration.LocalUpPath + fileName;
		return put(remote, local, delFile);
	}

	/**
	 *                
	 * 
	 * @param fileNames
	 *                 
	 * @param delFile
	 *                     
	 * @return
	 */
	public boolean[] put(String[] fileNames, boolean delFile) {
		boolean[] result = new boolean[fileNames.length];
		for (int j = 0; j < result.length; j++) {
			result[j] = false;
		}
		String remoteFile;
		String localFile;
		for (int i = 0; i < fileNames.length; i++) {
			localFile = fileNames[i];
			result[i] = put(localFile, delFile);
		}
		return result;
	}

	/**
	 *                
	 * 
	 * @param remoteAbsoluteFile
	 *                 (      )
	 * @param localAbsoluteFile
	 *                 (      )
	 * @return    ,  true,    false
	 */
	private boolean put(String remoteAbsoluteFile, String localAbsoluteFile,
			boolean delFile) {
		InputStream input = null;
		try {
			// //        
			if (binaryTransfer) {
				ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			} else {
				ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
			}
			//     
			input = new FileInputStream(localAbsoluteFile);
			ftp.storeFile(remoteAbsoluteFile, input);
			log.debug("put " + localAbsoluteFile);
			input.close();
			if (delFile) {
				(new File(localAbsoluteFile)).delete();
			}
			log.debug("delete " + localAbsoluteFile);
			return true;
		} catch (FileNotFoundException e) {
			log.error("local file not found.", e);
		} catch (IOException e1) {
			log.error("Could put file to server.", e1);
		} finally {
			try {
				if (input != null) {
					input.close();
				}
			} catch (Exception e2) {
			}
		}

		return false;
	}

	/**
	 *                 
	 * 
	 * @param remoteAbsoluteFile
	 *                 (      )
	 * @param localAbsoluteFile
	 *                 (      )
	 * @return    ,  true,    false
	 */
	public boolean get(String remoteAbsoluteFile, String localAbsoluteFile,
			boolean delFile) {
		OutputStream output = null;
		try {
			//         
			if (binaryTransfer) {
				ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			} else {
				ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
			}
			//     
			output = new FileOutputStream(localAbsoluteFile);
			ftp.retrieveFile(remoteAbsoluteFile, output);
			output.close();
			if (delFile) { //       
				ftp.deleteFile(remoteAbsoluteFile);
			}
			return true;
		} catch (FileNotFoundException e) {
			log.error("local file not found.", e);
		} catch (IOException e1) {
			log.error("Could get file from server.", e1);
		} finally {
			try {
				if (output != null) {
					output.close();
				}
			} catch (IOException e2) {
			}
		}
		return false;
	}

	/**
	 *             
	 * 
	 * @param remotePath
	 *                 
	 * @return              ,                 0     
	 */
	public String[] listNames(String remotePath) {
		String[] fileNames = null;
		try {
			FTPFile[] remotefiles = ftp.listFiles(remotePath);
			fileNames = new String[remotefiles.length];
			for (int i = 0; i < remotefiles.length; i++) {
				fileNames[i] = remotefiles[i].getName();
			}

		} catch (IOException e) {
			log.error("Could not list file from server.", e);
		}
		return fileNames;
	}

	/**
	 *   ftp  
	 */
	public void disconnect() {
		try {
			ftp.logout();
			if (ftp.isConnected()) {
				ftp.disconnect();
			}
		} catch (IOException e) {
			log.error("Could not disconnect from server.", e);
		}
	}

	/**
	 * @return Returns the binaryTransfer.
	 */
	public boolean isBinaryTransfer() {
		return binaryTransfer;
	}

	/**
	 * @param binaryTransfer
	 *            The binaryTransfer to set.
	 */
	public void setBinaryTransfer(boolean binaryTransfer) {
		this.binaryTransfer = binaryTransfer;
	}

	public static void main(String[] args) {
		FtpClient ftp = new FtpClient("130.85.51.131", "cbs", "cbs");
		ftp.connect();
		String[] temp = ftp.listNames("/tuxlog/cbs");
		System.out.println("connect sucess");
		System.out.println(temp.length);
		// ftp.put("test.txt", true);
		// System.out.println("upload sucess");

		/*
		 * boolean re = ftp.get("/expert/acc/bill/src/test.txt",
		 * "c://test02.txt", false);
		 * 
		 * if (re) { System.out.println("down sucess"); } else {
		 * System.out.println("down sucess"); }
		 */
		/*
		 * if(ftp.get("test.txt",false)){ System.out.println("down sucess");
		 * }else{ System.out.println("down faile"); }
		 */
		ftp.disconnect();
	}
}


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

/**
 * ftp Client
 * 
 * @author cuiyh
 * @version $Revision: 1.1 $ $Date: 2006/06/02 02:50:54 $
 */

public class FtpClient {
	private String server;

	private String username;

	private String password;

	private FTPClient ftp;

	private boolean binaryTransfer = false;

	private final static Logger log = Logger.getLogger(FtpClient.class);

	/**
	 * @param server
	 *            ftp     
	 * @param username
	 *            ftp       
	 * @param password
	 *            ftp    
	 */
	public FtpClient(String server, String username, String password) {

		this.server = server;
		this.username = username;
		this.password = password;
		ftp = new FTPClient();
		/*
		 * if(Configuration.PrintFTPCommandLog){ //  FTP  
		 * ftp.addProtocolCommandListener(new PrintCommandListener()); }
		 */
	}

	/**
	 *           FtpClient
	 */
	public FtpClient() {
		this(Configuration.FtpServer, Configuration.FtpUser,
				Configuration.FtpPassword);
	}

	public boolean connect() {
		try {
			int reply;
			ftp.connect(server);

			//                  
			reply = ftp.getReplyCode();

			if (FTPReply.isPositiveCompletion(reply)) {
				if (ftp.login(username, password)) {
					//    passive  
					ftp.enterLocalPassiveMode();
					return true;
				}
			} else {
				ftp.disconnect();
				log.error("FTP server refused connection.");
			}
		} catch (IOException e) {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException f) {
				}
			}
			log.error("Could not connect to server.", e);
		}
		return false;
	}

	/**
	 *                
	 * 
	 * @param fileName
	 *                (    )
	 * @param delFile
	 *                      
	 * @return
	 */
	public boolean get(String fileName, boolean delFile) {
		String remote = Configuration.RemoteDownPath + fileName;
		String local = Configuration.LocalDownPath + fileName;
		return get(remote, local, delFile);
	}

	/**
	 *                
	 * 
	 * @param fileName
	 *                (    )
	 * @param delFile
	 *                      
	 * @return
	 */
	public boolean put(String fileName, boolean delFile) {
		String remote = Configuration.RemoteUpPath + fileName;
		String local = Configuration.LocalUpPath + fileName;
		return put(remote, local, delFile);
	}

	/**
	 *                
	 * 
	 * @param fileNames
	 *                 
	 * @param delFile
	 *                     
	 * @return
	 */
	public boolean[] put(String[] fileNames, boolean delFile) {
		boolean[] result = new boolean[fileNames.length];
		for (int j = 0; j < result.length; j++) {
			result[j] = false;
		}
		String remoteFile;
		String localFile;
		for (int i = 0; i < fileNames.length; i++) {
			localFile = fileNames[i];
			result[i] = put(localFile, delFile);
		}
		return result;
	}

	/**
	 *                
	 * 
	 * @param remoteAbsoluteFile
	 *                 (      )
	 * @param localAbsoluteFile
	 *                 (      )
	 * @return    ,  true,    false
	 */
	private boolean put(String remoteAbsoluteFile, String localAbsoluteFile,
			boolean delFile) {
		InputStream input = null;
		try {
			// //        
			if (binaryTransfer) {
				ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			} else {
				ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
			}
			//     
			input = new FileInputStream(localAbsoluteFile);
			ftp.storeFile(remoteAbsoluteFile, input);
			log.debug("put " + localAbsoluteFile);
			input.close();
			if (delFile) {
				(new File(localAbsoluteFile)).delete();
			}
			log.debug("delete " + localAbsoluteFile);
			return true;
		} catch (FileNotFoundException e) {
			log.error("local file not found.", e);
		} catch (IOException e1) {
			log.error("Could put file to server.", e1);
		} finally {
			try {
				if (input != null) {
					input.close();
				}
			} catch (Exception e2) {
			}
		}

		return false;
	}

	/**
	 *                 
	 * 
	 * @param remoteAbsoluteFile
	 *                 (      )
	 * @param localAbsoluteFile
	 *                 (      )
	 * @return    ,  true,    false
	 */
	public boolean get(String remoteAbsoluteFile, String localAbsoluteFile,
			boolean delFile) {
		OutputStream output = null;
		try {
			//         
			if (binaryTransfer) {
				ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			} else {
				ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
			}
			//     
			output = new FileOutputStream(localAbsoluteFile);
			ftp.retrieveFile(remoteAbsoluteFile, output);
			output.close();
			if (delFile) { //       
				ftp.deleteFile(remoteAbsoluteFile);
			}
			return true;
		} catch (FileNotFoundException e) {
			log.error("local file not found.", e);
		} catch (IOException e1) {
			log.error("Could get file from server.", e1);
		} finally {
			try {
				if (output != null) {
					output.close();
				}
			} catch (IOException e2) {
			}
		}
		return false;
	}

	/**
	 *             
	 * 
	 * @param remotePath
	 *                 
	 * @return              ,                 0     
	 */
	public String[] listNames(String remotePath) {
		String[] fileNames = null;
		try {
			FTPFile[] remotefiles = ftp.listFiles(remotePath);
			fileNames = new String[remotefiles.length];
			for (int i = 0; i < remotefiles.length; i++) {
				fileNames[i] = remotefiles[i].getName();
			}

		} catch (IOException e) {
			log.error("Could not list file from server.", e);
		}
		return fileNames;
	}

	/**
	 *   ftp  
	 */
	public void disconnect() {
		try {
			ftp.logout();
			if (ftp.isConnected()) {
				ftp.disconnect();
			}
		} catch (IOException e) {
			log.error("Could not disconnect from server.", e);
		}
	}

	/**
	 * @return Returns the binaryTransfer.
	 */
	public boolean isBinaryTransfer() {
		return binaryTransfer;
	}

	/**
	 * @param binaryTransfer
	 *            The binaryTransfer to set.
	 */
	public void setBinaryTransfer(boolean binaryTransfer) {
		this.binaryTransfer = binaryTransfer;
	}

	public static void main(String[] args) {
		FtpClient ftp = new FtpClient("130.85.51.131", "cbs", "cbs");
		ftp.connect();
		String[] temp = ftp.listNames("/tuxlog/cbs");
		System.out.println("connect sucess");
		System.out.println(temp.length);
		// ftp.put("test.txt", true);
		// System.out.println("upload sucess");

		/*
		 * boolean re = ftp.get("/expert/acc/bill/src/test.txt",
		 * "c://test02.txt", false);
		 * 
		 * if (re) { System.out.println("down sucess"); } else {
		 * System.out.println("down sucess"); }
		 */
		/*
		 * if(ftp.get("test.txt",false)){ System.out.println("down sucess");
		 * }else{ System.out.println("down faile"); }
		 */
		ftp.disconnect();
	}
}