title: Apache FTPClient and Apache FTPServer (JAVA)
body: <p>Hello everyone my question is that I've created an FTP Server and FTPClient using Apache all in eclipse and everything seems to be working fine except the part when i try to upload a jar file which is for another code i wrote in java but it keeps corrupting and I've literally tried everything that i could find but it wouldn't work. So anybody who can help me or direct me to help would be greatly appreciated.
  Thank You. I've attached both my Apache FTPServer code and FTPCLient again Thank You in advance.</p>
  
  <pre><code>serverFactory = new FtpServerFactory();
      factory = new ListenerFactory();
      factory.setPort(3232);
      factory.setIdleTimeout(3000);
  
      serverFactory.addListener("default", factory.createListener());
      //serverFactory.setUserManager(addUser("jjj"));
  
      userManagerFactory = new PropertiesUserManagerFactory();
      //userManagerFactory.setFile(new File("C:/Users/Jean-Jacques/myusers.properties"));
  
      userManagerFactory.setPasswordEncryptor(new PasswordEncryptor ()
      {
  
          public String encrypt(String password){
              return password;
          }
  
          public boolean matches(String passwordTocheck, String storedpassword){
              return passwordTocheck.equals(storedpassword);
          }
      });
  
  
  
       serverFactory.setUserManager(addUser("jjj","hello"));
       m = new HashMap&lt;String, Ftplet&gt;();
  
       m.put("miaFtplet", new Ftplet()
       {
  
          @Override
          public FtpletResult afterCommand(FtpSession session, FtpRequest request,
                  FtpReply reply) throws FtpException, IOException {
              // TODO Auto-generated method stub
              return FtpletResult.DEFAULT;
          }
  
          @Override
          public FtpletResult beforeCommand(FtpSession session, FtpRequest request)
                  throws FtpException, IOException {
              // TODO Auto-generated method stub
              return FtpletResult.DEFAULT;
          }
  
          @Override
          public void destroy() {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void init(FtpletContext ftpletContext) throws FtpException {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public FtpletResult onConnect(FtpSession session) throws FtpException,
                  IOException {
              // TODO Auto-generated method stub
              return FtpletResult.DEFAULT;
          }
  
          @Override
          public FtpletResult onDisconnect(FtpSession session)
                  throws FtpException, IOException {
              // TODO Auto-generated method stub
              return FtpletResult.DEFAULT;
          }
  
       });
  
       server = serverFactory.createServer();
       try{
           server.start();
       }catch(FtpException ex){
           System.out.println("sorrty bro");
       }
  }
  
  public UserManager addUser(String person, String pass) {
      // TODO Auto-generated method stub
      user = new BaseUser();
      user.setName(person);
      user.setPassword(pass);
  
      //user.setHomeDirectory("C:/Users/Jean-Jacques/New  Folder"); //by taking this out you can create any directory you want
      userManagerFactory.setFile(new File("C:/Users/Jean-Jacques/myusers.properties"));
       authorities = new ArrayList&lt;Authority&gt;();
       authorities.add(new WritePermission());
       user.setAuthorities(authorities);
       um = userManagerFactory.createUserManager();
       try{
           um.save(user);
       } catch(FtpException e){
  
       }
      return um;
  }
  </code></pre>
  
  <p>here is the FTPCLient</p>
  
  <pre><code>private static void showServerReply(FTPClient ftpClient){
      String[] replies = ftpClient.getReplyStrings();
      if(replies != null &amp;&amp; replies.length &gt; 0){
          for (String aReply : replies) {
              System.out.println("Server: " + aReply);
          }
      }
  }
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      String server = "localhost";
      int port = 3232;
      String user = "jjj";
      String pass = "hello";
      FTPClient ftpClient = new FTPClient();
  
      try {
  
          ftpClient.connect(server, port);
          ftpClient.enterLocalPassiveMode();
          ftpClient.login(user, pass);
          if(ftpClient.setFileType(FTP.BINARY_FILE_TYPE)){
              System.out.print("bbbbbb");
          }
  
  
  
  
  
          ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
  
  
  
          showServerReply(ftpClient);
  
  
          System.out.println("binary");
  
          int replyCode = ftpClient.getReplyCode();
          if(!FTPReply.isPositiveCompletion(replyCode)) {
              System.out.println("Operation failed. server reply code: " + replyCode);
              return;
          }
          boolean gone = ftpClient.changeWorkingDirectory("/upload"); // this removes directory from root
          if(gone){
              System.out.println("gone");
          } else{
  
          }
  
  
  
  
  
  
  
          File dod = new File("C:\\Users\\Jean-Jacques\\New Folder\\BattleShip.jar");
          String file = "BattleShip.jar";
          //try{
  
          input = new FileInputStream(dod);
  
  
  
          System.out.println("its uploading");
          OutputStream output = ftpClient.storeFileStream(file);
          byte[] bytein = new byte[8192];
          int read =0;
          while((read = input.read(bytein)) != -1){
              output.write(bytein, 0, read);
          }
          input.close();
          output.close();
      boolean yo = ftpClient.completePendingCommand();
      if(yo){
          System.out.println("yo");
      }
  
      } catch (IOException ex) {
          System.out.println("Oops! ");
          ex.printStackTrace();
      }
      finally{
          System.out.println("finally");
  
              try {
                  if(ftpClient.isConnected()){
  
                  ftpClient.logout();
                  ftpClient.disconnect();
                  System.out.println("connected");}
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
          }
  
  
  
  }
  </code></pre>