![](/img/trans.png)
[英]How can I upload a file on dropbox without using a dropbox SDK in Java?
[英]get the sharedlink of an existing file in Dropbox using the dropBox-sdk-java
我想检索 Dropbox 上现有文件的共享文件 url。 我正在使用 dropbox-java-sdk,并且设法为我刚刚上传的文件创建了一个共享链接。 我设法获取现有文件的共享链接的唯一方法是列出所有链接并根据路径获取我想要的链接,如下所示
public void getShareLink(String path) throws DbxApiException, DbxException{
DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
try {
ListSharedLinksResult sharedLinkMetadata = client.sharing().listSharedLinks();
for (SharedLinkMetadata slm: sharedLinkMetadata.getLinks()){
if(slm.getPathLower().equalsIgnoreCase(path)){
System.out.println(slm.getUrl());
return;
}
}
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
} catch (DbxException ex) {
System.out.println(ex);
}
}
没有办法直接获取我想要的文件的url吗? 我只是认为为了获得其中一个而迭代所有项目是一种浪费。
解决方案
如果有人发现这很有用,则此代码为
public String getShareLink(String path) {
DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
try {
ListSharedLinksResult sh=client.sharing().listSharedLinksBuilder()
.withPath(path)
.withDirectOnly(true)
.start();
for (SharedLinkMetadata slm: sh.getLinks()){
return slm.getUrl();
}
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
return null;
} catch (DbxException ex) {
System.out.println(ex);
return null;
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.