Java上傳文件FTP服務(wù)器代碼實(shí)例
FTP服務(wù)器(File Transfer Protocol Server)是在互聯(lián)網(wǎng)上提供文件存儲(chǔ)和訪問服務(wù)的計(jì)算機(jī),它們依照FTP協(xié)議提供服務(wù)。 FTP是File Transfer Protocol(文件傳輸協(xié)議)。顧名思義,就是專門用來傳輸文件的協(xié)議。簡單地說,支持FTP協(xié)議的服務(wù)器就是FTP服務(wù)器。
在實(shí)際的應(yīng)用中,通常是通過程序來進(jìn)行文件的上傳。
1.實(shí)現(xiàn)java上傳文件到ftp服務(wù)器中
2.新建maven項(xiàng)目
添加依賴
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version></dependency>
3.實(shí)例代碼:
package com.test.fto.demo;/**ftp鏈接常量*/public class Ftp {private String ipAddr;//ip地址private Integer port;//端口號(hào)private String userName;//用戶名private String pwd;//密碼private String path;//aaa路徑public String getIpAddr() {return ipAddr;}public void setIpAddr(String ipAddr) {this.ipAddr = ipAddr;}public Integer getPort() {return port;}public void setPort(Integer port) {this.port = port;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getPath() {return path;}public void setPath(String path) {this.path = path;}}
測試代碼:
package com.test.fto.demo;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;import org.testng.annotations.Test;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class FtpUtil {private static FTPClient ftp;/** * 獲取ftp連接 * * @param f * @return * @throws Exception */public static boolean connectFtp(Ftp f) throws Exception { ftp = new FTPClient(); boolean flag = false; int reply; if (f.getPort() == null) { ftp.connect(f.getIpAddr(), 21); } else { ftp.connect(f.getIpAddr(), f.getPort()); } ftp.login(f.getUserName(), f.getPwd()); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return flag; } ftp.changeWorkingDirectory(f.getPath()); flag = true; return flag;}/** * 關(guān)閉ftp連接 */public static void closeFtp() { if (ftp != null && ftp.isConnected()) { try { ftp.logout(); ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } }}/** * ftp上傳文件 * * @param f * @throws Exception */public static void upload(File f) throws Exception { if (f.isDirectory()) { ftp.makeDirectory(f.getName()); ftp.changeWorkingDirectory(f.getName()); String[] files = f.list(); for (String fstr : files) { File file1 = new File(f.getPath() + '/' + fstr); if (file1.isDirectory()) {upload(file1);ftp.changeToParentDirectory(); } else {File file2 = new File(f.getPath() + '/' + fstr);FileInputStream input = new FileInputStream(file2);ftp.storeFile(file2.getName(), input);input.close(); } } } else { File file2 = new File(f.getPath()); FileInputStream input = new FileInputStream(file2); ftp.storeFile(file2.getName(), input); input.close(); }}@Testpublic static void test() throws Exception { Ftp f = new Ftp(); f.setIpAddr('your ip'); f.setUserName('username'); f.setPwd('password'); FtpUtil.connectFtp(f); File file = new File('F:/robotium-solo-5.6.1.jar'); FtpUtil.upload(file);//把文件上傳在ftp上 System.out.println('上傳文件完成。。。。');}}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Intellij IDEA官方最完美編程字體Mono使用2. springboot基于Redis發(fā)布訂閱集群下WebSocket的解決方案3. 關(guān)于探究python中sys.argv時(shí)遇到的問題詳解4. 基于android studio的layout的xml文件的創(chuàng)建方式5. CSS自定義滾動(dòng)條樣式案例詳解6. JS繪圖Flot如何實(shí)現(xiàn)動(dòng)態(tài)可刷新曲線圖7. IDEA項(xiàng)目的依賴(pom.xml文件)導(dǎo)入問題及解決8. python使用requests庫爬取拉勾網(wǎng)招聘信息的實(shí)現(xiàn)9. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果10. Java發(fā)送http請(qǐng)求的示例(get與post方法請(qǐng)求)
