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. + diff --git a/ResumableUpload.java b/ResumableUpload.java index 5952e15..a17e371 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 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(); @@ -51,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; @@ -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,35 +75,33 @@ 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 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(); } /** * 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,41 +114,37 @@ 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 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(); } /** * 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 +153,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 static 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 +163,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 +171,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 +179,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 static 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 +189,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); } } @@ -211,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() {