I'm trying to upload a largefile.zip to my OneDrive using the API, but it's not working and it returns 'Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: Java heap space.' Only the upload session is successful, and uploading the bytes to the upload session is not working.
@RequestMapping(value = "/UploadFile", method = RequestMethod.POST)
public void UpFileOD( @RequestBody RequestUploadFile f ) {
device_managementResponse response = new device_managementResponse();
// GET https://graph.microsoft.com/v1.0/users/+mail to get user_id
final String user_id = "2ada8e58-8d32-42d0-9928-9cbc3ae"; // Can change (depend on the app)
RestTemplate restTemplate = new RestTemplate();
try {
// Create Upload Session
//File file = new File(file_name.getAbsoluteFile().toString());
File file = new File(f.file_name);
if (!file.isFile())
throw new Exception();
long file_size = file.length();
//final String session_url = "https://graph.microsoft.com/v1.0/users/"+user_id+"/drive/items/root"+path+"/"+file.getName()+":/createUploadSession";
final String session_url = "https://graph.microsoft.com/v1.0/users/"+user_id+"/drive/items/root:/"+file.getName()+":/createUploadSession";
// Create request header
HttpHeaders session_header = new HttpHeaders();
session_header.setContentType(MediaType.APPLICATION_JSON);
session_header.add("Authorization", "Bearer " +AccessToken());
// Create request body
JSONObject json_request_body_nested = new JSONObject();
json_request_body_nested.put("@odata.type","microsoft.graph.driveItemUploadableProperties");
json_request_body_nested.put("@microsoft.graph.conflictBehavior","rename");
json_request_body_nested.put("name",file.getName());
JSONObject json_request_body = new JSONObject();
json_request_body.put("item", json_request_body_nested);
// Create HttpEntity
HttpEntity<JSONObject> session_entity = new HttpEntity<>(json_request_body,session_header );
// Call Session Upload API
ResponseEntity<String> session_upload = restTemplate.exchange(session_url, HttpMethod.PUT,session_entity,String.class );
// Map response body of Session Upload
ObjectMapper session_upload_map = new ObjectMapper();
SessionUpload sessionUpload_obj = session_upload_map.readValue(session_upload.getBody(),SessionUpload.class);
// Upload file
// URL of upload file depends on the response of the upload session
FileSystemResource fileSystemResource = new FileSystemResource(file);
// Create request header
HttpHeaders upload_header = new HttpHeaders();
upload_header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
upload_header.setContentLength(file_size);
// upload_header.add("Content-Range","bytes 0-"+(file_size-1)+"/"+file_size);
upload_header.add("Content-Range","*/"+file_size);
// Create entity
HttpEntity<FileSystemResource> upload_entity = new HttpEntity<>(fileSystemResource,upload_header);
// Call API
ResponseEntity<String> uploading_file = restTemplate.exchange(sessionUpload_obj.getUploadURL(),HttpMethod.PUT,upload_entity,String.class);
// System.out.println("Upload Success "+ "File Name: "+ file.getName() +" Path: "+path);
System.out.println("Upload Success "+ "File Name: "+ file.getName());
response.setRespcode(ResponseMessage.KEY_RC_SUCCESS);
response.setMessage(ResponseMessage.getMessage(ResponseMessage.KEY_RC_SUCCESS));
} catch (Exception e) {
e.printStackTrace();
response.setRespcode(ResponseMessage.KEY_RC_EXCEPTION);
response.setMessage(e.getMessage());
}
}
I tried to set spring.servlet.multipart.max-file-size = 3000MB spring.servlet.multipart.max-request-size = 3000MB and changed the RestTemplate to HttpURLConnection but it still does not work. I also try to set the heap size but it still does not work. I think this error is caused by creating an object that is larger than the heap size of JVM.