cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn about updates that we've made to the Search experience on the Android and iOS apps?Well, you can learn from Luke on the Mobile App team right 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: 
1
Ask
2
Comments

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 3
	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

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

Greg-DB
Dropboxer

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

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

Greg-DB
Dropboxer

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.

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

awideksy
Explorer | Level 3

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

 

Re: Java api : How to get create direct download link for a file?

himalayantrekkers10
New member | Level 2

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

Re: Java api : How to get create direct download link for a file?

Здравко
Super Collaborator | Level 20

@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.

Who's talking

Top contributors to this post

  • User avatar
    Здравко Super Collaborator | Level 20
  • User avatar
    himalayantrekkers10 New member | Level 2
  • User avatar
    awideksy Explorer | Level 3
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?