package jing.upfile;
//java中帶有詳細說明的FTP,使用sun.net.ftp包下的api
import sun.net.ftp.*;
import sun.net.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
FTP遠程命令列表<br>
USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT<br>
PASS PASV STOR REST CWD STAT RMD XCUP OPTS<br>
ACCT TYPE APPE RNFR XCWD HELP XRMD STOU AUTH<br>
REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ<br>
QUIT MODE SYST ABOR NLST MKD XPWD MDTM PROT<br>
在服務器上執行命令,如果用sendServer來執行遠程命令(不能執行本地FTP命令)的話,所有FTP命令都要加上/r/n<br>
ftpclient.sendServer("XMKD /test/bb/r/n"); //執行服務器上的FTP命令<br>
ftpclient.readServerResponse一定要在sendServer後調用<br>
nameList("/test")獲取指目錄下的文件列表<br>
XMKD建立目錄,當目錄存在的情況下再次創建目錄時報錯<br>
XRMD刪除目錄<br>
DELE刪除文件<br>
* <p>Title: 使用JAVA操作FTP服務器(FTP客戶端)</p>
* <p>Description: 上傳文件的類型及文件大小都放到調用此類的方法中去檢測,比如放到前臺JAVASCRIPT中去檢測等
* 針對FTP中的所有調用使用到文件名的地方請使用完整的路徑名(絕對路徑開始)。
* </p>
* <p>Copyright: Copyright (c) 2005</p>
* @author 歐朝敬
* QQ:35712069
* 手機:13873195792
* @version 1.0
*/
public class FtpUpfile {
private FtpClient ftpclient;
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 FtpUpfile(String ip, int port, String username, String password) throws
Exception {
ipAddress = new String(ip);
ipPort = port;
ftpclient = new FtpClient(ipAddress, ipPort);
//ftpclient = new FtpClient(ipAddress);
userName = new String(username);
PassWord = new String(password);
}
/**
* 構造函數
* @param ip String 機器IP,默認端口爲21
* @param username String FTP用戶名
* @param password String FTP密碼
* @throws Exception
*/
public FtpUpfile(String ip, String username, String password) throws
Exception {
ipAddress = new String(ip);
ipPort = 21;
ftpclient = new FtpClient(ipAddress, ipPort);
//ftpclient = new FtpClient(ipAddress);
userName = new String(username);
PassWord = new String(password);
}
/**
* 登錄FTP服務器
* @throws Exception
*/
public void login() throws Exception {
ftpclient.login(userName, PassWord);
}
/**
* 退出FTP服務器
* @throws Exception
*/
public void logout() throws Exception {
//用ftpclient.closeServer()斷開FTP出錯時用下更語句退出
ftpclient.sendServer("QUIT/r/n");
int reply = ftpclient.readServerResponse(); //取得服務器的返回信息
}
/**
* 在FTP服務器上建立指定的目錄,當目錄已經存在的情下不會影響目錄下的文件,這樣用以判斷FTP
* 上傳文件時保證目錄的存在目錄格式必須以"/"根目錄開頭
* @param pathList String
* @throws Exception
*/
public void buildList(String pathList) throws Exception {
ftpclient.ascii();
StringTokenizer s = new StringTokenizer(pathList, "/"); //sign
int count = s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpclient.sendServer("XMKD " + pathName + "/r/n");
} catch (Exception e) {
e = null;
}
int reply = ftpclient.readServerResponse();
}
ftpclient.binary();
}
/**
* 取得指定目錄下的所有文件名,不包括目錄名稱
* 分析nameList得到的輸入流中的數,得到指定目錄下的所有文件名
* @param fullPath String
* @return ArrayList
* @throws Exception
*/
public ArrayList fileNames(String fullPath) throws Exception {
ftpclient.ascii(); //注意,使用字符模式
TelnetInputStream list = ftpclient.nameList(fullPath);
byte[] names = new byte[2048];
int bufsize = 0;
bufsize = list.read(names, 0, names.length); //從流中讀取
list.close();
ArrayList namesList = new ArrayList();
int i = 0;
int j = 0;
while (i < bufsize /*names.length*/) {
//char bc = (char) names[i];
//System.out.println(i + " " + bc + " : " + (int) names[i]);
//i = i + 1;
if (names[i] == 10) { //字符模式爲10,二進制模式爲13
//文件名在數據中開始下標爲j,i-j爲文件名的長度,文件名在數據中的結束下標爲i-1
//System.out.write(names, j, i - j);
//System.out.println(j + " " + i + " " + (i - j));
String tempName = new String(names, j, i - j);
namesList.add(tempName);
//System.out.println(temp);
// 處理代碼處
//j = i + 2; //上一次位置二進制模式
j = i + 1; //上一次位置字符模式
}
i = i + 1;
}
return namesList;
}
/**
* 上傳文件到FTP服務器,destination路徑以FTP服務器的"/"開始,帶文件名、
* 上傳文件只能使用二進制模式,當文件存在時再次上傳則會覆蓋
* @param source String
* @param destination String
* @throws Exception
*/
public void upFile(String source, String destination) throws Exception {
buildList(destination.substring(0, destination.lastIndexOf("/")));
ftpclient.binary(); //此行代碼必須放在buildList之後
TelnetOutputStream ftpOut = ftpclient.put(destination);
TelnetInputStream ftpIn = new TelnetInputStream(new
FileInputStream(source), true);
byte[] buf = new byte[204800];
int bufsize = 0;
while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
ftpOut.write(buf, 0, bufsize);
}
ftpIn.close();
ftpOut.close();
}
/**
* JSP中的流上傳到FTP服務器,
* 上傳文件只能使用二進制模式,當文件存在時再次上傳則會覆蓋
* 字節數組做爲文件的輸入流,此方法適用於JSP中通過
* request輸入流來直接上傳文件在RequestUpload類中調用了此方法,
* destination路徑以FTP服務器的"/"開始,帶文件名
* @param sourceData byte[]
* @param destination String
* @throws Exception
*/
public void upFile(byte[] sourceData, String destination) throws Exception {
buildList(destination.substring(0, destination.lastIndexOf("/")));
ftpclient.binary(); //此行代碼必須放在buildList之後
TelnetOutputStream ftpOut = ftpclient.put(destination);
ftpOut.write(sourceData, 0, sourceData.length);
// ftpOut.flush();
ftpOut.close();
}
/**
* 從FTP文件服務器上下載文件SourceFileName,到本地destinationFileName
* 所有的文件名中都要求包括完整的路徑名在內
* @param SourceFileName String
* @param destinationFileName String
* @throws Exception
*/
public void downFile(String SourceFileName, String destinationFileName) throws
Exception {
ftpclient.binary(); //一定要使用二進制模式
TelnetInputStream ftpIn = ftpclient.get(SourceFileName);
byte[] buf = new byte[204800];
int bufsize = 0;
FileOutputStream ftpOut = new FileOutputStream(destinationFileName);
while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
ftpOut.write(buf, 0, bufsize);
}
ftpOut.close();
ftpIn.close();
}
/**
*從FTP文件服務器上下載文件,輸出到字節數組中
* @param SourceFileName String
* @return byte[]
* @throws Exception
*/
public byte[] downFile(String SourceFileName) throws
Exception {
ftpclient.binary(); //一定要使用二進制模式
TelnetInputStream ftpIn = ftpclient.get(SourceFileName);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
byte[] buf = new byte[204800];
int bufsize = 0;
while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
byteOut.write(buf, 0, bufsize);
}
byte[] return_arraybyte = byteOut.toByteArray();
byteOut.close();
ftpIn.close();
return return_arraybyte;
}
/**調用示例
* FtpUpfile fUp = new FtpUpfile("192.150.189.22", 21, "admin", "admin");
* fUp.login();
* fUp.buildList("/adfadsg/sfsdfd/cc");
* String destination = "/test.zip";
* fUp.upFile("C://Documents and Settings//Administrator//My Documents//sample.zip",destination);
* ArrayList filename = fUp.fileNames("/");
* for (int i = 0; i < filename.size(); i++) {
* System.out.println(filename.get(i).toString());
* }
* fUp.logout();
* @param args String[]
* @throws Exception
*/
public static void main(String[] args) throws Exception {
FtpUpfile fUp = new FtpUpfile("192.150.189.22", 21, "admin", "admin");
fUp.login();
/* fUp.buildList("/adfadsg/sfsdfd/cc");
String destination = "/test/SetupDJ.rar";
fUp.upFile(
"C://Documents and Settings//Administrator//My Documents//SetupDJ.rar",
destination);
ArrayList filename = fUp.fileNames("/");
for (int i = 0; i < filename.size(); i++) {
System.out.println(filename.get(i).toString());
}
fUp.downFile("/sample.zip", "d://sample.zip");
*/
FileInputStream fin = new FileInputStream(
"C://Documents and Settings//Administrator//My Documents//SetupDJ.exe");
byte[] data = new byte[20480000];
fin.read(data, 0, data.length);
fUp.upFile(data, "/test/SetupDJ.exe");
fUp.logout();
System.out.println("程序運行完成!");
/*FTP遠程命令列表
USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT
PASS PASV STOR REST CWD STAT RMD XCUP OPTS
ACCT TYPE APPE RNFR XCWD HELP XRMD STOU AUTH
REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ
QUIT MODE SYST ABOR NLST MKD XPWD MDTM PROT
*/
/*在服務器上執行命令,如果用sendServer來執行遠程命令(不能執行本地FTP命令)的話,所有FTP命令都要加上/r/n
ftpclient.sendServer("XMKD /test/bb/r/n"); //執行服務器上的FTP命令
ftpclient.readServerResponse一定要在sendServer後調用
nameList("/test")獲取指目錄下的文件列表
XMKD建立目錄,當目錄存在的情況下再次創建目錄時報錯
XRMD刪除目錄
DELE刪除文件
*/
}
}
-------------------------------------------------------------------------------------------------
畢業後頭五年決定你的一生 海量Android教程、開發資料和源碼
10類最急需IT人才:Java開發者居首 給將成爲“Android高手”的10個建議
成爲Java高手的25個學習目標--非常經典 Android 4.1果凍豆新特性詳解
芯片巨頭海思和展訊:給中國芯片業帶來信心 海量經典Java教程、學習資料和源碼
Java侵權訴訟Google獲勝,Android厚積薄發 面試必備:Android筆試總結
Android高手必須掌握的28大內容和10個建議 Android平臺研發人才缺口30萬
Android開發環境安裝和配置步驟詳細圖解 2012國內移動App開發者大調查結果
Windows 7下搭建android開發環境步驟圖解 Android 4.0的30個突出的新特性
Android高手要經過的6個階段和6個境界 linux下搭建Android開發環境步驟
從IT菜鳥變爲“IT骨幹開發者”的11個建議 程序員編程技術迅速提高的終極攻略
2012世界各國人均GDP排名,中國超泰國 2012年全國各省平均工資排行
2012年中國大學高校排行榜(580強排名) 中國各省市面積和人口數量排名
中國百萬開發者大調查:程序員的薪水不錯 Java高手需要越過的10座高山
周立功談嵌入式:我的25年嵌入式生涯 Android和Java語言的異同和關係
華爲中國區手機銷量達千萬,80%爲智能機 谷歌Android碎片化嚴重
2012年中國各省GDP和人均GDP排名 90後就業“錢景”:IT仍是最佳選擇
2012全球城市競爭力500強,69箇中國城市上榜 不要做浮躁的軟件工程師
2012年世界500強,79家大陸香港臺灣公司上榜名單 給IT新兵的15個建議
美國知名科技公司入門級軟件工程師的薪水排名 回顧Java經過的風風雨雨
71道經典Android面試題和答案--重要知識點都涉及到了