Java implements ftp file upload and download to solve the problems of downloading multiple files in slow Chinese

  • 2020-05-10 18:18:05
  • OfStack

Without further ado, I directly attached the code to you, as shown below:


// File upload 
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 The default port is 21
    }
    // A lot of people write with ftp.getReplyCode() To get the return value of the connection , But that leads to storeFileStream return null
    if(ftp.login(username,password))
    {
      ftp.enterLocalActiveMode();
      ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
      // Create a directory , Returns a failure if it exists 
      ftp.makeDirectory(path);
      // Switch directory 
      ftp.changeWorkingDirectory(path);
      // Upload a file  
      //FTP The agreement stipulates that the file encoding format is 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(); 
     }
  }
}
// File download 
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 The default port is 21
    }
    // A lot of people write with ftp.getReplyCode() To get the return value of the connection , But that leads to storeFileStream return 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
      } 
      // Switch directory   It can be judged here , Failure to switch indicates ftp There's no path up here 
      ftp.changeWorkingDirectory(path);
      // Upload a file  
      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();
        // When creating a local file, you need to transfer the encoding back 
        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);
      }
      // This is very important   You have to do this multiple times ftp The channel of the flow , Wait for his orders to be completed 
      ftp.completePendingCommand();
      out.flush();
      out.close();
      ftp.logout();
      sucess=true;
  }
  catch(Exception e)
  {
    
  }
  finally{
     if(ftp.isConnected())
     {
       ftp.disConnecct(); 
     }
  }
}

Related articles: