从java代码中检索文件

从java代码中检索文件,java,ftp-client,apache-commons-net,Java,Ftp Client,Apache Commons Net,我正在尝试从ftp检索文件。我遇到以下异常。 有时对同一文件的并发访问可能会出现这种情况。我的防火墙已关闭 我的问题是 由于无法下载必要的文件,我的程序无法正常工作,如何应对此异常 我的下载代码: public boolean downloadFile(String sourceFilePath, String destinationDirPath, String newFileName) throws IllegalStateException { //

我正在尝试从ftp检索文件。我遇到以下异常。 有时对同一文件的并发访问可能会出现这种情况。我的防火墙已关闭

我的问题是 由于无法下载必要的文件,我的程序无法正常工作,如何应对此异常

我的下载代码:

    public boolean downloadFile(String sourceFilePath, String destinationDirPath, String newFileName)
            throws IllegalStateException {

//      final FTPClient client = this.getClient();
        FileOutputStream output = null;
        try {
            client.setFileType(FTP.BINARY_FILE_TYPE);
            // The remote filename to be downloaded.
            String filename = new File(sourceFilePath).getName();
            // Downloads the selected file to the C drive
            output = new FileOutputStream(destinationDirPath + File.separator + newFileName);
            this.cd(new File(sourceFilePath).getParent());
            // Download file from FTP server
            boolean result = client.retrieveFile(filename, output);
            log.trace("File is downloaded from server:" + filename + ", to destination:"+newFileName);
            return result;
        }
        // Indicate that an exception is occurred while downloading file
        catch (Exception ioe) {
            throw new FileTransferException("Could not download file", ioe);
        } finally {
            if (output != null) {
                try {
                    // trying to close FileOutputStream
                    output.close();
                } catch (IOException e) {
                    log.warn("Error closing writer to file:"+newFileName, e);
                }
                output = null;
            }
        }
    }
这是堆栈跟踪:

Exception during data transfer, closing data connection socket
    java.net.SocketException: Software caused connection abort: socket write error
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
        at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
        at java.io.BufferedOutputStream.write(BufferedOutputStream.java:109)
        at org.apache.ftpserver.impl.IODataConnection.transfer(IODataConnection.java:289)
        at org.apache.ftpserver.impl.IODataConnection.transferToClient(IODataConnection.java:161)
        at org.apache.ftpserver.command.impl.RETR.execute(RETR.java:166)
        at org.apache.ftpserver.impl.DefaultFtpHandler.messageReceived(DefaultFtpHandler.java:210)
        at org.apache.ftpserver.listener.nio.FtpHandlerAdapter.messageReceived(FtpHandlerAdapter.java:61)
        at 

您没有说明异常是什么,也没有提出实际问题。请也向我们展示
客户端的初始化。你接电话了吗?您确定这个函数没有被两个线程调用吗?您可能需要使用PASV模式,具体取决于您所使用的防火墙/NAT。什么是客户端?你能显示它的代码吗?@Vivek从标签上看,我猜是。但是代码中可能有很多我们看不到的误用。。。