2015-01-06 79 views
0

我使用commans.net api来执行任务,例如上传和 从ftp服务器下载文件。我可以成功执行这些任务 ,但下载的文件已损坏。我无法以通常的方式打开 这些文件。请帮助我..JAVA Commons-Net FTPClient,下载文件已损坏

我简单的代码看起来像

public class FTPConnect { 

public static void main(String[] args) { 
    startServer(); 

    connectClient(); 

} 

private static void startServer() { 

    FtpServerFactory serverFactory = new FtpServerFactory(); 

    ListenerFactory factory = new ListenerFactory(); 
    factory.setPort(21);// set the port of the listener (choose your desired 
         // port, not 1234) 
    System.out.println("port has been set to 21"); 
    serverFactory.addListener("default", factory.createListener()); 
    PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); 
    userManagerFactory.setFile(new File("lib/users.properties")); 

    System.out.println("users.properties has been set.."); 
    userManagerFactory.setPasswordEncryptor(new PasswordEncryptor() { 

       @Override 
       public String encrypt(String password) { 
        return password; 
       } 

       @Override 
       public boolean matches(String passwordToCheck, 
         String storedPassword) { 
        return passwordToCheck.equals(storedPassword); 
       } 
      }); 
    System.out.println("password has been encrypted..."); 
    BaseUser user = new BaseUser(); 
    user.setName("java"); 
    user.setPassword("shiva.dave"); 
    System.out.println("password has been set..to java and shiva.dave"); 
    user.setHomeDirectory("lib"); 
    System.out.println("home directory has been set..."); 
    List<Authority> authorities = new ArrayList<Authority>(); 
    authorities.add(new WritePermission()); 
    user.setAuthorities(authorities); 
    UserManager um = userManagerFactory.createUserManager(); 
    try { 
     um.save(user);// Save the user to the user list on the filesystem 
     System.out.println("user has been set to the filesystem.."); 
    } catch (FtpException e1) { 
     e1.printStackTrace(); 
    } 
    serverFactory.setUserManager(um); 
    FtpServer server = serverFactory.createServer(); 
    try 
    { 
     server.start();//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set 
     System.out.println("Server has been started....."); 
    } 
    catch (FtpException ex) 
    { 
     ex.printStackTrace(); 
    } 

} 

private static void connectClient() { 

    FTPClient client = new FTPClient(); 
    try{ 
     client.connect(InetAddress.getLocalHost(), 21); 
     String loging_success = client.login("java", "shiva.dave") == true ? "success" : "failed"; 
     System.out.println("login: " + loging_success); 
     FTPFile[] clients = client.listFiles("/"); 
     System.out.println("Listed " + clients.length + " files."); 
     for (FTPFile file : clients) { 
      System.out.println(file.getName()); 
     } 

     for (FTPFile ftpFile : clients) { 
      String remoteFile2 = ftpFile.getName(); 
      File downloadFile2 = new File("D:/test2/"+ftpFile.getName()); 
      OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2)); 
      InputStream inputStream = client.retrieveFileStream(remoteFile2); 
      byte[] bytesArray = new byte[4096]; 
      int bytesRead = -1; 
      while ((bytesRead = inputStream.read(bytesArray)) != -1) { 
       outputStream2.write(bytesArray, 0, bytesRead); 
      } 

      boolean success = client.completePendingCommand(); 
      if (success) { 
       System.out.println("File #2 has been downloaded successfully."); 
      } 
      outputStream2.close(); 
      inputStream.close(); 
     } 

    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
    finally{ 
     try{ 
      client.logout(); 
      client.disconnect(); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

} 
} 

回答

3

我创立了一个解决方案,并希望在这里放.. 首先,当您尝试从FTP服务器下载文件,你必须指定文件类型和传输模式才能成功下载文件。

这里是你需要插入的代码。

client.setFileType(FTP.BINARY_FILE_TYPE,FTP.BINARY_FILE_TYPE); client.setFileTransferMode(FTP.BINARY_FILE_TYPE);

“看它佬”

必须需要插入后成功登录这两行代码,否则你不会将能够成功下载文件...

希望这个答案将帮助你下载文件而不被损坏..

+0

这里是检查所有文件类型和模式的文档:https://commons.apache.org/proper/commons-net/apidocs/constant-values.html #org.apache.commons.net.ftp.FTP.RECORD_STRUCTURE –