2014-09-24 43 views
3

我的代碼如下BOX API:如何使用https://api.box.com/2.0/files/{fileId}/content下載

WebResource webResource1 = cl.resource("https://api.box.com/2.0/files/{fileId}/content"); 

ClientResponse res1 = webResource1.header("Authorization", "Bearer"+p1.getAccess_token()).get(ClientResponse.class); 
String jsonStr1 = res1.getEntity(String.class); 

我的回答是給獲得位置屬性響應如下所示 -

{Object-Id=[file_20317568941], Cache-control=[private], Date=[Wed, 24 Sep 2014 12:11:43 GMT], Content-Length=[27], X-Robots-Tag=[noindex, nofollow], Content-Disposition=[attachment;filename="upload.txt";filename*=UTF-8''upload.txt], Accept-Ranges=[bytes, bytes], Connection=[keep-alive], Content-Type=[text/plain; charset=UTF-8], Server=[nginx], X-Content-Type-Options=[nosniff]} 

我得到狀態代碼200, OK;但要獲得location屬性,我需要具有狀態代碼302以及位置url(https://dl.boxcloud.com/*)。

在響應中沒有得到location: https://dl.boxcloud.com/*屬性,我該如何從box api下載文件?

+0

我找不到任何解決方案解決我的問題。任何人都面臨問題,並能夠解決它。請給我建議。提前致謝。 – 2014-10-09 04:48:01

+0

你得到的東西看起來像實際響應的標題。我會建議你檢查你的代碼的語法 - 看看[這個例子](http://stackoverflow.com/questions/18342456/how-to-add-headers-on-restful-call-using-jersey-client -API#18359483)。 – chrisben 2014-10-16 20:53:29

回答

2

上週六我有一些時間來看看你的問題。基本問題是,如果您需要獲取Location值,則需要停止自動重定向。以下是你的問題的解釋&解決方案:的Download a File

引用盒API文檔:

如果該文件可供下載,響應將是302 發現在DL的URL。 boxcloud.com。

維基百科文章上HTTP 302

HTTP響應Found狀態碼302是執行 URL重定向的一種常見的方式。

帶有此狀態碼的HTTP響應將在位置標題字段中額外提供一個URL 。用戶代理(例如網頁瀏覽器) 通過該代碼的響應邀請對第二個請求,否則請求 對位置字段中指定的新URL的請求。

因此,要獲取響應頭中的Location屬性,您需要停止自動重定向。否則,根據box文檔,您將獲取文件的原始數據而不是下載URL。

以下是使用Commons HTTPClient實現的解決方案:

private static void getFileDownloadUrl(String fileId, String accessToken) { 
    try { 
     String url = MessageFormat.format("https://api.box.com/2.0/files/{0}/content", fileId); 
     GetMethod getMethod = new GetMethod(url); 
     getMethod.setFollowRedirects(false); 

     Header header = new Header(); 
     header.setName("Authorization"); 
     header.setValue("Bearer " + accessToken); 
     getMethod.addRequestHeader(header); 

     HttpClient client = new HttpClient(); 
     client.executeMethod(getMethod); 

     System.out.println("Status Code: " + getMethod.getStatusCode()); 
     System.out.println("Location: " + getMethod.getResponseHeader("Location")); 
    } catch (Exception cause) { 
     cause.printStackTrace(); 
    } 
} 

另一種解決方案使用java.net.HttpURLConnection

private static void getFileDownloadUrl(String fileId, String accessToken) { 
    try { 
     String serviceURL = MessageFormat.format("https://api.box.com/2.0/files/{0}/content", fileId); 
     URL url = new URL(serviceURL); 

     HttpURLConnection connection = HttpURLConnection.class.cast(url.openConnection()); 
     connection.setRequestProperty("Authorization", "Bearer " + accessToken); 
     connection.setRequestMethod("GET"); 
     connection.setInstanceFollowRedirects(false); 
     connection.connect(); 

     int statusCode = connection.getResponseCode(); 
     System.out.println("Status Code: " + statusCode); 

     Map<String, List<String>> headerFields = connection.getHeaderFields(); 
     List<String> locations = headerFields.get("Location"); 

     if(locations != null && locations.size() > 0) { 
      System.out.println("Location: " + locations.get(0)); 
     } 
    } catch (Exception cause) { 
     cause.printStackTrace(); 
    } 
} 

由於Commons的HttpClient已過時以下解決方案是基於所述Apache HttpComponents

private static void getFileDownloadUrl(String fileId, String accessToken) { 
    try { 
     String url = MessageFormat.format("https://api.box.com/2.0/files/{0}/content", fileId); 
     CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build(); 
     HttpGet httpGet = new HttpGet(url); 
     BasicHeader header = new BasicHeader("Authorization", "Bearer " + accessToken); 
     httpGet.setHeader(header); 
     CloseableHttpResponse response = client.execute(httpGet); 
     int statusCode = response.getStatusLine().getStatusCode(); 
     System.out.println("Status Code: " + statusCode); 

     org.apache.http.Header[] headers = response.getHeaders(HttpHeaders.LOCATION); 

     if(header != null && headers.length > 0) { 
      System.out.println("Location: " + headers[0]); 
     } 
    } catch (Exception cause) { 
     cause.printStackTrace(); 
    } 
} 
+0

謝謝你的answer.it作品 – 2014-10-23 17:55:35