1. 程式人生 > 程式設計 >Java上傳檔案FTP伺服器程式碼例項

Java上傳檔案FTP伺服器程式碼例項

FTP伺服器(File Transfer Protocol Server)是在網際網路上提供檔案儲存和訪問服務的計算機,它們依照FTP協議提供服務。 FTP是File Transfer Protocol(檔案傳輸協議)。顧名思義,就是專門用來傳輸檔案的協議。簡單地說,支援FTP協議的伺服器就是FTP伺服器。

在實際的應用中,通常是通過程式來進行檔案的上傳。

1.實現java上傳檔案到ftp伺服器中

2.新建maven專案

新增依賴

<dependency>
  <groupId>commons-net</groupId>
  <artifactId>commons-net</artifactId>
  <version>3.3</version>
</dependency>

3.例項程式碼:

package com.test.fto.demo;
/**
ftp連結常量
*/
public class Ftp {
	private String ipAddr;
	//ip地址
	private Integer port;
	//埠號
	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;
}

/**
 * 關閉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();
  }
}
@Test
public 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("上傳檔案完成。。。。");
}
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。