您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java怎么實現FTP的上傳與下載功能”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Java怎么實現FTP的上傳與下載功能”吧!
JAVA操作FTP服務器,只需要創建一個FTPClient即可,所有的操作都封裝在FTPClient中,JDK自帶的有FTPClient(sun.net.ftp.FtpClient),也可以用第三方的FTPClient,一般使用apache的FTPClient(org.apache.commons.net.ftp.FTPClient),本文將使用apache的FTPClient,API都大同小異
關鍵依賴:commons-net
對常用操作(上傳、下載)封裝成工具類
package com.day0322; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /** * FTP工具類 * 文件上傳 * 文件下載 */ public class FTPUtil { private static final Logger log = LoggerFactory.getLogger(FTPUtil.class); /** * 設置緩沖區大小4M **/ private static final int BUFFER_SIZE = 1024 * 1024 * 4; /** * 本地字符編碼 **/ private static String LOCAL_CHARSET = "GBK"; /** * UTF-8字符編碼 **/ private static final String CHARSET_UTF8 = "UTF-8"; /** * OPTS UTF8字符串常量 **/ private static final String OPTS_UTF8 = "OPTS UTF8"; /** * FTP協議里面,規定文件名編碼為iso-8859-1 **/ private static final String SERVER_CHARSET = "ISO-8859-1"; private static FTPClient ftpClient = null; /** * 連接FTP服務器 */ private static void login(OaFtp oaFtp) { ftpClient = new FTPClient(); try { ftpClient.connect(oaFtp.getIp(), Integer.valueOf(oaFtp.getPort())); ftpClient.login(oaFtp.getName(), oaFtp.getPwd()); ftpClient.setBufferSize(BUFFER_SIZE); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { closeConnect(); } } catch (Exception e) { log.error("",e); throw new RuntimeException(e); } } /** * 關閉FTP連接 */ private static void closeConnect() { if (ftpClient != null && ftpClient.isConnected()) { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { log.error("",e); } } } /** * FTP服務器路徑編碼轉換 * * @param ftpPath FTP服務器路徑 * @return String */ private static String changeEncoding(String ftpPath) { String directory = null; try { if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(OPTS_UTF8, "ON"))) { LOCAL_CHARSET = CHARSET_UTF8; } directory = new String(ftpPath.getBytes(LOCAL_CHARSET), SERVER_CHARSET); } catch (Exception e) { log.error("",e); } return directory; } /** * 改變工作目錄 * 如果沒有,則創建工作目錄 * @param path */ private static void changeAndMakeWorkingDir(String path) { try { ftpClient.changeWorkingDirectory("/"); path = path.replaceAll("\\\\","/"); String[] path_array = path.split("/"); for (String s : path_array) { boolean b = ftpClient.changeWorkingDirectory(s); if (!b) { ftpClient.makeDirectory(s); ftpClient.changeWorkingDirectory(s); } } } catch (IOException e) { log.error("",e); throw new RuntimeException(e); } } /** * 上傳 * @param oaFtp * @param filename * @param dirPath * @param in * @return */ public static boolean upload (OaFtp oaFtp, String filename, String dirPath, InputStream in) { login(oaFtp); if (!ftpClient.isConnected()) { return false; } boolean isSuccess = false; if (ftpClient != null) { try { if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(OPTS_UTF8, "ON"))) { LOCAL_CHARSET = CHARSET_UTF8; } ftpClient.setControlEncoding(LOCAL_CHARSET); String path = changeEncoding(dirPath); changeAndMakeWorkingDir(path); isSuccess = ftpClient.storeFile(new String(filename.getBytes(), SERVER_CHARSET), in); } catch (Exception e) { log.error("",e); } finally { closeConnect(); } } return isSuccess; } /** * 下載 * @param oaFtp * @param filename * @param dirPath * @param out * @return */ public static void download (OaFtp oaFtp, String filename, String dirPath, FileOutputStream out) { // 登錄 login(oaFtp); if (ftpClient != null) { try { String path = changeEncoding(dirPath); changeAndMakeWorkingDir(path); String[] fileNames = ftpClient.listNames(); if (fileNames == null || fileNames.length == 0) { return; } for (String fileName : fileNames) { String ftpName = new String(fileName.getBytes(SERVER_CHARSET), LOCAL_CHARSET); if (StringUtils.equals(ftpName,filename)) { InputStream in = ftpClient.retrieveFileStream(fileName); IOUtils.copy(in,out); } } } catch (IOException e) { log.error("",e); } finally { closeConnect(); } } } }
1.上傳
2.下載
感謝各位的閱讀,以上就是“Java怎么實現FTP的上傳與下載功能”的內容了,經過本文的學習后,相信大家對Java怎么實現FTP的上傳與下載功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。