How to download multiple files in a zip folder. I am using spring-boot and documents are saved in MongoDB using GridFS.
I was trying to download using FileSystemResource which takes File as an argument taking reference from https://simplesolution.dev/spring-boot-download-multiple-files-as-zip-file/
I tried to get download a resource from mongodb using below line of code and convert it into File object using
gridFsTemplate.getResource(GridFsFile).getFile();
I but it throws an error saying
GridFS resource can not be resolved to an absolute file path.
I have done using ByteArrayResource:
public void downloadZipFile(HttpServletResponse response, List<String> listOfDocIds) {
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=download.zip");
List<FileResponse> listOfFiles = myService.bulkDownload(listOfDocIds);// This call will fetch docs in the form of byte[] based on docIds.
try(ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
for(FileResponse fileName : listOfFiles) {
ByteArrayResource fileSystemResource = new ByteArrayResource(fileName.getFileAsBytes);
ZipEntry zipEntry = new ZipEntry(fileName.getFileName());
zipEntry.setSize(fileSystemResource.contentLength());
zipEntry.setTime(System.currentTimeMillis());
zipOutputStream.putNextEntry(zipEntry);
StreamUtils.copy(fileSystemResource.getInputStream(), zipOutputStream);
zipOutputStream.closeEntry();
}
zipOutputStream.finish();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
class FileResponse{
private String fileName;
private byte[] fileAsBytes;
// setter and getters
}
I have method to upload files from user to S3 bucket. But when I'm testing it, it is both saved on S3 buckend and in my classpath.
This is my function to upload file:
public void uploadFileToNews(byte[] file, String fileName) {
File fileToSave = new File(fileName);
try(FileOutputStream fileOutputStream = new FileOutputStream(fileToSave) ) {
fileOutputStream.write(file);
amazonS3.putObject(new PutObjectRequest("gwnews", fileName, fileToSave));
} catch (IOException e) {
log.error("Error during uploading file to S3", e);
}
}
And this is function in my service:
public News addNewsImage(MultipartFile multipartFile, String newsId) throws IOException {
News news = getById(newsId);
news.setImageUrl(news.getId() + ".png");
fileService.uploadFileToNews(multipartFile.getBytes(), news.getId() + ".png");
return newsRepository.save(news);
}
Am I doing something wrong? How can I avoid saving file to my classpath?
This line is saving the content in your classpath : "fileOutputStream.write(file);"
And the next line is saving it in your S3 bucket for corresponding AWS account.
I'm trying to develop a method in Java that enables creating a folder inside of a specific folder in Google Drive, but what I found in Google documentation (https://developers.google.com/drive/api/v3/folder) is creating only a folder or moving a file to a folder.
Any help would be appreciated!
Just take what the API gave you for creating a folder and inserting a file in a folder and combine them.
From the API site: 'The parents property can be used when creating a folder as well to create a subfolder.'
String folderId = "folderID";
File fileMetadata = new File();
fileMetadata.setName("Invoices");
fileMetadata.setParents(Collections.singletonList(folderId));
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = driveService.files().create(fileMetadata)
.setFields("id, parent")
.execute();
System.out.println("Folder ID: " + file.getId());
You need to create a drive service first. Then you can call createSubFolder method.
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Drive driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
Resource Link: https://developers.google.com/drive/api/v3/quickstart/java
Here,
It is required to give id of parent folder.
And have to give the sub folder name.
public String createSubFolder(String parentFolderId, String subFolderName) {
System.out.println("Sub Folder Name: "+subFolderName);
File file = null;
try {
File fileMetadata = new File();
fileMetadata.setName(subFolderName);
fileMetadata.setMimeType("application/vnd.google-apps.folder");
if (parentFolderId != null) {
List<String> parents = Arrays.asList(parentFolderId);
fileMetadata.setParents(parents);
}
file = driveService.files().create(fileMetadata).setFields("id, name").execute();
System.out.println("Folder ID: " + file.getId());
return file.getId();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
In case you want to create a folder in a shared drive you have to specify following:
drive id (to file metadata)
supports all drives (to the files().create().setFields in builder chain)
private String createFolder(String folderName, String parent) throws IOException {
System.out.println("Creating folder " + folderName + " in " + parent);
File fileMetadata = new File();
fileMetadata.setParents(Arrays.asList(parent));
fileMetadata.setName(folderName);
fileMetadata.setDriveId(DRIVE_ID);
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = drive.files().create(fileMetadata)
.setSupportsAllDrives(true)
.setFields("id")
.execute();
System.out.println("created folder ID: " + file.getId());
return file.getId();
}
I have tried with the following code... Without any luck...
private void updateFile(Drive service, String fileId) {
try {
File file = new File(); /********/
final java.io.File fileToUpdate = new java.io.File("D:/Work Data/Files/pdf.pdf");
FileContent mediaContent = new FileContent("image/pdf", fileToUpdate);
file = service.files().update(fileId, file, mediaContent).execute();
System.out.println(fileId);
} catch (Exception e) {
if (isDebug) {
e.printStackTrace();
}
}
}
also tried with...
private void updateFile(Drive service, String fileId) {
try {
File file = service.files().get(fileId).execute(); /********/
final java.io.File fileToUpdate = new java.io.File("D:/Work Data/Files/pdf.pdf");
FileContent mediaContent = new FileContent("image/pdf", fileToUpdate);
file = service.files().update(fileId, file, mediaContent).execute();
System.out.println(fileId);
} catch (Exception e) {
if (isDebug) {
e.printStackTrace();
}
}
}
With every time i execute the code i get the following stacktrace:
java.lang.IllegalArgumentException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
at com.google.api.client.googleapis.media.MediaHttpUploader.setInitiationRequestMethod(MediaHttpUploader.java:872)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.initializeMediaUpload(AbstractGoogleClientRequest.java:237)
at com.google.api.services.drive.Drive$Files$Update.<init>(Drive.java:3163)
at com.google.api.services.drive.Drive$Files.update(Drive.java:3113)
at com.test.DriveTester.updateFile(DriveTester.java:76)
at com.test.DriveTester.main(DriveTester.java:64)
Can anyone tell what i am doing wrong ? Any sample code for this i.e. updating the content of an already existing file on google drive will be helpful...
For API v3 the solution proposed by Android Enthusiast does not work unfortunately.
The issue is with this bit:
// First retrieve the file from the API.
File file = service.files().get(fileId).execute();
doing this it will create a File object, with it's ID field being set, when executing the update, itt will throw an exception, since the ID meta field is not editable directly.
What you can do is simply create a new file:
File file = new File();
alter the meta you'd like and update file content if required as shown in the example.
then simply update the file as proposed above:
// Send the request to the API.
File updatedFile = service.files().update(fileId, file, mediaContent).execute();
So based a full example would look like this based on Android Enthusiast solution:
private static File updateFile(Drive service, String fileId, String newTitle,
String newDescription, String newMimeType, String newFilename, boolean newRevision) {
try {
// First create a new File.
File file = new File();
// File's new metadata.
file.setTitle(newTitle);
file.setDescription(newDescription);
file.setMimeType(newMimeType);
// File's new content.
java.io.File fileContent = new java.io.File(newFilename);
FileContent mediaContent = new FileContent(newMimeType, fileContent);
// Send the request to the API.
File updatedFile = service.files().update(fileId, file, mediaContent).execute();
return updatedFile;
} catch (IOException e) {
System.out.println("An error occurred: " + e);
return null;
}
}
I can share javascript code for uploading to an already existing file using v3
const url = 'https://www.googleapis.com/upload/drive/v3/files/' + fileId + '?uploadType=media';
if(self.fetch){
var setHeaders = new Headers();
setHeaders.append('Authorization', 'Bearer ' + authToken.access_token);
setHeaders.append('Content-Type', mime);
var setOptions = {
method: 'PATCH',
headers: setHeaders,
body: data
};
fetch(url,setOptions)
.then(response => { if(response.ok){
console.log("save to drive");
}
else{
console.log("Response wast not ok");
}
})
.catch(error => {
console.log("There is an error " + error.message);
});
To update files content, you can use Files:update, this method supports an /upload URI and accepts uploaded media with the following characteristics:
Maximum file size: 5120GB
Accepted Media MIME types: /
This method provides media upload functionality through two separate URIs. For more details, see the document on media upload.
Upload URI, for media upload requests:
PUT https://www.googleapis.com/upload/drive/v2/files/fileId
* Metadata URI, for metadata-only requests:
PUT https://www.googleapis.com/drive/v2/files/fileId
private static File updateFile(Drive service, String fileId, String newTitle,
String newDescription, String newMimeType, String newFilename, boolean newRevision) {
try {
// First retrieve the file from the API.
File file = service.files().get(fileId).execute();
// File's new metadata.
file.setTitle(newTitle);
file.setDescription(newDescription);
file.setMimeType(newMimeType);
// File's new content.
java.io.File fileContent = new java.io.File(newFilename);
FileContent mediaContent = new FileContent(newMimeType, fileContent);
// Send the request to the API.
File updatedFile = service.files().update(fileId, file, mediaContent).execute();
return updatedFile;
} catch (IOException e) {
System.out.println("An error occurred: " + e);
return null;
}
}
You may also check this SO ticket, the ticket discuss the said error.
Source
Unlike the other answers, this solution allows you to upload without having to create a file for the data on the device first.
// Create a File containing any metadata changes.
File metadata = new File().setName(name);
// Convert content to an AbstractInputStreamContent instance.
ByteArrayContent contentStream = ByteArrayContent.fromString("text/plain", content);
// Update the metadata and contents.
mDriveService.files().update(fileId, metadata, contentStream).execute();
I have saved a file into mongoDB using gridFS like so.
Mongo mongo = new Mongo("XXXXX", XXXX);
DB db = mongo.getDB("XXX");
GridFS fs = new GridFS(XXX);
File install = new File("C:\\Users\\Nabeel\\Desktop\\docs.txt");
GridFSInputFile inFile = fs.createFile(install);
inFile.save();
Now I want download that file using spring MVC. I don't seep to able to find example on how i can get the file back as gridFS has converted the file into binary.
An example code would be nice as i am new to all this.
Thanks you in advance
You can do something like this for serving file :
public void downloadFile(String videoId , HttpServletResponse response ) {
InputStream is = null;
ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoDBConfiguration.class);
GridFsOperations gridOperations = (GridFsOperations) ctx.getBean("YourBeanName");
List<GridFSDBFile> result = gridOperations.find(new Query().addCriteria(Criteria.where("_id").is(videoId)));
for (GridFSDBFile file : result) {
try {
/* send file */
} catch (Exception e) {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
}
}
Send file example here: How do I return a video with Spring MVC so that it can be navigated using the html5 <video> tag?
This link about video serving but it can give you an idea. Use method 2 in the link and GridFSDBFile's getInputStream() method.