Trying to download a file using Dropbox Java API in the GAE - java

I have an XML file on Dropbox that I want to access from my Google App Engine using the Dropbox Java API. After a bit of playing around I find the GAE doesn't support FileOutputStream.
FileOutputStream outputStream = new FileOutputStream("myFile.txt");
try {
DbxEntry.File downloadedFile = client.getFile("/myFile.txt", null,
outputStream);
System.out.println("Metadata: " + downloadedFile.toString());
}
Any ideas how I can get the XML data into my GAE (client or server side) from Dropbox?
Thanks
Tim

Got it! Thanks. ByteArrayOutputStream worked. So for anyone else trying to read a DropBox file in a Google App Engine environment (i.e. read into memory), here is what worked for me
String fileName = "myfile.xml"; OutputStream out = new ByteArrayOutputStream();
try {
dbxClient.getFile("/" + fileName, null, out);
} catch (DbxException e) {
e.printStackTrace();
}
System.out.println("File Contente: " + out.toString());

Related

OutOfMemory Error in downloading file from Google Drive using Java SDK API

I am using google drive JAVA SDK API to download the files below is the code, getGoogleDriveServiceInstance() method will return Drive instance in the below code. This code works perfect for small files but but for files > 500 MB i am getting Out of Memory Error, I need some help how to fix this issue.
public boolean downloadFile(String fileId, java.io.File path) {
FileOutputStream fos=null;
try{
fos = new FileOutputStream(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
getGoogleDriveServiceInstance().files().get(fileId).executeMediaAndDownloadTo(baos);
baos.writeTo(fos);
} catch(Exception ex) {
ex.printStackTrace();
return false;
} finally {
try{fos.close();fos=null;} catch(Exception ex){}
}
return true;
}
If the file size is big, below error is coming->
Exception in thread "Thread-22" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2271)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140)
at com.google.api.client.util.ByteStreams.copy(ByteStreams.java:55)
at com.google.api.client.util.IOUtils.copy(IOUtils.java:94)
at com.google.api.client.util.IOUtils.copy(IOUtils.java:63)
at com.google.api.client.googleapis.media.MediaHttpDownloader.executeCurrentRequest(MediaHttpDownloader.java:247)
at com.google.api.client.googleapis.media.MediaHttpDownloader.download(MediaHttpDownloader.java:199)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeMediaAndDownloadTo(AbstractGoogleClientRequest.java:562)
at com.google.api.services.drive.Drive$Files$Get.executeMediaAndDownloadTo(Drive.java:3560)
See https://developers.google.com/api-client-library/java/google-api-java-client/media-download which says "When you download a large media file from a server, use resumable media download to download the file chunk by chunk".

Can not access RenderedImage class file for java.awt.image.RenderedImage not found

