[英]How to check whether a file already exists in Dropbox
I am using following code to upload a file to Dropbox. 我正在使用以下代码将文件上传到Dropbox。 But I want to check if the file exists on Dropbox already, to avoid duplications.
但是我想检查文件是否已经存在于Dropbox上,以避免重复。 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
由于我是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
如果文件存在,则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;
}
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.