java操作FTP服務器文件系統代碼(轉)

          package com.dicpsi.common.file;


          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileReader;
          import java.io.FileWriter;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.PrintWriter;
          import java.net.*;

          import sun.net.TelnetOutputStream;
          import sun.net.ftp.FtpClient;

          public class ftpWriteAndRead {
           String ip="10.140.112.120";//ftp服務器默認IP地址
           int port=21;//ftp服務默認端口
           String user="";//登錄ftp所需帳戶
           String pwd="";//密碼
           String path="";//所在的路徑
           String err="";
           /*
            * 測試方法,用URL類直接讀取ftp上的文件
            * */
           public void tt() throws MalformedURLException{
            try{
             URL u=new URL("ftp://xjdl93:yzj@10.140.112.120/HBSQ/System_copy/123.txt");
             String _file=u.getFile();
             System.out.println(_file+">>>"+u.getHost());
             InputStream is= u.openStream();
             if(is == null){
              System.out.println("沒有能夠獲取指定的資源");
              return;
             }
            }catch(Exception e){
             e.printStackTrace();
            }
           }
           /**
            * @comment 獲取FTP客戶端連接
            * @author  chenshan
            * @date    2008-11-11
            * @param   ip:        ftp服務器所在的IP地址; 
            *    port:      ftp服務占用的端口;
            *    user:      登錄ftp所需帳戶;
            *    pwd:       登錄ftp所需密碼;
            *    path:  要登錄到ftp的相對路徑,如要進入ftp://10.140.112.120/HBSQ/System_copy
            *       那么path應該為”HBSQ/System_copy“
            */
           public FtpClient getFtpConnection(String ip,int port,String user,String pwd,String path){
            try{
             FtpClient cftp=new FtpClient();
             cftp.openServer(ip,port);
             cftp.login(user, pwd);
             System.out.println("!");
             if (path.length() != 0) cftp.cd(path);
             //cftp.binary();
             return cftp;
            }catch(Exception ex){
             ex.printStackTrace();
             err+=ex.getMessage();
             return null;
            }
           }
           
           /**
            * @comment 讀本地文件將其寫入ftp服務器(該方法測試用)
            * @author  chenshan
            * @date    2008-11-11
            * @param   textContext:所需寫入的內容 
            *    
            */
           public void upload(String textContext) {
            this.user="xjdl93";
            this.pwd="yzj";
            this.path="HBSQ/System_copy";
            FtpClient ftpClient = getFtpConnection(this.ip,this.port,this.user,this.pwd,this.path);
            String localfilename = "E://testftp//qqq.txt";
            String remotefilename = "qqq.txt";
            
            /*以下為獲取需要上傳的文件列表*/
          //  try{
          //   URL url=new URL("E:\testftp\qqq.txt");
          //   String [] fileNames=null;
          //   //fileNames=url.getFile().;
          //   System.out.print(url.getFile());
          //   return;
          //  }catch(Exception a){
          //   a.printStackTrace();
          //   System.out.println("獲取所需上傳文件列表失敗");
          //  }
            
            try {
             TelnetOutputStream os = ftpClient.put(remotefilename);//在服務器上創建文件
             /*以下為讀出本地文件*/
             java.io.File file_in = new java.io.File(localfilename);
             FileInputStream is = new FileInputStream(file_in);
             byte[] bytes = new byte[1024];
             int c;
             while ((c = is.read(bytes)) != -1) {
              os.write(bytes, 0, c);//寫入到ftp上創建的文件中
             }
             System.out.println("upload success");
             is.close();
             os.close();
             ftpClient.closeServer();
            } catch (IOException ex) {
             ex.printStackTrace();
             System.out.println("not upload");
             System.out.println(ex);
            }
           }
           
           /**
            * @comment 在ftp服務器創建文件,并將指定的內容寫入文件
            * @author  chenshan
            * @date    2008-11-11
            * @param   textContext:    需要寫入ftp文件的內容字符串, 
            *    remotefilename: 在ftp上創建的文件名稱
            *    bankCode        銀行代碼(夏津的銀行代碼為93和97,93是郵政局,97是中國銀行)
            *    path            代扣文件需要發送至服務器的相對路徑
            */
           public String writeFileToFtp(String textContext,String remotefilename,String bankCode,String path){
            try{
             this.path=path;
             if(bankCode.equals("97")){
              this.user="xjdl97";
              this.pwd="123456";
             }
             else if(bankCode.equals("93")){
              this.user="xjdl93";
              this.pwd="yzj";
             }
             else{
              System.out.println("發現不合法的銀行代碼: "+bankCode+"=========>代扣文件未能發送至FTP服務器");
              return "發現不合法的銀行代碼: "+bankCode+" -->代扣文件未能發送至FTP服務器,請仔細檢查并手工拷貝!";
             }
             FtpClient ftpClient = getFtpConnection(this.ip,this.port,this.user,this.pwd,this.path);
             byte[] _textContext = textContext.getBytes();
             TelnetOutputStream os = ftpClient.put(remotefilename);//在服務器上創建文件
             //將字節內容寫入到os所代表的文件中
             os.write(_textContext,0,_textContext.length);
             //關閉與ftp服務器的連接
             System.out.println("upload success");
             os.close();
             ftpClient.closeServer();
             return "0";
            }catch(Exception ex){
             this.err+=ex.getMessage();
             return err;
            }
           }

          }

          posted on 2010-09-27 17:33 月亮哭了 閱讀(751) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          留言簿

          文章檔案(1)

          收藏夾(1)

          搜索

          最新評論

          主站蜘蛛池模板: 军事| 洪泽县| 习水县| 南皮县| 丹凤县| 盐城市| 孝义市| 登封市| 宾阳县| 贵定县| 合江县| 洛南县| 九寨沟县| 三河市| 宁陕县| 高台县| 惠东县| 蒙山县| 准格尔旗| 光泽县| 高邑县| 海口市| 上饶市| 巴里| 定结县| 鹰潭市| 云林县| 黄石市| 肃北| 沂水县| 宁夏| 沈阳市| 安仁县| 沙湾县| 锦州市| 广德县| 秭归县| 洛宁县| 阜阳市| 荣成市| 诸暨市|