Forum Discussion

Aurelian R.'s avatar
Aurelian R.
New member | Level 1
11 years ago

Download a file from the Dropbox account into an output stream.

Hello
I download a file from Dropbox using the following code from the Dropbox API:

public final OutputStream downloadDropBoxFileAtOutputStream(final String dropBoxFilePath, String localFilePath) {
    OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(localFilePath);
    } catch (FileNotFoundException e) {
        LOGGER.error(e.getMessage());
    }
    try {
        DbxEntry.File downloadedFile = null;
        try {
            downloadedFile = client.getFile(dropBoxFilePath, null,
                    outputStream);
        } catch (DbxException | IOException e) {
            LOGGER.error(e.getMessage());
        }
    } finally {
        try {
            outputStream.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
    }
    return outputStream;
}

I want to pass the resulted output stream to another method without saving the file on the local disk. Is is this possible with the Dropbox core java API ?

Best Regards,
Aurelian

1 Reply

Replies have been turned off for this discussion
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    11 years ago

    The getFile method will just write the file contents to whatever OutputStream you supply, which doesn't necessarily need to be a FileOutputStream like the example in the documentation shows. That being the case, you supply a different kind of OutputStream to send the data to wherever you want. For example, a PipedOutputStream may make sense, depending on your scenario.

    Alternatively, you may want to use startGetFile which returns a DbxClient.Downloader instead, which has an InputStream with the file's contents as the body field, which may suit your use case better.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!