我正在尝试从Google云端硬盘下载文件。常见文件(pdf,jpg)的下载没有任何问题。但是我无法下载Google文件。我收到的空文件没有类型,大小为0。您是否知道可能导致此问题的原因?
public InputStream download(String id) throws CloudServiceException {
try {
File file = service.files()
.get(id)
.execute();
String link = file.getExportLinks().get("application/pdf");
HttpResponse resp = service.getRequestFactory()
.buildGetRequest(
new GenericUrl(link))
.execute();
return resp.getContent();
} catch (HttpResponseException e) {
throw CloudServiceExceptionTransformer.transform(e, e.getStatusCode());
} catch(IOException ex) {
throw new InternalException(ex);
}
}
因此,问题实际上出在建立回应中。Google文件的大小为0,并且无法识别Google媒体类型,从而导致文件损坏。
编辑:这是我的工作版本。我删除了设置的大小,以便它下载那些大小为0的文件。
ResponseEntity.BodyBuilder builder = ResponseEntity.status(HttpStatus.OK)
.header(HttpHeaders.CONTENT_DISPOSITION, encoding)
.contentType(MediaType.parseMediaType(resource.getMimeType()));
return builder.body(new InputStreamResource(resource.getContent()));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句