如何检查Dropbox中是否已存在文件

时间:2015-07-08 12:04:19

标签: java android dropbox dropbox-api

我使用以下代码将文件上传到Dropbox。但我想检查Dropbox上是否存在该文件,以避免重复。那么如何检查文件是否已经存在?由于我是Android的新手,我不知道该怎么做

public class UploadFileToDropbox extends AsyncTask<Void, Void, Boolean>
{

    private DropboxAPI<?> dropbox;
    private String path;
    private Context context;

    public UploadFileToDropbox(Context context, DropboxAPI<?> dropbox,
                               String path) {
        this.context = context.getApplicationContext();
        this.dropbox = dropbox;
        this.path = path;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        final File tempDir = context.getCacheDir();
        File tempFile;
        FileWriter fr;
        try {
            tempFile = File.createTempFile("file", ".txt", tempDir);
            fr = new FileWriter(tempFile);
            fr.write("Test file uploaded using Dropbox API for Android");
            fr.close();

            FileInputStream fileInputStream = new FileInputStream(tempFile);
            dropbox.putFile(path + "sample.txt", fileInputStream,
                    tempFile.length(), null, null);
            tempFile.delete();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DropboxException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            Toast.makeText(context, "File Uploaded Successfully!",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "Failed to upload file", Toast.LENGTH_LONG)
                    .show();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

private void loadFiles(final String directory) {
        new Thread() {
            @Override
            public void run() {

                String mPath = directory;
                Entry direntEx = null;
                try {
                    direntEx = mApi.metadata(mPath, 1000, null, true, null);
                } catch (DropboxException e) {
                    e.printStackTrace();
                }

                if (direntEx.contents.size() != 0) {
                    for (Entry ent : direntEx.contents) {
                        String name = ent.fileName();
                          /*Compare file here*/

                   }
                 }

                super.run();
            }

        }.start();

    }

答案 1 :(得分:0)

  

如果文件存在,则Entry不为空

 public boolean isExists(String path) {
            boolean ret = false;
            try {
                Entry existingEntry = metadata(path, 1, null, false, null);
                if (existingEntry != null) {
                    ret = true;
                }
            } catch (DropboxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ret = false;
            }
            return ret;
        }