使用Apache commons的Java FTP抛出“复制时捕获的IOException”

ht4b089n  于 2023-11-21  发布在  Apache
关注(0)|答案(1)|浏览(253)

我做了一个JavaFX应用程序,其中包括上传大文件(> 1GB)到服务器。每次我在同一个地方得到同样的错误。任何建议,我在这里做错了什么。

ftpclient.connect(server, port);
ftpclient.login(ftpuser, ftppass);
ftpclient.enterLocalPassiveMode();
ftpclient.setKeepAlive(true);
ftpclient.setControlKeepAliveTimeout(3000);

Task<Void> copyMnt = new Task<Void>() {
@Override
protected Void call(){
  try {                              
      new Thread(new FTPHandler(ftpclient, source , dest)).run();
  } catch (IOException ex) {
     Logger.getLogger(MyAppController.class.getName()).log(Level.SEVERE, null, ex); 
  }
 return null;
  }
};

new Thread(copyMnt).start();

字符串
现在,在FTPHandler类中

// The constructor will set the ftpclient, source and destinations.
@Override

public void run() {
    try {
        uploadDirectory(this.getClient(), this.getDest(), this.getSrc(), "");
    } catch (IOException ex) {
        Logger.getLogger(FTPHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void uploadDirectory(FTPClient ftpClient,
        String remoteDirPath, String localParentDir, String remoteParentDir)
        throws IOException {

    File localDir = new File(localParentDir);
    File[] subFiles = localDir.listFiles();
    if (subFiles != null && subFiles.length > 0) {
        for (File item : subFiles) {
            String remoteFilePath = remoteDirPath + "/" + remoteParentDir
                    + "/" + item.getName();
            if (remoteParentDir.equals("")) {
                remoteFilePath = remoteDirPath + "/" + item.getName();
            }

            if (item.isFile()) {
                // upload the file
                String localFilePath = item.getAbsolutePath();
                java.util.Date date= new java.util.Date();

                System.out.println(new Timestamp(date.getTime()) + "  : Uploading :: " + localFilePath + " to " + remoteFilePath);
                boolean uploaded = uploadSingleFile(ftpClient,
                        localFilePath, remoteFilePath);
                if (uploaded) {
                    System.out.println("Success : "
                            + remoteFilePath);
                } else {
                    System.out.println("Failed : "
                            + localFilePath);
                }
            } else {
                // create directory on the server
                boolean created = ftpClient.makeDirectory(remoteFilePath);
                if (created) {
                    System.out.println("CREATED the directory: "
                            + remoteFilePath);
                } else {
                    System.out.println("COULD NOT create the directory: "
                            + remoteFilePath);
                }

                // upload the sub directory
                String parent = remoteParentDir + "/" + item.getName();
                if (remoteParentDir.equals("")) {
                    parent = item.getName();
                }

                localParentDir = item.getAbsolutePath();
                uploadDirectory(ftpClient, remoteDirPath, localParentDir,
                        parent);
            }
        }
    }
}


每次我上传的文件(文件是不同类型的,如.iso,.dat等),前几个文件(上传顺序是像前几百个文件较小,即小于几MB,然后最后10个文件是超过1 GB大)将成功上传(即所有较小的文件和最后10个文件中的2个),但当它开始上传第三个大文件时,我得到以下异常。

SEVERE: null
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
    at org.apache.commons.net.io.Util.copyStream(Util.java:134)
    at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)
    at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
    at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)

0x6upsns

0x6upsns1#

CopyStreamException有一个“cause”异常。使用.getCause()检查一下,看看出了什么问题。
参见Util.copyStream方法:

public static final long copyStream(InputStream source, OutputStream dest,
                                    int bufferSize, long streamSize,
                                    CopyStreamListener listener,
                                    boolean flush)
throws CopyStreamException
{
    int bytes;
    long total = 0;
    byte[] buffer = new byte[bufferSize >= 0 ? bufferSize : DEFAULT_COPY_BUFFER_SIZE];

    try
    {
        while ((bytes = source.read(buffer)) != -1)
        {
             ....
        }
    }
    catch (IOException e)
    {
        throw new CopyStreamException("IOException caught while copying.",
                                      total, e);
    }

    return total;
}

字符串
uploadSingleFile函数中,

try
{
     ftpClient.storeFile(...)
}
catch (Exception e)
{
     e.printStackTrace();
     if (e.getCause() != null)
     { 
         e.getCause().printStackTrace();
     }
}

  • 我不懂Java,所以代码可能不是100%正确。

另请参阅Getting full string stack trace including inner exception

相关问题