【问题标题】:Java FTP client not downloading fileJava FTP客户端不下载文件
【发布时间】:2016-04-14 11:20:44
【问题描述】:

我试图使用 FTPClient 从服务器下载一个 zip 文件,我的下载代码是

FTPClient ftpClient = new FTPConnection().makeConnection(loc);

        try {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            success = ftpClient.changeWorkingDirectory(PATH + preset + "/" + file_to_download + offset);
            System.out.println("Download Path:-" + PATH + preset + "/" + file_to_download + offset);
            if (!success) {
                System.out.println("Could not changed the directory to RIBS");
                return;
            } else {
                System.out.println("Directory changed to RIBS");
            }
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file : files) {
                if (file.getName().contains(".zip")) {
                    dfile = file;
                }

            }
            fsize=dfile.getSize();
            fileMap.put("build", dfile.getName());
            primaryStage = (Stage) ap.getScene().getWindow();

            String homePath = System.getProperty("user.home");
            File downloadPath = new File(homePath + "\\Buildss\\" + osVer);
            if (!downloadPath.exists()) {
                if (downloadPath.mkdirs()) {
                    System.out.println("Directory is created!");
                } else {
                    System.out.println("Failed to create directory!");
                }
            }
            // System.out.println(chosenDir.getAbsolutePath());
            filePath = new File(downloadPath + "/" + dfile.getName());
            if (filePath.exists()) {
                System.out.println("File altready exist");
                return;
            }
            else {
                fileMap.put("path", filePath.toString());
                fileMap.put("kind", "RIBS");


                Task downloadTask = new Task<Void>() {
                    @Override
                    public Void call() throws IOException {
                        try {
                            long len = dfile.getSize();
                            System.out.println("File From Server:::::: " + len);
                             downloadFile = new File(downloadPath + "/" + dfile);
                            outputFile = new FileOutputStream(downloadFile);
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        ftpClient.sendNoOp();
                        ftpClient.setConnectTimeout(1000);
                        //ftpClient.retrieveFile(dfile, output);
                     if (ftpClient.retrieveFile(dfile.getName(), outputFile) == true) {
                            downloadButton.setDisable(true);
                            System.out.println("LOCAL FILE LENGTH:-" + downloadFile.length());

                        }
                        return null;
                    }
                };
                Thread t = new Thread(downloadTask);
                t.start();

但是现在如果我去下载位置,我会看到类似“-rwxrwx--- 1 ftp ftp 513235293 Apr 11 06”的内容,但没有下载文件。 这段代码几天前还在工作。

【问题讨论】:

  • retrieveFile() 调用后,您可能需要调用getReplyCode() 来查看上次FTP 回复的回复代码。它将为您提供更多信息。
  • 回复码为 226 表示“在成功处理之前影响数据连接的客户端命令后,服务器在关闭数据连接之前发送 226 回复码。大多数情况下,它表示完成文件传输”
  • 您也可以添加输出(sys out)语句吗?我也会 sysout downloadFile 对象只是为了看看它在哪里写。
  • 哦,谢谢,应该是 downloadFile = new File(downloadPath + "/" + dfile.getName());

标签: java ftp


【解决方案1】:

我认为问题与以下有关

// File dfile
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
    if (file.getName().contains(".zip")) {
    dfile = file;
    }
}
...
File downloadPath = new File(homePath + "\\Buildss\\" + osVer);
...
// File downloadFile;
downloadFile = new File(downloadPath + "/" + dfile);

我假设 dfile 引用了一个代表 Unix 文件的 File 对象。所以downloadPath + "/" + dfile 的串联不会返回你所期望的。

您应该使用downloadPath.getName() + "/" + dfile.getName()。但是您需要从dfile.getName() 中删除一个可能的路径名。首选方法是改用java.nio.file.Path 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 2017-04-14
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多