gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:02:11 25 4
gpt4 key购买 nike

我正在使用以下代码将文件上传到 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();
}
}
}

最佳答案

If the file exists, then the Entry is not null

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

关于java - 如何检查文件是否已存在于 Dropbox 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31292106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com