ftp下載文件java
(KmConfigkmConfig,StringfileName,StringclientFileName,OutputStreamoutputStream){
	try{
		StringftpHost=kmConfig.getFtpHost();
		intport=kmConfig.getFtpPort();
		StringuserName=kmConfig.getFtpUser();
		StringpassWord=kmConfig.getFtpPassword();
		Stringpath=kmConfig.getFtpPath();
		FtpClientftpClient=newFtpClient(ftpHost,port);//ftpHost為FTP伺服器的IP地址,port為FTP伺服器的登陸埠,ftpHost為String型,port為int型。
		ftpClient.login(userName,passWord);//userName、passWord分別為FTP伺服器的登陸用戶名和密碼
		ftpClient.binary();
		ftpClient.cd(path);//path為FTP伺服器上保存上傳文件的路徑。
		try{
			TelnetInputStreamin=ftpClient.get(fileName);
			byte[]bytes=newbyte[1024];
			intcnt=0;
			while((cnt=in.read(bytes,0,bytes.length))!=-1){
				outputStream.write(bytes,0,cnt);
			}
			//##############################################
			//這里文件就已經下載完了,自己理解一下
			//#############################################
			
			outputStream.close();
			in.close();
		}catch(Exceptione){
			ftpClient.closeServer();
			e.printStackTrace();
		}
		ftpClient.closeServer();
	}catch(Exceptione){
		System.out.println("下載文件失敗!請檢查系統FTP設置,並確認FTP服務啟動");
	}
}
B. java 下載異地FTP中的zip文件
好像需要一個支持jar包把,把ftp4j的下載地址貼出來
C. java高手進來,要用JAVA做個從FTP下載文件的軟體,有沒有這方面的教程或者資料,急急急!!!!
Java陣營中有這方面的免費開源技術,最權威的是Apache的Java開源陣營。
您可以應用Apache commons開源組件集合中的Net組件包,到官網免費獲得:
http://commons.apache.org/net/
通過它可以輕松的創建FTP、FTPS 、TFTP等下載程序,很方便。具體方法參見官網的文檔和示常式序,一點也不難!示常式序如下:
http://commons.apache.org/net/examples/ftp/FTPClientExample.java
如果還不能解決,需要幫忙,可以QQ聯絡我:[email protected]。
D. java如何實現txt下載 就是從遠程FTP伺服器上直接把TXT文本下載下來!
strUrl為文件地址,fileName為文件在本地的保存路徑,試試吧~
public static void writeFile(String strUrl, String fileName) {
  URL url = null;
  try {
   url = new URL(strUrl);
  } catch (MalformedURLException e2) {
   e2.printStackTrace();
  }
  InputStream is = null;
  try {
   is = url.openStream();
  } catch (IOException e1) {
   e1.printStackTrace();
  }
  OutputStream os = null;
  try {
   os = new FileOutputStream( fileName);
   int bytesRead = 0;
   byte[] buffer = new byte[8192];
   while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
    os.write(buffer, 0, bytesRead);
   }
   System.out.println("下載成功:"+strUrl);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
E. Java的ftp操作方法有哪幾種
FTP(File Transfer Protocol)是 Internet 上用來傳送文件的協議(文件傳輸協議)。它是為了我們能夠在 Internet 上互相傳送文件而制定的的文件傳送標准,規定了 Internet 上文件如何傳送。也就是說,通過 FTP 協議,我們就可以跟 Internet 上的 FTP 伺服器進行文件的上傳(Upload)或下載(Download)等動作。
和其他 Internet 應用一樣,FTP 也是依賴於客戶程序/伺服器關系的概念。在 Internet 上有一些網站,它們依照 FTP 協議提供服務,讓網友們進行文件的存取,這些網站就是 FTP 伺服器。網上的用戶要連上 FTP 伺服器,就要用到 FPT 的客戶端軟體,通常 Windows 都有「ftp」命令,這實際就是一個命令行的 FTP 客戶程序,另外常用的 FTP 客戶程序還有 CuteFTP、Ws_FTP、FTP Explorer等。
要連上 FTP 伺服器(即「登陸」),必須要有該 FTP 伺服器的帳號。如果是該伺服器主機的注冊客戶,你將會有一個 FTP 登陸帳號和密碼,就憑這個帳號密碼連上該伺服器。但 Internet 上有很大一部分 FTP 伺服器被稱為「匿名」(Anonymous)FTP 伺服器。這類伺服器的目的是向公眾提供文件拷貝服務,因此,不要求用戶事先在該伺服器進行登記注冊。
Anonymous(匿名文件傳輸)能夠使用戶與遠程主機建立連接並以匿名身份從遠程主機上拷貝文件,而不必是該遠程主機的注冊用戶。用戶使用特殊的用戶名「anonymous」和「guest」就可有限制地訪問遠程主機上公開的文件。現在許多系統要求用戶將Emai1地址作為口令,以便更好地對訪問進行跟綜。出於安全的目的,大部分匿名FTP主機一般只允許遠程用戶下載(download)文件,而不允許上載(upload)文件。也就是說,用戶只能從匿名FTP主機拷貝需要的文件而不能把文件拷貝到匿名FTP主機。另外,匿名FTP主機還採用了其他一些保護措施以保護自己的文件不至於被用戶修改和刪除,並防止計算機病毒的侵入。在具有圖形用戶界面的 WorldWild Web環境於1995年開始普及以前,匿名FTP一直是Internet上獲取信息資源的最主要方式,在Internet成千上萬的匿名PTP主機中存儲著無以計數的文件,這些文件包含了各種各樣的信息,數據和軟體。 人們只要知道特定信息資源的主機地址, 就可以用匿名FTP登錄獲取所需的信息資料。雖然目前使用WWW環境已取代匿名FTP成為最主要的信息查詢方式,但是匿名FTP仍是 Internet上傳輸分發軟體的一種基本方法
F. java 實現ftp上傳下載,windows下和linux下游何區別
packagecom.weixin.util;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.io.PrintWriter;
importjava.io.RandomAccessFile;
importorg.apache.commons.net.PrintCommandListener;
importorg.apache.commons.net.ftp.FTP;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPFile;
importorg.apache.commons.net.ftp.FTPReply;
importcom.weixin.constant.DownloadStatus;
importcom.weixin.constant.UploadStatus;
/**
*支持斷點續傳的FTP實用類
*@version0.1實現基本斷點上傳下載
*@version0.2實現上傳下載進度匯報
*@version0.3實現中文目錄創建及中文文件創建,添加對於中文的支持
*/
publicclassContinueFTP{
publicFTPClientftpClient=newFTPClient();
publicContinueFTP(){
//設置將過程中使用到的命令輸出到控制台
this.ftpClient.addProtocolCommandListener(newPrintCommandListener(newPrintWriter(System.out)));
}
/**
*連接到FTP伺服器
*@paramhostname主機名
*@paramport埠
*@paramusername用戶名
*@parampassword密碼
*@return是否連接成功
*@throwsIOException
*/
publicbooleanconnect(Stringhostname,intport,Stringusername,Stringpassword)throwsIOException{
ftpClient.connect(hostname,port);
ftpClient.setControlEncoding("GBK");
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username,password)){
returntrue;
}
}
disconnect();
returnfalse;
}
/**
*從FTP伺服器上下載文件,支持斷點續傳,上傳百分比匯報
*@paramremote遠程文件路徑
*@paramlocal本地文件路徑
*@return上傳的狀態
*@throwsIOException
*/
publicDownloadStatusdownload(Stringremote,Stringlocal)throwsIOException{
//設置被動模式
ftpClient.enterLocalPassiveMode();
//設置以二進制方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatusresult;
//檢查遠程文件是否存在
FTPFile[]files=ftpClient.listFiles(newString(remote.getBytes("GBK"),"iso-8859-1"));
if(files.length!=1){
System.out.println("遠程文件不存在");
returnDownloadStatus.Remote_File_Noexist;
}
longlRemoteSize=files[0].getSize();
Filef=newFile(local);
//本地存在文件,進行斷點下載
if(f.exists()){
longlocalSize=f.length();
//判斷本地文件大小是否大於遠程文件大小
if(localSize>=lRemoteSize){
System.out.println("本地文件大於遠程文件,下載中止");
returnDownloadStatus.Local_Bigger_Remote;
}
//進行斷點續傳,並記錄狀態
FileOutputStreamout=newFileOutputStream(f,true);
ftpClient.setRestartOffset(localSize);
InputStreamin=ftpClient.retrieveFileStream(newString(remote.getBytes("GBK"),"iso-8859-1"));
byte[]bytes=newbyte[1024];
longstep=lRemoteSize/100;
longprocess=localSize/step;
intc;
while((c=in.read(bytes))!=-1){
out.write(bytes,0,c);
localSize+=c;
longnowProcess=localSize/step;
if(nowProcess>process){
process=nowProcess;
if(process%10==0)
System.out.println("下載進度:"+process);
//TODO更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
booleanisDo=ftpClient.completePendingCommand();
if(isDo){
result=DownloadStatus.Download_From_Break_Success;
}else{
result=DownloadStatus.Download_From_Break_Failed;
}
}else{
OutputStreamout=newFileOutputStream(f);
InputStreamin=ftpClient.retrieveFileStream(newString(remote.getBytes("GBK"),"iso-8859-1"));
byte[]bytes=newbyte[1024];
longstep=lRemoteSize/100;
longprocess=0;
longlocalSize=0L;
intc;
while((c=in.read(bytes))!=-1){
out.write(bytes,0,c);
localSize+=c;
longnowProcess=localSize/step;
if(nowProcess>process){
process=nowProcess;
if(process%10==0)
System.out.println("下載進度:"+process);
//TODO更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
booleanupNewStatus=ftpClient.completePendingCommand();
if(upNewStatus){
result=DownloadStatus.Download_New_Success;
}else{
result=DownloadStatus.Download_New_Failed;
}
}
returnresult;
}
/**
*上傳文件到FTP伺服器,支持斷點續傳
*@paramlocal本地文件名稱,絕對路徑
*@paramremote遠程文件路徑,使用/home/directory1/subdirectory/file.ext按照Linux上的路徑指定方式,支持多級目錄嵌套,支持遞歸創建不存在的目錄結構
*@return上傳結果
*@throwsIOException
*/
publicUploadStatusupload(Stringlocal,Stringremote)throwsIOException{
//設置PassiveMode傳輸
ftpClient.enterLocalPassiveMode();
//設置以二進制流的方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GBK");
UploadStatusresult;
//對遠程目錄的處理
StringremoteFileName=remote;
if(remote.contains("/")){
remoteFileName=remote.substring(remote.lastIndexOf("/")+1);
//創建伺服器遠程目錄結構,創建失敗直接返回
if(CreateDirecroty(remote,ftpClient)==UploadStatus.Create_Directory_Fail){
returnUploadStatus.Create_Directory_Fail;
}
}
//檢查遠程是否存在文件
FTPFile[]files=ftpClient.listFiles(newString(remoteFileName.getBytes("GBK"),"iso-8859-1"));
if(files.length==1){
longremoteSize=files[0].getSize();
Filef=newFile(local);
longlocalSize=f.length();
if(remoteSize==localSize){
returnUploadStatus.File_Exits;
}elseif(remoteSize>localSize){
returnUploadStatus.Remote_Bigger_Local;
}
//嘗試移動文件內讀取指針,實現斷點續傳
result=uploadFile(remoteFileName,f,ftpClient,remoteSize);
//如果斷點續傳沒有成功,則刪除伺服器上文件,重新上傳
if(result==UploadStatus.Upload_From_Break_Failed){
if(!ftpClient.deleteFile(remoteFileName)){
returnUploadStatus.Delete_Remote_Faild;
}
result=uploadFile(remoteFileName,f,ftpClient,0);
}
}else{
result=uploadFile(remoteFileName,newFile(local),ftpClient,0);
}
returnresult;
}
/**
*斷開與遠程伺服器的連接
*@throwsIOException
*/
publicvoiddisconnect()throwsIOException{
if(ftpClient.isConnected()){
ftpClient.disconnect();
}
}
/**
*遞歸創建遠程伺服器目錄
*@paramremote遠程伺服器文件絕對路徑
*@paramftpClientFTPClient對象
*@return目錄創建是否成功
*@throwsIOException
*/
(Stringremote,FTPClientftpClient)throwsIOException{
UploadStatusstatus=UploadStatus.Create_Directory_Success;
Stringdirectory=remote.substring(0,remote.lastIndexOf("/")+1);
if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(newString(directory.getBytes("GBK"),"iso-8859-1"))){
//如果遠程目錄不存在,則遞歸創建遠程伺服器目錄
intstart=0;
intend=0;
if(directory.startsWith("/")){
start=1;
}else{
start=0;
}
end=directory.indexOf("/",start);
while(true){
StringsubDirectory=newString(remote.substring(start,end).getBytes("GBK"),"iso-8859-1");
if(!ftpClient.changeWorkingDirectory(subDirectory)){
if(ftpClient.makeDirectory(subDirectory)){
ftpClient.changeWorkingDirectory(subDirectory);
}else{
System.out.println("創建目錄失敗");
returnUploadStatus.Create_Directory_Fail;
}
}
start=end+1;
end=directory.indexOf("/",start);
//檢查所有目錄是否創建完畢
if(end<=start){
break;
}
}
}
returnstatus;
}
/**
*上傳文件到伺服器,新上傳和斷點續傳
*@paramremoteFile遠程文件名,在上傳之前已經將伺服器工作目錄做了改變
*@paramlocalFile本地文件File句柄,絕對路徑
*@paramprocessStep需要顯示的處理進度步進值
*@paramftpClientFTPClient引用
*@return
*@throwsIOException
*/
publicUploadStatusuploadFile(StringremoteFile,FilelocalFile,FTPClientftpClient,longremoteSize)throwsIOException{
UploadStatusstatus;
//顯示進度的上傳
longstep=localFile.length()/100;
longprocess=0;
longlocalreadbytes=0L;
RandomAccessFileraf=newRandomAccessFile(localFile,"r");
OutputStreamout=ftpClient.appendFileStream(newString(remoteFile.getBytes("GBK"),"iso-8859-1"));
//斷點續傳
if(remoteSize>0){
ftpClient.setRestartOffset(remoteSize);
process=remoteSize/step;
raf.seek(remoteSize);
localreadbytes=remoteSize;
}
byte[]bytes=newbyte[1024];
intc;
while((c=raf.read(bytes))!=-1){
out.write(bytes,0,c);
localreadbytes+=c;
if(localreadbytes/step!=process){
process=localreadbytes/step;
System.out.println("上傳進度:"+process);
//TODO匯報上傳狀態
}
}
out.flush();
raf.close();
out.close();
booleanresult=ftpClient.completePendingCommand();
if(remoteSize>0){
status=result?UploadStatus.Upload_From_Break_Success:UploadStatus.Upload_From_Break_Failed;
}else{
status=result?UploadStatus.Upload_New_File_Success:UploadStatus.Upload_New_File_Failed;
}
returnstatus;
}
publicstaticvoidmain(String[]args){
ContinueFTPmyFtp=newContinueFTP();
try{
System.err.println(myFtp.connect("10.10.6.236",21,"5","jieyan"));
// myFtp.ftpClient.makeDirectory(newString("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.changeWorkingDirectory(newString("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.makeDirectory(newString("愛你等於愛自己".getBytes("GBK"),"iso-8859-1"));
// System.out.println(myFtp.upload("E:\yw.flv","/yw.flv",5));
// System.out.println(myFtp.upload("E:\愛你等於愛自己.mp4","/愛你等於愛自己.mp4"));
//System.out.println(myFtp.download("/愛你等於愛自己.mp4","E:\愛你等於愛自己.mp4"));
myFtp.disconnect();
}catch(IOExceptione){
System.out.println("連接FTP出錯:"+e.getMessage());
}
}
}
G. 看了一段java代碼是從FTP上下載文件,ftpClient.setBufferSize()這個是什麼用處,要怎麼使用它
緩沖區大小,等從ftp下載的數據存儲到緩沖區,等緩沖區滿了,進行磁碟讀寫。
H. java 使用ftp 下載文件在windows環境下正常,在linux下載不了
是不是文件的讀寫許可權問題,linux的許可權控制比windows嚴格的多
I. ftp java 下載的時候怎麼保存到指定位置
通過工具類來實現本地路徑定義和下載即可。
import hkrt.b2b.view.util.Log;
import hkrt.b2b.view.util.ViewUtil;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class CCFCCBFTP {
    /**
     * 上傳文件
     *
     * @param fileName
     * @param plainFilePath 明文文件路徑路徑
     * @param filepath
     * @return 
     * @throws Exception
     */
    public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        FTPClient ftpClient = new FTPClient();
        String bl = "false";
        try {
            fis = new FileInputStream(plainFilePath);
            bos = new ByteArrayOutputStream(fis.available());
            byte[] buffer = new byte[1024];
            int count = 0;
            while ((count = fis.read(buffer)) != -1) {
                bos.write(buffer, 0, count);
            }
            bos.flush();
            Log.info("加密上傳文件開始");
            Log.info("連接遠程上傳伺服器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
            ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
            ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
            FTPFile[] fs;
            fs = ftpClient.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(filepath)) {
                    bl="true";
                    ftpClient.changeWorkingDirectory("/"+filepath+"");
                }
            }
             Log.info("檢查文件路徑是否存在:/"+filepath);
             if("false".equals(bl)){
                 ViewUtil.dataSEErrorPerformedCommon( "查詢文件路徑不存在:"+"/"+filepath);
                  return bl;
            }
            ftpClient.setBufferSize(1024);
            ftpClient.setControlEncoding("GBK");
            // 設置文件類型(二進制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.storeFile(fileName, fis);
            Log.info("上傳文件成功:"+fileName+"。文件保存路徑:"+"/"+filepath+"/");
            return bl;
        } catch (Exception e) {
            throw e;
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                    Log.info(e.getLocalizedMessage(), e);
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (Exception e) {
                    Log.info(e.getLocalizedMessage(), e);
                }
            }
        }
    
    }
    /**
     *下載並解壓文件
     *
     * @param localFilePath
     * @param fileName
     * @param routeFilepath
     * @return 
     * @throws Exception
     */
    public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        FileOutputStream fos = null;
        FTPClient ftpClient = new FTPClient();
        String SFP = System.getProperty("file.separator");
        String bl = "false";
        try {
            Log.info("下載並解密文件開始");
            Log.info("連接遠程下載伺服器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
            ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
            ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
CMBCUtil.CMBCLOGINPASSWORD);
            FTPFile[] fs;        
            
            ftpClient.makeDirectory(routeFilepath);
            ftpClient.changeWorkingDirectory(routeFilepath);
            bl = "false";
            fs = ftpClient.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(fileName)) {
                        bl = "true";
                        Log.info("下載文件開始。");
                        ftpClient.setBufferSize(1024);
                        // 設置文件類型(二進制)
                        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                        InputStream is = ftpClient.retrieveFileStream(fileName);
                        bos = new ByteArrayOutputStream(is.available());
                        byte[] buffer = new byte[1024];
                        int count = 0;
                        while ((count = is.read(buffer)) != -1) {
                            bos.write(buffer, 0, count);
                        }
                        bos.flush();
                        fos = new FileOutputStream(localFilePath+SFP+fileName);
                        fos.write(bos.toByteArray());
                        Log.info("下載文件結束:"+localFilePath);
                }
            }
            Log.info("檢查文件是否存:"+fileName+"  "+bl);
            if("false".equals(bl)){
                 ViewUtil.dataSEErrorPerformedCommon("查詢無結果,請稍後再查詢。");
                 return bl;
            }
          return bl;
        } catch (Exception e) {
            throw e;
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                    Log.info(e.getLocalizedMessage(), e);
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (Exception e) {
                    Log.info(e.getLocalizedMessage(), e);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    Log.info(e.getLocalizedMessage(), e);
                }
            }
        }
    }
