Apache FTPClient和Apache FTPServer(JAVA)

时间:2013-04-29 18:06:18

标签: java apache jar ftp corrupt

大家好我的问题是我在eclipse中使用Apache创建了一个FTP服务器和FTPClient,除了我试图上传一个jar文件的部分以外,一切似乎工作正常,这是我用java编写的另一个代码但它一直腐败,我真的尝试了我能找到的一切,但它不会起作用。所以任何可以帮助我或指导我帮助的人都将不胜感激。 谢谢。我已经再次附加了我的Apache FTPServer代码和FTPCLient,谢谢你。

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<String, Ftplet>();

     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<Authority>();
     authorities.add(new WritePermission());
     user.setAuthorities(authorities);
     um = userManagerFactory.createUserManager();
     try{
         um.save(user);
     } catch(FtpException e){

     }
    return um;
}

这是FTPCLient

private static void showServerReply(FTPClient ftpClient){
    String[] replies = ftpClient.getReplyStrings();
    if(replies != null && replies.length > 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();
            }

        }



}

0 个答案:

没有答案