Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
awideksy
5 years agoExplorer | Level 4
Java api : How to get already-created download link for a file?
public String uploadFile(List<File> list, String dropboxPath) throws Exception {
StringBuilder sb = new StringBuilder("");
for (File f : list) {
try (FileInputStream in = new FileInput...
- 5 years ago
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.
Greg-DB
Dropbox Community Moderator
5 years ago1. 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.
awideksy
5 years agoExplorer | Level 4
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();
}
}
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!