JavaでのFTPの操作(ファイルのアップロード、ファイルのダウンロード、ファイルの削除......)


最近プロジェクトの中の1つの机能は2つのシステムの中でファイルがFTPの中で操作することを必要として、だからFTPに対して少し方法のカプセル化をして、记录します
実環境BOSSシステムと業務プラットフォームの照合の使用.
業務の簡単な説明:BOSSシステムは帳簿の報告文を業務プラットフォームに発送して、業務プラットフォームは報告文の内容によって業務プラットフォームの中で必要とする提供するデータを検索してファイルに書き込んで、そしてFTPサーバーにアップロードして、BOSSシステムのダウンロードに供する
   
    1.まず、ファイルへの書き込み
構想、業務要求ファイルサイズはデフォルト500 M最大2 Gなので、書き込みの効率を高めるには以下の書き方を採用する
  
public class FtpFile {
	private final static Log logger = LogFactory.getLog(FtpFile.class);
	BufferedReader bufferedReader;
	BufferedWriter bufferedWriter;
	InputStreamReader reader;

	/**
	 *       
	 * 
	 * @param filePath        
	 * @param content         
	 * @return
	 */
	public boolean string2File(String content, String filePath) {
		boolean flag = true;
		try {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			bufferedReader = new BufferedReader(new StringReader(content));
			bufferedWriter = new BufferedWriter(new FileWriter(file));
			char buffer[] = new char[1024];
			int len;
			while ((len = bufferedReader.read(buffer)) != -1) {
				bufferedWriter.write(buffer, 0, len);
			}
			bufferedWriter.flush();
			bufferedReader.close();
			bufferedWriter.close();
		} catch (IOException e) {
			logger.error(e);
			flag = false;
			return flag;
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					logger.error(e);
				}
			}
		}
		return flag;
	}
}
   

指定したファイルに文字列を書き込む方法です.

	/**
	 *       
	 * @param filePath        
	 * @param encoding             
	 * @return
	 */
	public String file2String(String filePath, String encoding) {
		StringWriter writer = new StringWriter();
		File file = new File(filePath);
		try {
			if (encoding == null || "".equals(encoding.trim())) {
				reader = new InputStreamReader(new FileInputStream(file));
			} else {
				reader = new InputStreamReader(new FileInputStream(file), encoding);
			}
			//          
			char[] buffer = new char[1024];
			int n = 0;
			while (-1 != (n = reader.read(buffer))) {
				writer.write(buffer, 0, n);
			}
		} catch (Exception e) {
			logger.error(e);
			return null;
		} finally {
			if (reader != null)
				try {
					reader.close();
				} catch (IOException e) {
					logger.error(e);
				}
		}
		//       
		if (writer != null) {
			return writer.toString();
		} else {
			return null;
		}
	}




上の方法で文字コードを入力するのは、中国語の文字化けしを防ぐためです.
ファイルの読み書きの問題はすべてすでに解決しましたそれでは今FTPに対する操作で、サーバーとしてきっとそれに対して接続します
以下にFTPの基本動作のパッケージを示す

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class FtpBusiness {
	private final static Log logger = LogFactory.getLog(FtpBusiness.class);

	/**
	 *     
	 * 
	 * @param IP FTP     
	 * @param userName FTP      
	 * @param passWord FTP     
	 * @return
	 * @throws Exception
	 */
	public FtpClient ftpConnection(String IP, String userName, String passWord)
			throws Exception {
		FtpClient fc = null;
		try {
			fc = new FtpClient();
			fc.openServer(IP);
			fc.login(userName, passWord);
			fc.binary();
		} catch (Exception e) {
			logger.error(e);
		}
		return fc;
	}

	/**
	 *     
	 * 
	 * @param fc FTP    
	 * @return
	 */
	public boolean ftpClose(FtpClient fc) {
		try {
			fc.closeServer();
		} catch (Exception e) {
			logger.error(e);
			return false;
		}
		return true;
	}

	/**
	 *       
	 * 
	 * @param fc FTP    
	 * @return
	 */
	public String ftpPWD(FtpClient fc){
		try {
			return fc.pwd();
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}

	public void ftpCD(FtpClient fc, String path){
		try {
			fc.cd(path);
		} catch (Exception e) {
			logger.error("FTP      " + path + "  :" + e);
		}
	}

	/**
	 *       
	 * 
	 * @param fc FTP    
	 * @return
	 * @throws Exception
	 */
	public String ftpList(FtpClient fc){
		try {
			TelnetInputStream is = fc.list();
			StringBuffer sb = new StringBuffer();
			int k;
			while ((k = is.read()) != -1) {
				sb.append((char) k);
			}
			is.close();
			return new String(sb.toString().getBytes("iso-8859-1"), "GBK");
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}

	/**
	 *     
	 * 
	 * @param fc FTP    
	 * @param filename        
	 * @return
	 * @throws Exception
	 */
	public InputStream getFile(FtpClient fc, String filename){
		InputStream is = null;
		try {
			fc.binary();
			is = fc.get(filename);
			return is;
		} catch (Exception e) {
			logger.error("    :" + filename + "   ");
			return null;
		}
	}

	/**
	 *     
	 * 
	 * @param fc FTP    
	 * @param filename         
	 * @return
	 * @throws IOException
	 */
	public boolean ftpPut(FtpClient fc, String filename, String Url) {
		FileInputStream is = null;
		TelnetOutputStream os = null;

		try {

			os = fc.put(filename);
			File file_in = new File(Url);
			is = new FileInputStream(file_in);
			byte[] bytes = new byte[1024];
			int c;
			while ((c = is.read(bytes)) != -1) {
				os.write(bytes, 0, c);
			}
		} catch (IOException ex) {
			logger.error(ex);
			return false;
		} finally {
			try {
				is.close();
				os.close();
			} catch (Exception e) {
				logger.error(e);
			}

		}
		return true;
	}
	/**
	 *     
	 * 
	 * @param fc FTP    
	 * @param filename        
	 * @return
	 */
	public boolean ftpDelete(FtpClient fc, String filename) {
		try {
			fc.cd(ftpPWD(fc));
		} catch (IOException e) {
			logger.error(e);
			return false;
		} 
		fc.sendServer("dele " + filename + "\r
"); try { fc.readServerResponse(); } catch (IOException e) { logger.error(e); } return true; } }

上记はFTPの操作推定に上记の内容があってFTPをする时基本的に1つの概念が不足してアップロードとダウンロードと削除が时间の関系でロットの実现をしていないことを提供しました.
後に小例をつける