From f6754b86866554ff66849c3a21608892239cad2c Mon Sep 17 00:00:00 2001 From: Rajnish Mishra <47335719+darajnish@users.noreply.github.com> Date: Fri, 18 Dec 2020 11:21:24 +0530 Subject: [PATCH 1/4] HttpServletRequest and HttpServletResponse are irrelevant --- ResumableUpload.java | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/ResumableUpload.java b/ResumableUpload.java index 5952e15..7e8d873 100644 --- a/ResumableUpload.java +++ b/ResumableUpload.java @@ -7,8 +7,6 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Locale; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; /** * A helpful java library/class for uploading large files in chunks on google drive. @@ -26,8 +24,6 @@ public class ResumableUpload /** * This function returns url to which file is to be uploaded - * @param request HttpServletRequest - * @param response HttpServletResponse * @param credential google credential for AccessToken * @param jsonStructure It will be used to get structure of file it should contain * 1) MimeType of file @@ -37,7 +33,7 @@ public class ResumableUpload * @throws MalformedURLException * @throws IOException */ - public String requestUploadUrl(HttpServletRequest request, HttpServletResponse response, Credential credential, com.google.api.services.drive.model.File jsonStructure) throws MalformedURLException, IOException + public String requestUploadUrl(Credential credential, com.google.api.services.drive.model.File jsonStructure) throws MalformedURLException, IOException { URL url = new URL("https://www.googleapis.com/upload/drive/v3/files"+((jsonStructure.getId() != null)?"/"+jsonStructure.getId():"")+"?uploadType=resumable"); HttpURLConnection req = (HttpURLConnection) url.openConnection(); @@ -67,8 +63,6 @@ public class ResumableUpload /** * Uploads String packet - * @param request HttpServletRequest - * @param response HttpServletResponse * @param sessionUri Last Session Url * @param jsonStructure It will be used to get structure of file it should contain * 1) MimeType of file @@ -81,7 +75,7 @@ public class ResumableUpload * @throws MalformedURLException * @throws IOException */ - public int uploadStringPacket(HttpServletRequest request, HttpServletResponse response, String sessionUri, com.google.api.services.drive.model.File jsonStructure, String packet, long chunkStart, long uploadBytes) throws MalformedURLException, IOException + public int uploadStringPacket(String sessionUri, com.google.api.services.drive.model.File jsonStructure, String packet, long chunkStart, long uploadBytes) throws MalformedURLException, IOException { URL url1 = new URL(sessionUri); HttpURLConnection req1 = (HttpURLConnection) url1.openConnection(); @@ -108,8 +102,6 @@ public class ResumableUpload /** * Upload java.io.File packet - * @param request HttpServletRequest - * @param response HttpServletResponse * @param sessionUri Last Session Url * @param jsonStructure It will be used to get structure of file it should contain * 1) MimeType of file @@ -122,7 +114,7 @@ public class ResumableUpload * @throws MalformedURLException * @throws IOException */ - public int uploadFilePacket(HttpServletRequest request, HttpServletResponse response, String sessionUri, com.google.api.services.drive.model.File jsonStructure, java.io.File file, long chunkStart, long uploadBytes) throws MalformedURLException, IOException + public int uploadFilePacket(String sessionUri, com.google.api.services.drive.model.File jsonStructure, java.io.File file, long chunkStart, long uploadBytes) throws MalformedURLException, IOException { URL url1 = new URL(sessionUri); HttpURLConnection req1 = (HttpURLConnection) url1.openConnection(); @@ -155,8 +147,6 @@ public class ResumableUpload /** * Upload File * Upload java.io.File packet - * @param request HttpServletRequest - * @param response HttpServletResponse * @param credential google credential for AccessToken * @param jsonStructure It will be used to get structure of file it should contain * 1) MimeType of file @@ -165,9 +155,9 @@ public class ResumableUpload * @param file File to upload * @throws IOException */ - public void uploadFile(HttpServletRequest request, HttpServletResponse response, Credential credential, com.google.api.services.drive.model.File jsonStructure, java.io.File file) throws IOException, UploadFileException + public void uploadFile(Credential credential, com.google.api.services.drive.model.File jsonStructure, java.io.File file) throws IOException, UploadFileException { - String sessionUrl = requestUploadUrl(request, response, credential, jsonStructure); + String sessionUrl = requestUploadUrl(credential, jsonStructure); for(long i = 1, j = CHUNK_LIMIT;i <= jsonStructure.getSize();i+=CHUNK_LIMIT) { @@ -175,7 +165,7 @@ public class ResumableUpload { j = jsonStructure.getSize() - i + 1; } - int responseCode = uploadFilePacket(request, response, sessionUrl, jsonStructure, file, i-1, j); + int responseCode = uploadFilePacket(sessionUrl, jsonStructure, file, i-1, j); if(!(responseCode == OK || responseCode == CREATED || responseCode == INCOMPLETE)) throw new UploadFileException(responseCode); } } @@ -183,8 +173,6 @@ public class ResumableUpload /** * * Upload String - * @param request HttpServletRequest - * @param response HttpServletResponse * @param credential google credential for AccessToken * @param jsonStructure It will be used to get structure of file it should contain * 1) MimeType of file @@ -193,9 +181,9 @@ public class ResumableUpload * @param text Text to upload * @throws IOException */ - public void uploadString(HttpServletRequest request, HttpServletResponse response, Credential credential, com.google.api.services.drive.model.File jsonStructure, String text) throws IOException, UploadFileException + public void uploadString(Credential credential, com.google.api.services.drive.model.File jsonStructure, String text) throws IOException, UploadFileException { - String sessionUrl = requestUploadUrl(request, response, credential, jsonStructure); + String sessionUrl = requestUploadUrl(credential, jsonStructure); for(long i = 1, j = CHUNK_LIMIT;i <= jsonStructure.getSize();i+=CHUNK_LIMIT) { @@ -203,7 +191,7 @@ public class ResumableUpload { j = jsonStructure.getSize() - i + 1; } - int responseCode = uploadStringPacket(request, response, sessionUrl, jsonStructure, text, i-1, j); + int responseCode = uploadStringPacket(sessionUrl, jsonStructure, text, i-1, j); if(!(responseCode == OK || responseCode == CREATED || responseCode == INCOMPLETE)) throw new UploadFileException(responseCode); } } From 4090f5e1b8f2e284993561fc062ce0455c4ba289 Mon Sep 17 00:00:00 2001 From: Rajnish Mishra <47335719+darajnish@users.noreply.github.com> Date: Fri, 18 Dec 2020 11:54:41 +0530 Subject: [PATCH 2/4] Added a few more fixes --- ResumableUpload.java | 84 +++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/ResumableUpload.java b/ResumableUpload.java index 7e8d873..19c48d4 100644 --- a/ResumableUpload.java +++ b/ResumableUpload.java @@ -33,7 +33,7 @@ public class ResumableUpload * @throws MalformedURLException * @throws IOException */ - public String requestUploadUrl(Credential credential, com.google.api.services.drive.model.File jsonStructure) throws MalformedURLException, IOException + public static String requestUploadUrl(Credential credential, com.google.api.services.drive.model.File jsonStructure) throws MalformedURLException, IOException { URL url = new URL("https://www.googleapis.com/upload/drive/v3/files"+((jsonStructure.getId() != null)?"/"+jsonStructure.getId():"")+"?uploadType=resumable"); HttpURLConnection req = (HttpURLConnection) url.openConnection(); @@ -47,9 +47,9 @@ public class ResumableUpload String body = "{ \"name\": \""+jsonStructure.getName()+"\""+((jsonStructure.getParents() != null)?", \"parents\":[\""+jsonStructure.getParents().get(0)+"\"]":"")+" }"; req.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body.getBytes().length)); - OutputStream outputStream = req.getOutputStream(); - outputStream.write(body.getBytes()); - outputStream.close(); + try (OutputStream outputStream = req.getOutputStream()) { + outputStream.write(body.getBytes()); + } req.connect(); String sessionUri = null; @@ -75,29 +75,29 @@ public class ResumableUpload * @throws MalformedURLException * @throws IOException */ - public int uploadStringPacket(String sessionUri, com.google.api.services.drive.model.File jsonStructure, String packet, long chunkStart, long uploadBytes) throws MalformedURLException, IOException + public static int uploadStringPacket(String sessionUri, com.google.api.services.drive.model.File jsonStructure, String packet, long chunkStart, long uploadBytes) throws MalformedURLException, IOException { - URL url1 = new URL(sessionUri); - HttpURLConnection req1 = (HttpURLConnection) url1.openConnection(); + URL url = new URL(sessionUri); + HttpURLConnection req = (HttpURLConnection) url.openConnection(); - req1.setRequestMethod("PUT"); - req1.setDoOutput(true); - req1.setDoInput(true); - req1.setConnectTimeout(10000); + req.setRequestMethod("PUT"); + req.setDoOutput(true); + req.setDoInput(true); + req.setConnectTimeout(10000); - req1.setRequestProperty("Content-Type", jsonStructure.getMimeType()); - req1.setRequestProperty("Content-Length", String.valueOf(uploadBytes)); - req1.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadBytes -1) + "/" + jsonStructure.getSize()); + req.setRequestProperty("Content-Type", jsonStructure.getMimeType()); + req.setRequestProperty("Content-Length", String.valueOf(uploadBytes)); + req.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadBytes -1) + "/" + jsonStructure.getSize()); byte[] buffer = packet.substring((int)chunkStart, (int)(chunkStart + uploadBytes)).getBytes(); - OutputStream outputStream1 = req1.getOutputStream(); - outputStream1.write(buffer); - outputStream1.close(); + try (OutputStream outputStream = req.getOutputStream()) { + outputStream.write(buffer); + } - req1.connect(); + req.connect(); - return req1.getResponseCode(); + return req.getResponseCode(); } /** @@ -114,34 +114,32 @@ public class ResumableUpload * @throws MalformedURLException * @throws IOException */ - public int uploadFilePacket(String sessionUri, com.google.api.services.drive.model.File jsonStructure, java.io.File file, long chunkStart, long uploadBytes) throws MalformedURLException, IOException + public static int uploadFilePacket(String sessionUri, com.google.api.services.drive.model.File jsonStructure, java.io.File file, long chunkStart, long uploadBytes) throws MalformedURLException, IOException { URL url1 = new URL(sessionUri); - HttpURLConnection req1 = (HttpURLConnection) url1.openConnection(); + HttpURLConnection req = (HttpURLConnection) url1.openConnection(); - req1.setRequestMethod("PUT"); - req1.setDoOutput(true); - req1.setDoInput(true); - req1.setConnectTimeout(10000); + req.setRequestMethod("PUT"); + req.setDoOutput(true); + req.setDoInput(true); + req.setConnectTimeout(10000); - req1.setRequestProperty("Content-Type", jsonStructure.getMimeType()); - req1.setRequestProperty("Content-Length", String.valueOf(uploadBytes)); - req1.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadBytes -1) + "/" + jsonStructure.getSize()); + req.setRequestProperty("Content-Type", jsonStructure.getMimeType()); + req.setRequestProperty("Content-Length", String.valueOf(uploadBytes)); + req.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadBytes -1) + "/" + jsonStructure.getSize()); - OutputStream outstream = req1.getOutputStream(); + try (OutputStream outstream = req.getOutputStream()) { + byte[] buffer = new byte[(int) uploadBytes]; + try (FileInputStream fileInputStream = new FileInputStream(file)) { + fileInputStream.getChannel().position(chunkStart); + if (fileInputStream.read(buffer, 0, (int) uploadBytes) != -1) + outstream.write(buffer); + } + } + + req.connect(); - byte[] buffer = new byte[(int) uploadBytes]; - FileInputStream fileInputStream = new FileInputStream(file); - fileInputStream.getChannel().position(chunkStart); - if (fileInputStream.read(buffer, 0, (int) uploadBytes) == -1); - fileInputStream.close(); - - outstream.write(buffer); - outstream.close(); - - req1.connect(); - - return req1.getResponseCode(); + return req.getResponseCode(); } /** @@ -155,7 +153,7 @@ public class ResumableUpload * @param file File to upload * @throws IOException */ - public void uploadFile(Credential credential, com.google.api.services.drive.model.File jsonStructure, java.io.File file) throws IOException, UploadFileException + public static void uploadFile(Credential credential, com.google.api.services.drive.model.File jsonStructure, java.io.File file) throws IOException, UploadFileException { String sessionUrl = requestUploadUrl(credential, jsonStructure); @@ -199,7 +197,7 @@ public class ResumableUpload /** * Exception thrown when there is a problem while uploading file */ - public class UploadFileException extends Exception + public static class UploadFileException extends Exception { public UploadFileException() { From 477e4050b773c393dcedea58512bd967edbb3d4a Mon Sep 17 00:00:00 2001 From: Rajnish Mishra <47335719+darajnish@users.noreply.github.com> Date: Fri, 18 Dec 2020 12:35:13 +0530 Subject: [PATCH 3/4] Update README.md --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 0101e1f..ed5ff71 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ # google-drive-ResumableUpload A helpful java library/class for uploading large files in chunks on google drive. + +## Example usage for uploading a file + +Here's an example for uploading a java.io.File to google drive. + +```java +// Make sure we've already got Google OAuth Credentials object `credential` and +// java.io.File object `file` for the desired file to be uploaded. + +// Now create the metadata google drive file with the name, mime-type and size +File fmeta = new File(); +fmeta.setName("My Report"); +fmeta.setMimeType("application/vnd.google-apps.spreadsheet"); +fmeta.setSize(file.length()); // `file` is the object for the desired java.io.File + +// Now, use uploadFile() method to start uploading the file in multi-part requests. +ResumableUpload.uploadFile(credential, fmeta, file); +``` +## How this works + + - At, first we use the `requestUploadUrl()` to create a new request to the google drive api for +our requirement of uploading a file. This method returns the url retured by the google api, which we'll be +using to upload the file data. + - Then we can use `uploadFilePacket()` to upload a part of a file or `uploadStringPacket()` to upload + a part of any string as a file data. This step can repeated for each part of part of the file. + From a56c7ad3f5ae7493810620e6d59de6d73651d4fb Mon Sep 17 00:00:00 2001 From: Rajnish Mishra <47335719+darajnish@users.noreply.github.com> Date: Fri, 18 Dec 2020 12:35:56 +0530 Subject: [PATCH 4/4] Update ResumableUpload.java --- ResumableUpload.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ResumableUpload.java b/ResumableUpload.java index 19c48d4..a17e371 100644 --- a/ResumableUpload.java +++ b/ResumableUpload.java @@ -179,7 +179,7 @@ public class ResumableUpload * @param text Text to upload * @throws IOException */ - public void uploadString(Credential credential, com.google.api.services.drive.model.File jsonStructure, String text) throws IOException, UploadFileException + public static void uploadString(Credential credential, com.google.api.services.drive.model.File jsonStructure, String text) throws IOException, UploadFileException { String sessionUrl = requestUploadUrl(credential, jsonStructure);