Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

awideksy's avatar
awideksy
Explorer | Level 4
5 years ago
Solved

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

4 Replies

  • awideksy's avatar
    awideksy
    Explorer | Level 4
    5 years ago

    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's avatar
    himalayantrekkers10
    New member | Level 2
    4 years ago

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

  • Здравко's avatar
    Здравко
    Legendary | Level 20
    4 years ago

    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.

About Dropbox API Support & Feedback

Node avatar for 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!