[FTP] apache FTPClient FileUpload FTP檔案上傳範例(using org.apache.commons.net.ftp.FTPClient)
本範例利用org.apache.commons.net.ftp.FTPClient做檔案上傳
程式不多說 看註解吧。
JAR download / maven Repository
http://mvnrepository.com/artifact/commons-net/commons-net/3.3
code:
package smple.ftpfile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
/**
*
* @author nani12a
*/
public class FtpUpload {
static public FTPClient ftp = null;
//server path
static public String server = "XXX.XXX.XXX.XXX";
//account
static public String account = "account";
//password
static public String password = "password";
/**
*
* @param serverFoder FTP上傳路徑 ex.web/files
* @param filePath 檔案實體路徑 ex D:\\sample.xlsx
* @param fileName 檔案名稱 ex sample2.xlsx
* @return true 成功 false 失敗
* @throws IOException
*/
static public boolean ftpUploadFile(String serverFoder, String filePath, String fileName) throws IOException {
boolean status = false;
try {
ftp = new FTPClient();
//建立連線
ftp.connect(server);
//登入
if (!ftp.login(account, password)) {
ftp.logout();
throw new Exception("登入失敗");
}
//取得回應碼
int reply = ftp.getReplyCode();
//登入狀態
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("無回應");
}
//FTP改為被動模式
ftp.enterLocalPassiveMode();
//網站路徑
ftp.changeWorkingDirectory(serverFoder);
//讀取檔案
InputStream input = new FileInputStream(new File(filePath));
//設定二進位檔
ftp.setFileType(ftp.BINARY_FILE_TYPE);
//上傳檔案
ftp.storeFile(fileName, input);
//關閉檔案
input.close();
status = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (ftp != null) {
//登出
ftp.logout();
//關閉連線
ftp.disconnect();
}
}
return status;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
程式不多說 看註解吧。
JAR download / maven Repository
http://mvnrepository.com/artifact/commons-net/commons-net/3.3
code:
package smple.ftpfile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
/**
*
* @author nani12a
*/
public class FtpUpload {
static public FTPClient ftp = null;
//server path
static public String server = "XXX.XXX.XXX.XXX";
//account
static public String account = "account";
//password
static public String password = "password";
/**
*
* @param serverFoder FTP上傳路徑 ex.web/files
* @param filePath 檔案實體路徑 ex D:\\sample.xlsx
* @param fileName 檔案名稱 ex sample2.xlsx
* @return true 成功 false 失敗
* @throws IOException
*/
static public boolean ftpUploadFile(String serverFoder, String filePath, String fileName) throws IOException {
boolean status = false;
try {
ftp = new FTPClient();
//建立連線
ftp.connect(server);
//登入
if (!ftp.login(account, password)) {
ftp.logout();
throw new Exception("登入失敗");
}
//取得回應碼
int reply = ftp.getReplyCode();
//登入狀態
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("無回應");
}
//FTP改為被動模式
ftp.enterLocalPassiveMode();
//網站路徑
ftp.changeWorkingDirectory(serverFoder);
//讀取檔案
InputStream input = new FileInputStream(new File(filePath));
//設定二進位檔
ftp.setFileType(ftp.BINARY_FILE_TYPE);
//上傳檔案
ftp.storeFile(fileName, input);
//關閉檔案
input.close();
status = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (ftp != null) {
//登出
ftp.logout();
//關閉連線
ftp.disconnect();
}
}
return status;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}