<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 100  文章 - 50  trackbacks - 0
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    我收藏的一些文章!

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    這是偶第一次寫java程序實現公司要求的FTP數據同步,所有原代碼如下,以供各位參考并提出寶貴意見!另外俺還有兩個功能沒有實現,有請高手指點一二:1、如何刪除FTP服務器上已經下載過的文件?2、如何將上傳的文件改名?上傳時在文件名前加“temp_”,上傳完畢后需要把文件名改回來。3、如何使用openPassiveDataConnection()方法將ftp連接更改為主動連接,因為要從unix系統配置的ftp服務器下載文件的話,就需要主動連接。



    /*
    *******************************************************************************************************

    Filename: ftp.java
    Author:   leetsing(elove)
    Create date: 2004-08-30
    Use:   connect to FTP server,then upload and download file
    Modify date: 2004-09-05 add to upload file
      2004-09-13 add to download file
    Copy right: Magisky Media Technology Co.,Ltd.

    *******************************************************************************************************
    */
    //import cz.dhl.io.*;
    //import cz.dhl.ftp.*;
    import sun.net.ftp.*;
    import sun.net.*;
    import java.applet.*;
    import java.io.*;
    import java.io.IOException;
    import java.util.StringTokenizer;
    import sun.net.ftp.FtpClient;
    import java.util.ArrayList;

    public class ftp extends Applet
    {
    FtpClient aftp;
    DataOutputStream outputs ;
    TelnetInputStream ins;
    TelnetOutputStream outs;

    int ch;
    public String a;
    String hostname=\"\";
    private String path = \"/\";

    public static void main(String[] args)
    {
    String hostname = \"192.168.0.56\";
    int port = 2121;
    String uid = \"lee\";
    String pwd = \"lee\";
    String RWFileDir = \"D:\\\\smsftp\\\\\";//文件目錄

    //連接ftp服務器
    ftp ft = new ftp();
    ft.connect(RWFileDir,hostname,port,uid,pwd);

    //下載文件
    if (ft.aftp != null){
      try {
      ft.getNameList(RWFileDir);
      }catch(IOException e)
      {
      System.out.println(\"下載文件出錯:\"+e);
      }
    }


    //上傳文件
    if (ft.aftp != null){
      String sdir = RWFileDir + \"subunsubfromsp\\\\\";
      File fdir = new File(sdir);
      String FileName = \"\";

      for(int i=0;i   FileName = sdir + (fdir.list());
     
      ft.uploadFile(RWFileDir,FileName);
      }
      //System.out.println(\"成功上傳的文件:\");
      //ft.showFileContents(\"subunsubfromsp\\\\\");
    }

    //刪除subunsubfromsp目錄下已經上傳的文件文件
    // ft.deleFile(RWFileDir);

    //斷開服務器連接
    ft.stop(RWFileDir);

    }

    public FtpClient connect(String RWFileDir,String hostname,int port,String uid,String pwd)
    {
    this.hostname = hostname;
    System.out.println(\"正在連接\"+hostname+\",請等待.....\");
    try{
      aftp = new FtpClient(hostname,port);
      aftp.login(uid,pwd);
      aftp.binary();
      //aftp.openPortDataConnection();
      a = \"連接主機:\"+hostname+\"成功!\";
      System.out.println(a);
    }
    catch(FtpLoginException e){
      a=\"登陸主機:\"+hostname+\"失敗!請檢查用戶名或密碼是否正確:\"+e;
      System.out.println(a);
      //return false;
    }
    catch (IOException e){
      a=\"連接主機:\"+hostname+\"失敗!請檢查端口是否正確:\"+e;
      System.out.println(a);
      //return false;
    }
    catch(SecurityException e)
    {
      a=\"無權限與主機:\"+hostname+\"連接!請檢查是否有訪問權限:\"+e;
      System.out.println(a);
      //return false;
    }

    log(RWFileDir,a);
    return aftp;
    }

    public void stop(String RWFileDir)
    {
    String message = \"\";
    try {
      if(aftp!=null){
      aftp.closeServer();
      message = \"與主機\"+hostname+\"連接已斷開!\";
      System.out.println(message);
      log(RWFileDir,message);
      }
    }
    catch(IOException e)
    {
      message = \"與主機\"+hostname+\"斷開連接失敗!\"+e;
      System.out.println(message);
      log(RWFileDir,message);
    }
    }


    public boolean downloadFile(String RWFileDir,String filepathname){
    boolean result=true;
    String message = \"\";
    if (aftp != null)
    {
      System.out.println(\"正在下載文件\"+filepathname+\",請等待....\");
      String badfile = filepathname.substring(filepathname.length()-4,filepathname.length());
      String badlog = filepathname.substring(filepathname.length()-7,filepathname.length());
      String baddir = \"\";
      if ((badfile.compareTo(\".bad\") != 0) && (badlog.compareTo(\".badlog\") != 0)){
      baddir = \"subunsubtosp\\\\\";
      }
      else{
      baddir = \"bad\\\\\";
      }
      String strdir = \"subunsubtosp\\\\\";
      //System.out.println(RWFileDir + baddir + filepathname);
      try{
      //FtpClient fc=new FtpClient(\"192.168.0.56\",2121);
      //fc.login(\"lee\",\"lee\");
      int ch;
      File fi = new File(RWFileDir + baddir + filepathname);
      //aftp.cd(strdir);
      RandomAccessFile getFile = new RandomAccessFile(fi,\"rw\");
      getFile.seek(0);
      TelnetInputStream fget=aftp.get(strdir+filepathname);
      DataInputStream puts = new DataInputStream(fget);
      while ((ch = puts.read()) >= 0) {
      getFile.write(ch);
     
      }
      //s.delete();
     
      fget.close();
      getFile.close();
      //fc.closeServer();

      message = \"下載\"+filepathname+\"文件到\"+baddir +\"目錄成功!\";
      System.out.println(message);
      log(RWFileDir,message);
      }
      catch(IOException e){
      message = \"下載\"+filepathname+\"文件到\"+baddir +\"目錄失敗!\"+e;
      System.out.println(message);
      log(RWFileDir,message);
      result = false ;

      }
    }
    else{
      result = false;
    }
    return result;
    }


    public boolean uploadFile(String RWFileDir,String filepathname){
    boolean result=true;
    String message = \"\";
    if (aftp != null)
    {
      System.out.println(\"正在上傳文件\"+filepathname+\",請等待....\");

      try{
      String fg =new String(\"\\\\subunsubfromsp\\\\\");
      int index = filepathname.lastIndexOf(fg);
      String filename = filepathname.substring(index+1);
      File localFile = new File(filepathname) ;
       
      RandomAccessFile sendFile = new RandomAccessFile(filepathname,\"r\");
      //
      sendFile.seek(0);
      //改名上傳temp_
      filename = filename.substring(0,15)+\"temp_\"+filename.substring(15,filename.length());
      outs = aftp.put(filename);
      outputs = new DataOutputStream(outs);
      while (sendFile.getFilePointer() < sendFile.length() )
      {
      ch = sendFile.read();
      outputs.write(ch);
      }
     
      rename(filename.substring(15,filename.length()),filename.substring(20,filename.length()));
      outs.close();
      sendFile.close();
     
      message = \"上傳\"+filepathname+\"文件成功!\";
      System.out.println(message);
      log(RWFileDir,message);
      }
      catch(IOException e){
      message = \"上傳\"+filepathname+\"文件失敗!\"+e;
      System.out.println(message);
      log(RWFileDir,message);
      result = false ;

      }
    }
    else{
      result = false;
    }
    return result;
    }

    public void rename(String oldName,String newName){

      //aftp.renameTo(oldName,newName);
      File Old = new File(oldName); //oldName
      File New = new File(newName); //newName
      //aftp.renameTo(New);
      //boolean Old.renameTo(File newName);
      //System.out.println(Old);
      //System.out.println(New);

    }

    public static void deleFile(String RWFileDir) {
    //try {
    //取得ReadFile目錄下的txt文件
    String sdir = RWFileDir + \"subunsubfromsp\\\\\";
    File fdir = new File(sdir);
    String FileName = \"\";
    int j = fdir.list().length;

    System.out.println(sdir+\"目錄下要刪除的文件數:\"+fdir.list().length);
    File file;
    for(int i=0;i {
      //刪除subunsubfromsp中的txt文件
      FileName = RWFileDir + \"subunsubfromsp\\\\\" + (fdir.list())[0];
      file = new File(FileName);
      file.delete();
      System.out.println(\"已經成功刪除\"+FileName+\"文件!\");
    }
    //}
    //catch (IOException e) {
    // System.out.println(\"刪除txt文件錯誤!\");
    // e.printStackTrace();
    //}
    }


    public void showFileContents(String strdir)
    {
    StringBuffer buf = new StringBuffer();
    try {
      aftp.cd(strdir);
      ins= aftp.list();
      while ((ch=ins.read())>=0){
      buf.append((char)ch);
      }
     
      System.out.println(buf.toString());

      ins.close();
        }
    catch(IOException e)
    {
    }
    }



    // 返回當前目錄的所有文件及文件夾

    public ArrayList getFileList() throws IOException {
    BufferedReader dr = new BufferedReader(new InputStreamReader(aftp.list()));
    ArrayList al = new ArrayList();
    String s = \"\";
    while ( (s = dr.readLine()) != null) {
    al.add(s);
    }
    return al;
    }

    public void setPath(String path) throws IOException {
    if (aftp == null)
    this.path = path;
    else {
      aftp.cd(path);
    }
    }

    // 返回當前目錄的文件名稱

    public ArrayList getNameList(String RWFileDir) throws IOException {

    BufferedReader dr = new BufferedReader(new InputStreamReader(aftp.nameList(\"subunsubtosp\\\\\")));
    ArrayList al = new ArrayList();
    String s = \"\";
    while ( (s = dr.readLine()) != null) {
      al.add(s);
      s = s.substring(13,s.length());
      isFile(s);
      downloadFile(RWFileDir,s);
      //String strFileDelF = aftp.nameList(\"subunsubtosp\\\\\");
      File fileDelF=new File(s);
      fileDelF.delete();
    }
    return al;
    //System.out.println(al.add(s));
    }

    // 判斷一行文件信息是否為目錄

    public boolean isDir(String line) {
    return ( (String) parseLine(line).get(0)).indexOf(\"d\") != -1;
    }

    public boolean isFile(String line) {
    return!isDir(line);
    }

    // 處理getFileList取得的行信息

    private ArrayList parseLine(String line) {
    ArrayList s1 = new ArrayList();
    StringTokenizer st = new StringTokenizer(line, \" \");
    while (st.hasMoreTokens()) {
      s1.add(st.nextToken());
    }
      return s1;
    }

    //寫消息日志
    public static void log(String RWFileDir,String msg)
    {
    String message = \"\";
    try {
      java.text.DateFormat df = new java.text.SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");
      java.text.DateFormat dflog = new java.text.SimpleDateFormat(\"yyyyMMdd\");
      java.util.Date date = new java.util.Date() ;
      String datestr = df.format(new java.util.Date()) ;
      String datelog = dflog.format(new java.util.Date()) ;
      //String datelog = datestr.substring(0,10);
      //datelog = datelog.replace(\'-\',\' \');

      //按日期每天生成一個日志文件
      FileWriter fwl = new FileWriter(RWFileDir + \"CMSSftp\"+datelog+\".log\",true);
      PrintWriter outl = new PrintWriter(fwl);
      outl.println(datestr + \" \" + msg);
      outl.close();
      fwl.close();
    }catch (IOException e) {
      message = \"寫log文件錯誤!\"+e;
      e.printStackTrace();
      log(RWFileDir,message);
      System.out.println(message);
    }
    }
    }

    轉自:http://bbs.java.ccidnet.com/read.php?tid=135053&fpage=4
    posted on 2010-09-01 18:30 fly 閱讀(241) 評論(0)  編輯  收藏 所屬分類: java學習
    主站蜘蛛池模板: 亚洲精品无码av人在线观看 | av电影在线免费看| 亚洲精品和日本精品| 亚洲精品成人网站在线观看| 日本免费人成视频在线观看| 学生妹亚洲一区二区| 亚洲免费一区二区| 亚洲香蕉免费有线视频| 亚洲精品无码少妇30P| 国产亚洲精品精华液| 成年女人色毛片免费看| 一个人晚上在线观看的免费视频 | 国产区卡一卡二卡三乱码免费| 亚洲黄片手机免费观看| 亚洲字幕在线观看| 亚洲国产精品丝袜在线观看| 在线观看www日本免费网站| 免费一区二区三区在线视频| 亚洲精彩视频在线观看| 久久精品乱子伦免费| 亚洲精华液一二三产区| 亚洲av无码不卡| 免费jjzz在线播放国产| 91免费人成网站在线观看18| h视频在线免费观看| 麻豆狠色伊人亚洲综合网站 | 久久久久亚洲精品美女| 国产午夜鲁丝片AV无码免费 | 无码中文字幕av免费放| 亚洲婷婷第一狠人综合精品| 怡红院亚洲怡红院首页| 日本无吗免费一二区| 久久午夜夜伦鲁鲁片免费无码影视| 一级毛片无遮挡免费全部| 亚洲国产精品无码久久| 亚洲国产日韩在线| 亚洲高清在线视频| 亚洲人成色7777在线观看不卡 | 亚洲国产精品成人精品无码区在线| 亚洲精品无码少妇30P| 亚洲国产成a人v在线|