错误而由RestTemplates
问题描述:
我使用谷歌驱动器V3,并试图上传使用RestTemplate文件谷歌驱动V3上传文件,但我得到了以下错误: -错误而由RestTemplates
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
at com.bostonbyte.thelift.utils.GoogleDriveUtil.uploadFileByRest(GoogleDriveUtil.java:411)
at com.bostonbyte.thelift.utils.GoogleDriveDown.main(GoogleDriveDown.java:19)
2017-09-27 11:37:27 [main] ERROR c.b.thelift.utils.GoogleDriveUtil -
Exception in getLatestFileChanges method of org.springframework.web.client.HttpClientErrorException
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
at com.bostonbyte.thelift.utils.GoogleDriveUtil.uploadFileByRest(GoogleDriveUtil.java:411)
at com.bostonbyte.thelift.utils.GoogleDriveDown.main(GoogleDriveDown.java:19)
我FileUploading方法:
static void uploadFileByRest() {
try {
String accessToken = GoogleDriveCredentialsProvider.getAccessToken();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("file", new ClassPathResource("demo.txt"));
map.add("name", "demo");
httpHeaders.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA);
httpHeaders.add(Constant.Header.AUTH_TOKEN, BEARER + accessToken);
HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(
map, httpHeaders);
ResponseEntity<String> responseEntity;
responseEntity = restTemplate.exchange(Constant.Google.UPLOAD_FILE, HttpMethod.POST, requestEntity,
String.class);
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(""+responseEntity.getBody());
} catch (Exception e) {
LOGGER.error("Exception in getLatestFileChanges method of " + e.getClass().getName(), e);
}
}
下列网址我使用上传的文件:
" https://www.googleapis.com/drive/v3/files?uploadType=multipart ";
当我把这个米ethod与驱动器参数那个时候,我得到这个错误,请帮忙提前
答
我得到了我的问题的答案me.Thanks,下面的方法更新文件对谷歌使用resttemplates驱动,
public static void updateFile() {
Drive drive = getService();
File fileMetadata = new File();
fileMetadata.setName("Harshad");
fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");
java.io.File filePath = new java.io.File("/home/xpointers/convertcsv.csv");
FileContent mediaContent = new FileContent("text/csv", filePath);
// Send the request to the API.
try {
File updatedFile = drive.files().update(fileId, fileMetadata, mediaContent).execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
请再次确认您显示的“https://www.googleapis.com/drive/v3/files?uploadType = multipart”的端点。如果您真的使用它,那么对https://www.googleapis.com/upload/drive/v3/files?uploadType = multipart'进行修改如何?在多部分请求中,使用https://www.googleapis.com/upload/drive/v3/files?uploadType = multipart'作为端点。您可以在[这里](https://developers.google.com/drive/v3/web/multipart-upload#sending_a_multipart_upload_request)查看详细信息。如果这些信息对你没有用处,我很抱歉。 – Tanaike