我正在使用Google Drive v3并尝试使用RestTemplate上传文件,但我收到以下错误: -
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)
我的文件上传方法:
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);
}
}
以下网址用于上传文件:
&#34; https://www.googleapis.com/drive/v3/files?uploadType=multipart&#34 ;;
当我用驱动器作为参数调用此方法时,我得到该错误,请帮助我。谢谢提前
答案 0 :(得分:0)
我得到了我的问题的答案,方法是使用resttemplates更新google驱动器上的文件,
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();
}
}