2017-09-27 64 views
0

我使用谷歌驅動器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與驅動器參數那個時候,我得到這個錯誤,請幫忙提前

+0

請再次確認您顯示的「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

回答

0

我得到了我的問題的答案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(); 
    } 
}