从Dropbox下载文件

时间:2017-04-10 16:02:47

标签: java json dropbox-api resttemplate

我打算从Dropbox下载文件。他们为此提供了API,但我得到 ERROR 400

我的代码是:

RestTemplate restTemplate = new RestTemplate();
RestCall re= new RestCall();
ClientHttpRequestInterceptor ri =  re.new LoggingRequestInterceptor();
List<ClientHttpRequestInterceptor> ris = new ArrayList<>();
ris.add(ri);
restTemplate.setInterceptors(ris);
PathEntity pathEntity = new PathEntity();
pathEntity.setPath("/Apps/DemoVivekApp/home/xpointers/Desktop/dmo/test");

ObjectMapper objectMapper = new ObjectMapper();

String jsonString = objectMapper.writeValueAsString(pathEntity);

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
httpHeaders.add("Authorization","Bearer ItdjL2a2ihAAAAAAAAAAFd3cJhYxpQhbtlXjgb6RleJ7xVQj_Pon");
httpHeaders.add("Dropbox-API-Arg",jsonString);
httpHeaders.add("User-Agent", "api-explorer-client");

HttpEntity<PathEntity> entity = new HttpEntity<>(httpHeaders);

ResponseEntity<byte[]> response = restTemplate.exchange(DIRECT_FILE_DOWNLOAD, HttpMethod.POST, entity, byte[].class, "1");  

PathEntity.java:

public class PathEntity {

    private String path;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}

我尝试打印我的http请求,打印如下:

{Accept=[application/octet-stream, application/json, application/*+json, */*], Content-Type=[application/json], Authorization=[Bearer ItdjL2a2ihAAAAAAAAAAFd3cJhYxpQhbtlXjgb6RleJ7xVQj_Pon], Dropbox-API-Arg=[{path":"/newApp/ccJAR}], User-Agent=[api-explorer-client], Content-Length=[0]}

更新

我的回复正文是:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://content.dropboxapi.com/2/files/download": Server returned HTTP response code: 400 for URL: https://content.dropboxapi.com/2/files/download; nested exception is java.io.IOException: Server returned HTTP response code: 400 for URL: https://content.dropboxapi.com/2/files/download
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:607)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)
at com.demo.RestCall.main(RestCall.java:134)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://content.dropboxapi.com/2/files/download
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at org.springframework.http.client.SimpleClientHttpResponse.getBody(SimpleClientHttpResponse.java:81)
    at com.demo.RestCall$LoggingRequestInterceptor.log(RestCall.java:199)
    at com.demo.RestCall$LoggingRequestInterceptor.intercept(RestCall.java:192)
    at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:85)
    at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:69)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:596)
    ... 3 more

1 个答案:

答案 0 :(得分:0)

要下载,您必须使用GET请求而不是发布。

ResponseEntity<byte[]> response = restTemplate.exchange(DIRECT_FILE_DOWNLOAD, HttpMethod.POST, entity, byte[].class, "1");

更改如下

ResponseEntity<byte[]> response = restTemplate.exchange(downloadFilePath, HttpMethod.GET, entity, byte[].class, "1");

希望这会对您有所帮助。