Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
public String uploadFile(List<File> list, String dropboxPath) throws Exception {
StringBuilder sb = new StringBuilder("");
for (File f : list) {
try (FileInputStream in = new FileInputStream(f)) {
UploadProgress prog = new UploadProgress(f);
FileMetadata metadata = client.files().uploadBuilder(dropboxPath + f.getName()).withMode(WriteMode.ADD)
.withClientModified(new Date(f.lastModified()))
.uploadAndFinish(in, prog);
System.out.println();
System.out.println(f.getName() + "matadata : " + metadata.toStringMultiline());
prog.done();
sb.append(client.sharing().createSharedLinkWithSettings(dropboxPath + f.getName()).getUrl());
sb.append(System.lineSeparator());
} catch (Exception ex) {
System.out.println();
System.out.println("Failed to upload file \"" + f.getName() + "\": " + ex.getMessage());
System.out.println();
throw ex;
}
}
return sb.toString();
}
I've made a method that uploads files and retrieve download links of them.
my question is :
1. I sometimes got this Exception message :
Exception in thread "main" com.dropbox.core.v2.sharing.CreateSharedLinkWithSettingsErrorException: Exception in 2/sharing/create_shared_link_with_settings: {".tag":"shared_link_already_exists","shared_link_already_exists": ... and so on
probably because I used createSharedLinkWithSettings method multiple times testing.
So. How can I grab Existing download link without trying to recreate them?
2. When I click one of the created link to a pdf file, I got a viewer, not a download page.
Is there any way to get a link that gives me a direct download page?
1. To retrieve existing links, you can use listSharedLinks or listSharedLinksBuilder.
2. You can modify shared links for different behaviors, such as direct file content access, using the URL parameters documented here.
1. To retrieve existing links, you can use listSharedLinks or listSharedLinksBuilder.
2. You can modify shared links for different behaviors, such as direct file content access, using the URL parameters documented here.
Thanks for your answer. I'll post my fixed code for those who might have similar problem with mine.
public String uploadFileAndGetLink(List<File> list, String dropboxPath) throws Exception {
System.out.println();
System.out.println("Getting download links...");
StringBuilder sb = new StringBuilder("");
for (File f : list) {
String link;
try {
if(!isLinkExists(dropboxPath + f.getName())) {
System.out.println("Uploading " + f.getName() + "...");
uploadFile(f, dropboxPath);
link = client.sharing().createSharedLinkWithSettings(dropboxPath + f.getName()).getUrl();
} else {
System.out.println(f.getName() + " is already uploaded, retrieving link...");
link = client.sharing().listSharedLinksBuilder().withPath(dropboxPath + f.getName()).start().getLinks().get(0).getUrl();
}
sb.append(link.contains("dl=0") ? link.replace("dl=0", "dl=1") : (link.contains("?") ? link + "&dl=1" : link + "?dl=1"));
sb.append(System.lineSeparator());
} catch (Exception ex) {
System.out.println();
System.out.println("Failed to upload file \"" + f.getName() + "\": " + ex.getMessage());
System.out.println("Try deleting uploaded files...");
System.out.println();
for (int i = 0; i < list.indexOf(f); i++) {
deleteLink(dropboxPath + list.get(i).getName());
}
throw ex;
}
}
System.out.println();
return sb.toString();
}
private void uploadFile(File f, String path) throws Exception {
FileInputStream in = new FileInputStream(f);
UploadProgress prog = new UploadProgress(f);
FileMetadata metadata = client.files().uploadBuilder(path + f.getName()).withMode(WriteMode.ADD)
.withClientModified(new Date(f.lastModified())).uploadAndFinish(in, prog);
System.out.println();
System.out.println(f.getName() + "matadata : " + metadata.toStringMultiline());
prog.done();
in.close();
}
private boolean isLinkExists(String path) {
boolean result = true;
try {
client.files().getMetadata(path);
} catch (Exception e){
result = false;
}
return result;
}
private void deleteLink(String path) {
try {
if(isLinkExists(path)) client.files().deleteV2(path);
} catch (Exception e) {
System.out.println();
System.out.println("Failed to delete uploaded file \"" + path + "\"");
e.printStackTrace();
System.out.println();
}
}
hi i want to know how to create direct download link for ab website file hosted in dropbox
@himalayantrekkers10 wrote:hi i want to know how to create direct download link for ab website file hosted in dropbox
At the beginning can you clarify what's not clear in Greg's post above? There are directions you can use.
In addition you don't need to check if a link exists or not (in such a way you can speed up you application - less calls). If 'createSharedLinkWithSettings' fails because of already existing link, the error (exception) contains this link. You have to just catch it. 😉 Much faster!
Good luck.
The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.
Sound good? Let's get started.Hi there!
If you need more help you can view your support options (expected response time for a ticket is 24 hours), or contact us on Twitter or Facebook.
For more info on available support options, see this article.
If you found the answer to your question, please 'like' the post to say thanks to the user!