如何检查文件是否已经存在于Dropbox的文件、Dropbox

2023-09-07 17:24:45 作者:时间不会说谎

我使用下面的code上传文件到Dropbox的。但我想检查文件上的Dropbox已经存在,以避免重复。所以,我怎么能检查文件是否已经存在或不?由于我是新来的Andr​​oid,我不知道现在该做什么

 公共类UploadFileToDropbox扩展的AsyncTask<虚空,虚空,布尔>
{

    私人DropboxAPI<> Dropbox的;
    私人字符串路径;
    私人上下文的背景下;

    公共UploadFileToDropbox(上下文的背景下,DropboxAPI<>的Dropbox,
                               字符串路径){
        this.context = context.getApplicationContext();
        this.dropbox =保管箱;
        this.path =路径;
    }

    @覆盖
    保护布尔doInBackground(空... PARAMS){
        最终文件TEMPDIR = context.getCacheDir();
        文件临时文件;
        FileWriter的FR;
        尝试 {
            TEMPFILE = File.createTempFile(文件,名.txt,TEMPDIR);
            FR =新的FileWriter(临时文件);
            fr.write(测试文件上传使用Dropbox的API为Android);
            fr.close();

            的FileInputStream的FileInputStream =新的FileInputStream(临时文件);
            dropbox.putFile(路径+为sample.txt的FileInputStream,
                    tempFile.length(),NULL,NULL);
            tempFile.delete();
            返回true;
        }赶上(IOException异常E){
            e.printStackTrace();
        }赶上(DropboxException E){
            e.printStackTrace();
        }
        返回false;
    }

    @覆盖
    保护无效onPostExecute(布尔结果){
        如果(结果){
            Toast.makeText(背景下,文件上传成功!,
                    Toast.LENGTH_LONG).show();
        } 其他 {
            Toast.makeText(背景下,无法上传文件,Toast.LENGTH_LONG)
                    。显示();
        }
    }
}
 

解决方案

 私人无效loadFiles(最后弦乐目录){
        新的Thread(){
            @覆盖
            公共无效的run(){

                字符串的mpath =目录;
                进入direntEx = NULL;
                尝试 {
                    direntEx = mApi.metadata(的mpath,1000,空,真,空);
                }赶上(DropboxException E){
                    e.printStackTrace();
                }

                如果(direntEx.contents.size()!= 0){
                    对于(输入ENT:direntEx.contents){
                        字符串名称= ent.fileName();
                          / *这里比较文件* /

                   }
                 }

                super.run();
            }

        }。开始();

    }
 

internet选项 设置 查看文件 里的临时文件保存在哪里

I am using following code to upload a file to Dropbox. But I want to check if the file exists on Dropbox already, to avoid duplications. So how can I check if a file already exists or not? As I am new to Android, I don't know what to do now

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();
        }
    }
}

解决方案

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();

    }