java實現ftp文件上傳下載,解決慢,中文亂碼,多個文件下載等問題


//文件上傳
public static boolean uploadToFTP(String url,int port,String username,String password,String path,String filename,InputStream input)
{
  boolean success=false;
  FTPClient ftp=new FTPClient();//org.apache.commons.net.ftp
  try{
    if(port>-1)
    {
      ftp.connect(url,port);  
    }else{
      ftp.connect(url);//ftp默認的端口是21
    }
    //很多人寫的是用ftp.getReplyCode()給獲取連接的返回值,但是這樣會導致storeFileStream返回null
    if(ftp.login(username,password))
    {
      ftp.enterLocalActiveMode();
      ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
      //創建目錄,如果存在會返回失敗
      ftp.makeDirectory(path);
      //切換目錄
      ftp.changeWorkingDirectory(path);
      //上傳文件  
      //FTP協議規定文件編碼格式為ISO-8859-1
      filename=new String(filename.getBytes("GBK"),"ISO-8859-1");
      OutputStream out=ftp.storeFileStream(filename);
      byte[]byteArray=new byte[4096];
      int read=0;
      while((read=input.read(byteArray))!=-1)
      {
        out.write(byteArray,0,read);
      }
      out.close();
      ftp.logout();
      sucess=true;
    }  
  }
  catch(Exception e)
  {
    
  }
  finally{
     if(ftp.isConnected())
     {
       ftp.disConnecct(); 
     }
  }
}

//文件下載
public static boolean downloadFromFTP(String url,int port,String username,String password,String path,String localpath)
{
  boolean success=false;
  FTPClient ftp=new FTPClient();//org.apache.commons.net.ftp
  try{
    int reply;
    if(port>-1)
    {
      ftp.connect(url,port);  
    }else{
      ftp.connect(url);//ftp默認的端口是21
    }
    //很多人寫的是用ftp.getReplyCode()給獲取連接的返回值,但是這樣會導致storeFileStream返回null
    ftp.login(username,password)
        
      ftp.enterLocalActiveMode();
      ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
      reply=ftp.getReplyCode();
      if(!FTPReply.isPositionCompletion(reply))
      {
        ftp.disconnect();
        return success;s
      } 
      //切換目錄  此處可以判斷,切換失敗就說明ftp上面沒有這個路徑
      ftp.changeWorkingDirectory(path);
      //上傳文件  
      FTPFile[]fs=ftp.listFiles();
      OutputStream out=null;
      InputStream in=null;
      for(int i=0;i<fs.length;i++)
      {
        FTPFile ff=fs[i];
        String outFileName=ff.getName();
        //創建本地的文件時候要把編碼格式轉回來
        String localFileName=new String(ff.getName().getBytes("ISO-8859-"),"GBK");
        File localFile=new File(localpath+lcoalFileName);
        out=new FileOutputStream(localFile);
        in=ftp.retrieveFileStream(outFileName); 
        byte[]byteArray=new byte[4096];
        int read=0;
      while((read=in.read(byteArray))!=-1)
      {
        out.write(byteArray,0,read);
      }
      //這句很重要  要多次操作這個ftp的流的通道,要等他的每次命令完成
      ftp.completePendingCommand();
      out.flush();
      out.close();
      ftp.logout();
      sucess=true;

  }
  catch(Exception e)
  {
    
  }
  finally{
     if(ftp.isConnected())
     {
       ftp.disConnecct(); 
     }
  }
}


上面代碼都在博客園編輯器手敲的可能會有些錯誤,上面兩種方式速度都很塊  比上傳用storeFile   下載用retrieveFile這種方法快很多

  

 
        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM