Domanda

I am using the apache commons net library to get a file from FTP server.

I don't need to download the whole file but just to read the headers to determine the file size. The library I am using to do this is metadata extractor

The problem is that when I call client.completePendingCommand() it always returns false - however the date variable is printed correctly. I have asked the developer of metadata extractor and he doesn't know why its returning false. Anyone have an explanation? I'm not sure if it is ok just to ignore the false?

FTPClient client = new FTPHTTPClient(proxy settings);
InputStream stream = null;
try {
        client.connect(FTPProperties.getInstance().getProperty("ftp.server"));
        client.login(FTPProperties.getInstance().getProperty("ftp.username"), FTPProperties.getInstance().getProperty("ftp.password"));
        client.enterLocalPassiveMode();

        for (String path : paths) { //paths are the jpeg files to download
            try {
                stream = client.retrieveFileStream(p);

                Metadata metadata = ImageMetadataReader.readMetadata(stream);
                Directory directory = metadata.getDirectory(ExifSubIFDDirectory.class);
                Date date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
                System.out.println("DATE " + date);
            } catch (IOException ex) {
                Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if(stream != null) {
                  stream.close();
                }
                if (in != null) {
                    in.close();
                }
                if (!client.completePendingCommand()) {
                     Logger.getLogger("Error");
                }
            }
        }
    } catch (Exception ex) {
        Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (client != null && client.isConnected()) {
            client.disconnect();
        }
    }
È stato utile?

Soluzione

I don't think you are doing anything wrong and I don't think there is anything wrong with metadata extractor. You may be better off checking the stream you are retrieving can be correctly processed rather than using completePendingCommand() as an indication of success. Metadata extractor may already do this for you by throwing an exception if there is a problem.

Explanation: completePendingCommand() verifies the success of the entire transaction and success or failure relies on the FTPClients reply code being within the range of 200 <= replyCode < 300 (http://commons.apache.org/proper/commons-net/apidocs/src-html/org/apache/commons/net/ftp/FTPReply.html#line.133).

I had a similar problem and found that my FTPClient object had a reply code of 150 which indicates that according to the FTP server, the transaction has not completed. Reply code 150 is a positive preliminary reply but is not classified as a positive completion reply (https://www.rfc-editor.org/rfc/rfc959 page 37). My point is that the response was still positive and although I thought I have completed the transaction, the FTP server still thinks I need to do something. This may be an issue with org.apache.commons.net.ftp.FTPClient or the FTP server it is interacting with.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top