I am developing an android app that has a module for OCR ,and after taking sometime to find API for doing that ,i have found a certain API from internet. I have downloaded their jar file and imported (com.asprise.ocr.Ocr) it on my project. My intention is to use the library to extract the information from an image in my SD and display them on my android application, But when i run the project i get the error below:
Error:(137, 35) error: cannot access RenderedImage
class file for java.awt.image.RenderedImage not found
And from the logs it gives me the information that the error comes from the line below of my code script:
String s = ocr.recognize(new File[]{file}, Ocr.RECOGNIZE_TYPE_ALL, `Ocr.OUTPUT_FORMAT_PLAINTEXT);
`
And here is my android code part:
try {
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
viewImage.setImageBitmap(bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
///////////////////////////////////////////
Ocr.setUp(); // one time setup
Ocr ocr = new Ocr(); // create a new OCR engine
ocr.startEngine("eng", Ocr.SPEED_FASTEST); // English
String s = ocr.recognize(new File[]{file}, Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT);
System.out.println("Result: " + s);
Toast.makeText(getBaseContext() ,"IMAGE INFORMATION: "+s ,Toast.LENGTH_SHORT).show();
// ocr more images here ...
ocr.stopEngine();
//////////////////////////////
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Can anyone assist me on how to solve the issue from the above android code script
Ok,
Do you know that there's no such class as java.awt.image.RenderedImage in standard Android API?
Java on Android is not the same Java as on regular PC, it doesn't have all the same classes and packages, you know that right?
Because of that not all java libraries are out-of-the-box compatible with Android.
If you could copy the source code of java.awt.image.RenderedImage and all of it dependecies with the same package names as in regular java into your application, then maybe you could fix those incomaptibility errors. The trick is to add the same classes that are needed by the library to your Android app, but probably you'll need to edit those sources to be Android compatible anyway, so it will be probably more work than just copy of few sources from a standard "PC" java.
You could try to search for an dedicated OCR for android instead.
Or if you insist on this library then you need to make it work yourself.

Is there a way to upload file to Google Drive from InputStream in Drive SDK?

I have to load some data to Google Drive, but I can't use recommended by Drive SDK way to do it:
FileContent fileContent = new FileContent(mimeType, dataFile);
try {
File file = service.files().insert(body, fileContent).execute();
return file;
} catch (IOException e) {
System.out.println("An error occured: " + e);
return null;
}
because my data isn't always has the java.io.File as a source. Sometimes it may be an InputStream from encrypted storage or from other cloud storage and therefore I can't get FileContent from it. Is there a way to load data from InputStream to Google Drive without their intermediate storing on file system (as for Dropbox API method "putFileOverwrite", for example)?
Check this out
File file= drive.files().insert(body,
new InputStreamContent(
fileItemStream
.getContentType(),
new ByteArrayInputStream(
IOUtils.toByteArray(fileInputStream)))).execute;

download the files from Gdrive to local system by using java

Drive Quickstart: Run a Drive App in Java example works for uploading files fine. I want to download the files from Gdrive to local system by using java.
For download they are given a method
private static InputStream downloadFile(Drive service, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp =
service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
The above method,how can i give inputs? and from where i give the inputs? Can anyone give a complete code for download like Quickstart upload class.
any help will be appreciated.
you can use google drive api and send Http get request, you can see this tutorial
https://developers.google.com/drive/manage-downloads
Thanks Hanan it works fine.By using the retrieveAllFiles() i can list all the documents.And i have stored the retrieved documents in my local by using this below code.Is it a correct way to download.
for(File f:result){
i++;
System.out.println("File Name==>"+f.getTitle());
System.out.println("File Id==>"+f.getId());
System.out.println("File ext==>"+f.getFileExtension());
System.out.println("File size==>"+f.getFileSize());
InputStream in = downloadFile(service,f);
byte b[] = new byte[in.available()];
in.read(b);
java.io.File ff = new java.io.File("/home/test/Desktop/gdocs/"+f.getTitle());
FileOutputStream fout = new FileOutputStream(ff);
fout.write(b);
fout.close();
}
It stores all the documents in local. The text (.txt) files are open properly in my local, but the image files or pdf files are not open properly.It gives some error messages like file corrupted. There is no content in the image or pdf documents how can i get content and store it. Any suggestions

Google App Engine for Java and Google Cloud Storage

I have an Google App Engine (Java) based application which stores the file data in Google Cloud storage.
This file download servlet works fine in my local eclipse environment and when deployed to appspot domain, this works for simple text files but for any documents (displayed in the browser in a new tab), but if I try with any other binary files (doc, jpeg, gif etc) seem to do nothing, no error is thrown as well at the server side . I checked directly in the file folders in Google Cloud storage, files are stored properly and able to access it directly, but cannot do so via the app engine.
Can you please let me know if I am missing something?
The code snippet below,
try {
FileService newfileService = FileServiceFactory.getFileService();
AppEngineFile file = new AppEngineFile(cloudpath) ;
FileReadChannel channel = newfileService.openReadChannel(file, false);
BufferedInputStream bis = new BufferedInputStream(Channels.newInputStream(channel));
BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());
resp.setHeader("Content-Disposition", "inline;filename=\"" + file.getNamePart() + "");
int b = 0;
while((b = bis.read()) != -1) {
bos.write(b);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
}
Instead of trying to stream the file yourself you should use the BlobstoreService.serve method. This takes care or streaming and can be used on files of any size.
Something like
BlobstoreService blobService = BlobstoreServiceFactory.getBlobstoreService();
blobService.serve(blobService.createGsBlobKey(cloudpath), resp);
you'll try the following order of statements.
...
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition", "inline;filename=\"" + file.getNamePart() + "");
BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());
int b=0;
...

Categories