【问题标题】:Java file upload via ftp zero byte errorJava文件通过ftp上传零字节错误
【发布时间】:2011-08-22 13:44:51
【问题描述】:

我将文件上传到服务器,但文件为空。(大小为零字节)

     int reply;
         ftp.connect(server,215);
         ftp.login(username, Password);
         System.out.println("Connected to " + server + ".");
         System.out.print(ftp.getReplyString());
         reply = ftp.getReplyCode();
         if(!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                System.err.println("FTP server refused connection.");
         }
         System.out.println("FTP server connected.");
          ftp.setFileType(FTP.BINARY_FILE_TYPE);
          ftp.enterLocalPassiveMode();

         InputStream input= new FileInputStream(source_file_path);

         ftp.storeFile(dest_dir, input);
         System.out.println(ftp.storeFile(dest_dir, input));
         System.out.println( ftp.getReplyString() );

                            input.close();

                            ftp.logout();

【问题讨论】:

    标签: java file-upload ftp


    【解决方案1】:

    查看您的代码,我认为您正在使用 Jakarta commons net。 如果是真的,试试这个:

    ftp.connect(address, port);
    boolean ft = ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    if(!ft)
        throw new Exception("Error");
    ftp.enterLocalPassiveMode(); or ftp.enterLocalActiveMode();
    boolean log = ftp.login(user, password);
    //if log == true, then u are logged in
    ftp.storeFile(remote, local);
    

    另外,如果你想使用进度条来获取传输进度,你可以试试这个方法而不是 ftp.storeFile

    InputStream stO = new BufferedInputStream(new FileInputStream(file), ftp.getBufferSize());
    OutputStream stD = ftp.storeFileStream(file.getName());
    
                        org.apache.commons.net.io.Util.copyStream(
                        stO,
                        stD,
                        ftp.getBufferSize(),
                        file.length(),
                        new org.apache.commons.net.io.CopyStreamAdapter() 
                        {
                            @Override
                            public void bytesTransferred(long totalBytesTransferred, int bytesTransferred,
                                    long streamSize) 
                            {
                                try
                                {
                                    pb.setMaximum((int)streamSize);
                                    pb.setValue((int)totalBytesTransferred);
                                }
                                catch(Exception ex)
                                {
                                    pb.setMaximum(-1);
                                }
                            }
                        });
                        stO.close();
                        stD.close();
                        boolean ok = ftp.completePendingCommand();
                        if(!ok)
                            throw new Exception("ERROR while sending the file");
    

    PS:pb 是一个 JProgressBar

    【讨论】:

      【解决方案2】:

      您将文件存储两次。

      ftp.storeFile(dest_dir, input);
      System.out.println(ftp.storeFile(dest_dir, input));
      

      由于您没有重新打开输入流,因此您第二次调用 storeFile 方法时,输入流处于 EOF,因此您第二次上传的文件将没有内容,因此会覆盖第一个文件。

      要打印storeFile 的结果,您应该:

      Object result = ftp.storeFile(dest_dir, input);
      System.out.println(result);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-22
        • 2013-03-21
        • 2012-07-29
        • 1970-01-01
        • 2012-04-26
        • 2013-03-24
        • 1970-01-01
        相关资源
        最近更新 更多