Search code examples
javaapidownloaddropbox.app

Java Dropbox Api - Download *.app


I've coded a Class called DropboxHandler. This class manages all the stuff that interacts directly with my Dropbox account. This class has some methods like uploading and downloading a file, listing all files in a folder, and so on. Everything works fine, except with *.app Files. I know, that these are folders, but i cannot find out, how to download and save them on my HD. Here is my method to download an Folder/ File

public static void downloadFolder(String fileToDownload, String tempFileName) throws IOException {
    FileOutputStream outputStream = new FileOutputStream(tempFileName);
    try {
        DbxEntry.WithChildren listing = client.getMetadataWithChildren(fileToDownload);
        for (DbxEntry child : listing.children) {
            if (child instanceof DbxEntry.Folder) {
                (new File(tempFileName)).mkdirs();
                downloadFolder(fileToDownload + "/" + child.name, tempFileName + "/" + child.name);
            } else if (child instanceof DbxEntry.File) {
                DbxEntry.File downloadedFile = client.getFile(fileToDownload, null, outputStream);
                System.out.println("Metadata: " + downloadedFile.toString());
                System.out.println("Downloaded: " + downloadedFile.toString());
            }
        }

    } catch (DbxException e) {
        System.out.println(e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } finally {
        outputStream.close();
        System.out.println("Download finished");
    }
}

When i run my code, it creates a Folder called Launcher.app (Launcher is the File to download). But when it should download the content of the Launcher, the FileOutputStream fires an error that says that Launcher.app/Content isn't a Folder.

So maybe anyone has some Ideas, how to download the *.app "Files"

Greetings


Solution

  • There are a number of issues with the code you posted. The one you're hitting now is that the very first line of the method creates a file with the same name as the folder you want to write to.

    I think that the next issue you'll hit is where you call getFile. It looks like you're trying to save every file into the same output stream. So essentially you're creating a file (instead of a folder) called Launcher.app and then writing the contents of every file into that file (maybe concatenated together as one big file).

    I took a stab at fixing the code, but I haven't tested it at all. (I don't even know if it compiles.) See if this helps:

    // recursively download a folder from Dropbox to the local file system
    public static void downloadFolder(String path, String destination) throws IOException {
        new File(destination).mkdirs();
        try {
            for (DbxEntry child : client.getMetadataWithChildren(path).children) {
                if (child instanceof DbxEntry.Folder) {
                    // recurse
                    downloadFolder(path + "/" + child.name, destination + "/" + child.name);
                } else if (child instanceof DbxEntry.File) {
                    // download an individual file
                    OutputStream outputStream = new FileOutputStream(
                        destination + "/" + child.name);
                    try {
                        DbxEntry.File downloadedFile = client.getFile(
                            path + "/" + child.name, null, outputStream);
                    } finally {
                        outputStream.close();
                    }
                }
            }
        } catch (DbxException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }