从远程Ftp服务器下载文件(文件下载为空)

时间:2013-05-08 11:07:05

标签: java ftp remote-server

我正在努力使用Java(FTPClient)从远程ftp服务器下载文件。该文件已成功下载,但它是空的,我没有找到我的问题的解决方案

这是我的代码

FTPClient ftpClient = new FTPClient();
                 try {

                        ftpClient.connect(ip, port);
                        ftpClient.login(user, pass);
                        ftpClient.enterLocalPassiveMode();
                        ftpClient.epsv();
                        ftpClient.mlsd();

                        File downloadFile = new File("contextFolder/test.txt");
                        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
                        InputStream inputStream = ftpClient.retrieveFileStream(remoteFile);

                        byte[] bytesArray = new byte[4096];
                        int length;
                        //copy the file content in bytes 
                        while ((length = inputStream.read(bytesArray)) > 0){

                            outputStream.write(bytesArray, 0, length);

                        }

                        Boolean success = ftpClient.completePendingCommand();

                        outputStream.close();
                        inputStream.close();
                        if (success) {
                            System.out.println("File "+remoteFile+" has been downloaded successfully.");
                        }


            }catch(Exception ex){}

我需要将文件保存到contextFolder中,名称为test.txt :)谢谢大家:)


这是我得到的例外

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.read(Unknown Source)
    at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:314)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:556)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:605)
    at org.apache.commons.net.ftp.FTP.pasv(FTP.java:956)
    at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:806)
    at org.apache.commons.net.ftp.FTPClient._retrieveFileStream(FTPClient.java:1853)
    at org.apache.commons.net.ftp.FTPClient.retrieveFileStream(FTPClient.java:1844)
    at com.ericsson.etl.module.Activity1.execute(Activity1.java:49)
    at com.ericsson.etl.SequenceProcessor.doActivities(SequenceProcessor.java:130)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

2 个答案:

答案 0 :(得分:1)

您是否收到“已下载文件XXX”消息?如果没有,可能的原因是抛出异常,并由此静默丢弃:

} catch (Exception ex){}

你永远不应该抓住这样的例外。

答案 1 :(得分:0)

关闭前请尝试outputStream.flush()

如果这没有帮助,您应该检查是否通过try块中的ex.printStackTrace()抛出任何异常。您经常会找到原因(例如,写入权限,连接错误,......)。