Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

michal_k's avatar
michal_k
Explorer | Level 4
10 years ago
Solved

[Java SDK V2] How to find out whether file exists or not

Spoiler
 

Hello,

 

we are migrating from Java SDK API V1 to Java SDK V2 and I am writing some integration tests.

 

For example, after I delete a file I need to check, that this file is no longer present on Drobpox or after upload I need to check, that file is there. What is the recommended way, how to do that?

 

My current ugly workaround is this:

    private boolean fileExistsOnDbx(String dropboxPath, DbxClientWrapper dbxClientWrapper) throws DbxException {
        try{
            dbxClientWrapper.files().getMetadata(dropboxPath);
            return true;
        }catch (GetMetadataErrorException e){
            if (e.getMessage().contains("{\".tag\":\"path\",\"path\":\"not_found\"}")) {
                return false;
            } else {
                throw e;
            }
        }
    }

 

Thank you,

 

Michal

  • You can access the structured error information like this:

     

            try {
                client.files().getMetadata(dropboxPath);
            } catch (GetMetadataErrorException e){
                if (e.errorValue.isPath() && e.errorValue.getPathValue().isNotFound()) {
                    System.out.println("File not found.");
                } else {
                    throw e;
                }
            }

1 Reply

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    You can access the structured error information like this:

     

            try {
                client.files().getMetadata(dropboxPath);
            } catch (GetMetadataErrorException e){
                if (e.errorValue.isPath() && e.errorValue.getPathValue().isNotFound()) {
                    System.out.println("File not found.");
                } else {
                    throw e;
                }
            }

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!