//       調用樣例:
    public static void main(String[] args) {
        try {
            // 明文文件路徑
            String plainFilePath = "D:/req_20150204_0011.txt";
            // 密文文件路徑
            String secretFilePath = "req_20150204_00134.txt";
          fileDownloadByFtp("D://123.zip","123.zip","req/20150228");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
J. 如何在Java程序中實現FTP的上傳下載功能
以下是這三部分的JAVA源程序: (1)顯示FTP伺服器上的文件 void ftpList_actionPerformed(ActionEvent e) {String server=serverEdit.getText();//輸入的FTP伺服器的IP地址 String user=userEdit.getText();//登錄FTP伺服器的用戶名 String password=passwordEdit.getText();//登錄FTP伺服器的用戶名的口令 String path=pathEdit.getText();//FTP伺服器上的路徑 try {FtpClient ftpClient=new FtpClient();//創建FtpClient對象 ftpClient.openServer(server);//連接FTP伺服器 ftpClient.login(user, password);//登錄FTP伺服器 if (path.length()!=0) ftpClient.cd(path); TelnetInputStream is=ftpClient.list(); int c; while ((c=is.read())!=-1) { System.out.print((char) c);} is.close(); ftpClient.closeServer();//退出FTP伺服器 } catch (IOException ex) {;} } (2)從FTP伺服器上下傳一個文件 void getButton_actionPerformed(ActionEvent e) { String server=serverEdit.getText(); String user=userEdit.getText(); String password=passwordEdit.getText(); String path=pathEdit.getText(); String filename=filenameEdit.getText(); try { FtpClient ftpClient=new FtpClient(); ftpClient.openServer(server); ftpClient.login(user, password); if (path.length()!=0) ftpClient.cd(path); ftpClient.binary(); TelnetInputStream is=ftpClient.get(filename); File file_out=new File(filename); FileOutputStream os=new FileOutputStream(file_out); byte[] bytes=new byte[1024]; int c; while ((c=is.read(bytes))!=-1) { os.write(bytes,0,c); } is.close(); os.close(); ftpClient.closeServer(); } catch (IOException ex) {;} } (3)向FTP伺服器上上傳一個文件 void putButton_actionPerformed(ActionEvent e) { String server=serverEdit.getText(); String user=userEdit.getText(); String password=passwordEdit.getText(); String path=pathEdit.getText(); String filename=filenameEdit.getText(); try { FtpClient ftpClient=new FtpClient(); ftpClient.openServer(server); ftpClient.login(user, password); if (path.length()!=0) ftpClient.cd(path); ftpClient.binary(); TelnetOutputStream os=ftpClient.put(filename); File file_in=new File(filename); FileInputStream is=new FileInputStream(file_in); byte[] bytes=new byte[1024]; int c; while ((c=is.read(bytes))!=-1){ os.write(bytes,0,c);} is.close(); os.close(); ftpClient.closeServer(); } catch (IOException ex) {;} } }
