ftp 업로드 다운로드 도구 클래스

4098 단어 FTPJava
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.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 * ftp       
 * 

Title: FtpUtil

*

Description:

* @author * @date 2018 7 23 5:25:14 * @version 1.0 */ public class FtpUtil { /** * Description: FTP * @param host FTP hostname * @param port FTP * @param username FTP * @param password FTP * @param basePath FTP * @param filePath FTP 。 :/2018/01/01。 basePath+filePath * @param filename FTP * @param input * @return true, false */ public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream input) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port);// FTP // , ftp.connect(host) FTP ftp.login(username, password);// reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } // if (!ftp.changeWorkingDirectory(basePath+filePath)) { // String[] dirs = filePath.split("/"); String tempPath = basePath; for (String dir : dirs) { if (null == dir || "".equals(dir)) continue; tempPath += "/" + dir; if (!ftp.changeWorkingDirectory(tempPath)) { if (!ftp.makeDirectory(tempPath)) { return result; } else { ftp.changeWorkingDirectory(tempPath); } } } } // ftp.setFileType(FTP.BINARY_FILE_TYPE); // if (!ftp.storeFile(filename, input)) { return result; } input.close(); ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } /** * Description: FTP * @param host FTP hostname * @param port FTP * @param username FTP * @param password FTP * @param remotePath FTP * @param fileName * @param localPath * @return */ public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // , ftp.connect(host) FTP ftp.login(username, password);// reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath);// FTP FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } public static void main(String[] args) { try { FileInputStream in=new FileInputStream(new File("G:\\image\\1.jpg")); boolean flag = uploadFile("123.207.74.170", 21, "ftpuser", "19970921", "/home/ftpuser/www/images","/2015/01/21", "1.jpg", in); System.out.println(flag); } catch (FileNotFoundException e) { e.printStackTrace(); } } }

좋은 웹페이지 즐겨찾기