2

みなさん、こんにちは私の質問は、ApacheをすべてEclipseで使用してFTPサーバーとFTPクライアントを作成したことです.Javaで書いた別のコード用のjarファイルをアップロードしようとしたときの部分を除いて、すべてが正常に機能しているようです。破損していて、文字通り見つけたすべてのものを試しましたが、うまくいきませんでした。ですから、私を助けてくれる人、または私を助けるように指示してくれる人は誰でも大歓迎です。ありがとうございました。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();
            }

        }



}
4

0 に答える 0