sun.net.ftp.FtpClient 操作FTP服务器上的文件和目录

               

sun.net.ftp.FtpClient 操作FTP服务器上的文件和目录


[java]  view plain  copy
  1. /** 
  2. * 在当前目录下创建文件夹 
  3. * @param dir 
  4. * @return 
  5. * @throws Exception 
  6. */  
  7. private boolean createDir(String dir){  
  8.    try{  
  9.     ftpClient.ascii();  
  10.     StringTokenizer s = new StringTokenizer(dir, "/"); //sign  
  11.     s.countTokens();  
  12.     String pathName = ftpClient.pwd();  
  13.     while(s.hasMoreElements()){  
  14.      pathName = pathName + "/" + (String) s.nextElement();  
  15.      try {  
  16.       ftpClient.sendServer("MKD " + pathName + "\r\n");  
  17.      } catch (Exception e) {  
  18.       e = null;  
  19.       return false;  
  20.      }  
  21.      ftpClient.readServerResponse();  
  22.     }  
  23.     ftpClient.binary();  
  24.     return true;  
  25.    }catch (IOException e1){  
  26.     e1.printStackTrace();  
  27.     return false;  
  28.    }  
  29. }  
  30.   
  31. /** 
  32.    * ftp上传 
  33.    * 如果服务器段已存在名为filename的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换 
  34.    *  
  35.    * @param filename 要上传的文件(或文件夹)名 
  36.    * @return 
  37.    * @throws Exception 
  38.    */  
  39. public boolean upload(String filename){  
  40.    String newname = "";  
  41.    if(filename.indexOf("/") > -1){  
  42.     newname = filename.substring(filename.lastIndexOf("/") + 1);  
  43.    }else{  
  44.     newname = filename;  
  45.    }  
  46.    return upload(filename, newname);  
  47. }  
  48.   
  49. /** 
  50.    * ftp上传 
  51.    * 如果服务器段已存在名为newName的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换 
  52.    *  
  53.    * @param fileName 要上传的文件(或文件夹)名 
  54.    * @param newName 服务器段要生成的文件(或文件夹)名 
  55.    * @return 
  56.    */  
  57. public boolean upload(String fileName, String newName){  
  58.    try{  
  59.     String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");   
  60.     File file_in = new File(savefilename);//打开本地待长传的文件  
  61.     if(!file_in.exists()){  
  62.      throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");  
  63.     }  
  64.     if(file_in.isDirectory()){  
  65.      upload(file_in.getPath(),newName,ftpClient.pwd());  
  66.     }else{  
  67.      uploadFile(file_in.getPath(),newName);  
  68.     }  
  69.       
  70.     if(is != null){  
  71.      is.close();  
  72.     }  
  73.     if(os != null){  
  74.      os.close();  
  75.     }  
  76.     return true;  
  77.    }catch(Exception e){   
  78.        e.printStackTrace();   
  79.        System.err.println("Exception e in Ftp upload(): " + e.toString());   
  80.        return false;  
  81.    }finally{  
  82.     try{  
  83.      if(is != null){  
  84.       is.close();  
  85.      }  
  86.      if(os != null){   
  87.       os.close();   
  88.      }  
  89.     }catch(IOException e){  
  90.      e.printStackTrace();  
  91.     }   
  92.    }  
  93. }  
  94.   
  95. /** 
  96.    * 真正用于上传的方法 
  97.    * @param fileName 
  98.    * @param newName 
  99.    * @param path 
  100.    * @throws Exception 
  101.    */  
  102. private void upload(String fileName, String newName,String path) throws Exception{  
  103.     String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");   
  104.     File file_in = new File(savefilename);//打开本地待长传的文件  
  105.     if(!file_in.exists()){  
  106.      throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");  
  107.     }  
  108.     if(file_in.isDirectory()){  
  109.      if(!isDirExist(newName)){  
  110.       createDir(newName);  
  111.      }  
  112.      ftpClient.cd(newName);  
  113.      File sourceFile[] = file_in.listFiles();  
  114.      for(int i = 0; i < sourceFile.length; i++){  
  115.          if(!sourceFile[i].exists()){  
  116.         continue;  
  117.          }  
  118.          if(sourceFile[i].isDirectory()){  
  119.         this.upload(sourceFile[i].getPath(),sourceFile[i].getName(),path+"/"+newName);  
  120.          }else{  
  121.         this.uploadFile(sourceFile[i].getPath(),sourceFile[i].getName());  
  122.           }  
  123.         }  
  124.     }else{  
  125.      uploadFile(file_in.getPath(),newName);  
  126.     }  
  127.     ftpClient.cd(path);  
  128. }   
  129. /** 
  130. * upload 上传文件 
  131.  
  132. * @param filename 要上传的文件名 
  133. * @param newname 上传后的新文件名 
  134. * @return -1 文件不存在 >=0 成功上传,返回文件的大小 
  135. * @throws Exception 
  136. */  
  137. public long uploadFile(String filename, String newname) throws Exception{  
  138.    long result = 0;  
  139.    TelnetOutputStream os = null;  
  140.    FileInputStrea
[java]  view plain  copy
  1. /** 
  2. * 在当前目录下创建文件夹 
  3. * @param dir 
  4. * @return 
  5. * @throws Exception 
  6. */  
  7. private boolean createDir(String dir){  
  8.    try{  
  9.     ftpClient.ascii();  
  10.     StringTokenizer s = new StringTokenizer(dir, "/"); //sign  
  11.     s.countTokens();  
  12.     String pathName = ftpClient.pwd();  
  13.     while(s.hasMoreElements()){  
  14.      pathName = pathName + "/" + (String) s.nextElement();  
  15.      try {  
  16.       ftpClient.sendServer("MKD " + pathName + "\r\n");  
  17.      } catch (Exception e) {  
  18.       e = null;  
  19.       return false;  
  20.      }  
  21.      ftpClient.readServerResponse();  
  22.     }  
  23.     ftpClient.binary();  
  24.     return true;  
  25.    }catch (IOException e1){  
  26.     e1.printStackTrace();  
  27.     return false;  
  28.    }  
  29. }  
  30.   
  31. /** 
  32.    * ftp上传 
  33.    * 如果服务器段已存在名为filename的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换 
  34.    *  
  35.    * @param filename 要上传的文件(或文件夹)名 
  36.    * @return 
  37.    * @throws Exception 
  38.    */  
  39. public boolean upload(String filename){  
  40.    String newname = "";  
  41.    if(filename.indexOf("/") > -1){  
  42.     newname = filename.substring(filename.lastIndexOf("/") + 1);  
  43.    }else{  
  44.     newname = filename;  
  45.    }  
  46.    return upload(filename, newname);  
  47. }  
  48.   
  49. /** 
  50.    * ftp上传 
  51.    * 如果服务器段已存在名为newName的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换 
  52.    *  
  53.    * @param fileName 要上传的文件(或文件夹)名 
  54.    * @param newName 服务器段要生成的文件(或文件夹)名 
  55.    * @return 
  56.    */  
  57. public boolean upload(String fileName, String newName){  
  58.    try{  
  59.     String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");   
  60.     File file_in = new File(savefilename);//打开本地待长传的文件  
  61.     if(!file_in.exists()){  
  62.      throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");  
  63.     }  
  64.     if(file_in.isDirectory()){  
  65.      upload(file_in.getPath(),newName,ftpClient.pwd());  
  66.     }else{  
  67.      uploadFile(file_in.getPath(),newName);  
  68.     }  
  69.       
  70.     if(is != null){  
  71.      is.close();  
  72.     }  
  73.     if(os != null){  
  74.      os.close();  
  75.     }  
  76.     return true;  
  77.    }catch(Exception e){   
  78.        e.printStackTrace();   
  79.        System.err.println("Exception e in Ftp upload(): " + e.toString());   
  80.        return false;  
  81.    }finally{  
  82.     try{  
  83.      if(is != null){  
  84.       is.close();  
  85.      }  
  86.      if(os != null){   
  87.       os.close();   
  88.      }  
  89.     }catch(IOException e){  
  90.      e.printStackTrace();  
  91.     }   
  92.    }  
  93. }  
  94.   
  95. /** 
  96.    * 真正用于上传的方法 
  97.    * @param fileName 
  98.    * @param newName 
  99.    * @param path 
  100.    * @throws Exception 
  101.    */  
  102. private void upload(String fileName, String newName,String path) throws Exception{  
  103.     String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");   
  104.     File file_in = new File(savefilename);//打开本地待长传的文件  
  105.     if(!file_in.exists()){  
  106.      throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");  
  107.     }  
  108.     if(file_in.isDirectory()){  
  109.      if(!isDirExist(newName)){  
  110.       createDir(newName);  
  111.      }  
  112.      ftpClient.cd(newName);  
  113.      File sourceFile[] = file_in.listFiles();  
  114.      for(int i = 0; i < sourceFile.length; i++){  
  115.          if(!sourceFile[i].exists()){  
  116.         continue;  
  117.          }  
  118.          if(sourceFile[i].isDirectory()){  
  119.         this.upload(sourceFile[i].getPath(),sourceFile[i].getName(),path+"/"+newName);  
  120.          }else{  
  121.         this.uploadFile(sourceFile[i].getPath(),sourceFile[i].getName());  
  122.           }  
  123.         }  
  124.     }else{  
  125.      uploadFile(file_in.getPath(),newName);  
  126.     }  
  127.     ftpClient.cd(path);  
  128. }   
  129. /** 
  130. * upload 上传文件 
  131.  
  132. * @param filename 要上传的文件名 
  133. * @param newname 上传后的新文件名 
  134. * @return -1 文件不存在 >=0 成功上传,返回文件的大小 
  135. * @throws Exception 
  136. */  
  137. public long uploadFile(String filename, String newname) throws Exception{  
  138.    long result = 0;  
  139.    TelnetOutputStream os = null;  
  140.    FileInputStrea

猜你喜欢

转载自blog.csdn.net/qq_44945073/article/details/89437483