繁体   English   中英

从Dropbox下载文件

[英]File Download from Dropbox

我将从Dropbox下载文件。 他们为此提供了自己的API,但出现了错误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

对于下载,您必须使用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");

希望这对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM