cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Java api : How to get already-created download link for a file?

Java api : How to get already-created download link for a file?

awideksy
Explorer | Level 4
Go to solution
	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 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

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.

View solution in original post

4 Replies 4

Greg-DB
Dropbox Staff
Go to solution

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.

awideksy
Explorer | Level 4
Go to solution

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

 

himalayantrekkers10
New member | Level 2
Go to solution

hi i want to know how to create direct download link for ab website file hosted in dropbox

Здравко
Legendary | Level 20
Go to solution

@himalayantrekkers10 wrote:

hi i want to know how to create direct download link for ab website file hosted in dropbox


Hi @himalayantrekkers10,

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.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    himalayantrekkers10 New member | Level 2
  • User avatar
    awideksy Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?