上傳文件到ftp(apach方法)
public class FTPConnectorBK {
private String ipAddress;
private int ipPort;
private String userName;
private String PassWord;
/**
* 構造函數
* @param ip String 機器IP
* @param port String 機器FTP端口號
* @param username String FTP用戶名
* @param password String FTP密碼
* @throws Exception
*/
public FTPConnectorBK(String ip, int port, String username, String password)
throws Exception {
ipAddress = new String(ip);
ipPort = port;
userName = new String(username);
PassWord = new String(password);
}
/**
* Description: 向FTP服務器上傳文件
* @param path 本地文件路徑
* @param filename 上傳到FTP服務器上的文件名
* @return 成功返回true,否則返回false
*/
public boolean uploadFile(String path, String filename) {
boolean returnValue = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(ipAddress, ipPort);// 連接FTP服務器
ftp.login(userName, PassWord);// 登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return returnValue;
}
InputStream ftpIn = new FileInputStream(path);
ftp.enterLocalPassiveMode(); //沒有這個 ftp.storeFile(filename, ftpIn)返回false
ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //沒有這個上傳文件大小會變化
ftp.storeFile(filename, ftpIn);
ftpIn.close();
ftp.logout();
returnValue = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return returnValue;
}
public static void main(String[] args){
try {
new FTPConnectorBK("*********", 21, "*****", "********").uploadFile(
"D:\\pps_update_xml\\ppstreamsetup_update.exe", "test.exe");
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String ipAddress;
private int ipPort;
private String userName;
private String PassWord;
/**
* 構造函數
* @param ip String 機器IP
* @param port String 機器FTP端口號
* @param username String FTP用戶名
* @param password String FTP密碼
* @throws Exception
*/
public FTPConnectorBK(String ip, int port, String username, String password)
throws Exception {
ipAddress = new String(ip);
ipPort = port;
userName = new String(username);
PassWord = new String(password);
}
/**
* Description: 向FTP服務器上傳文件
* @param path 本地文件路徑
* @param filename 上傳到FTP服務器上的文件名
* @return 成功返回true,否則返回false
*/
public boolean uploadFile(String path, String filename) {
boolean returnValue = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(ipAddress, ipPort);// 連接FTP服務器
ftp.login(userName, PassWord);// 登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return returnValue;
}
InputStream ftpIn = new FileInputStream(path);
ftp.enterLocalPassiveMode(); //沒有這個 ftp.storeFile(filename, ftpIn)返回false
ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //沒有這個上傳文件大小會變化
ftp.storeFile(filename, ftpIn);
ftpIn.close();
ftp.logout();
returnValue = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return returnValue;
}
public static void main(String[] args){
try {
new FTPConnectorBK("*********", 21, "*****", "********").uploadFile(
"D:\\pps_update_xml\\ppstreamsetup_update.exe", "test.exe");
} catch (Exception e) {
e.printStackTrace();
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.