从Dropbox下载文件

问题描述 投票:0回答:1

我打算从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");  

path entity.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
java json dropbox-api resttemplate
1个回答
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");

希望这会对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.