Reputation: 1184
I tried with the file service to store the data in Google app engine and i successfully uploaded it but later i noted its not storing as blob values. So i googled and got this link How do I handle multipart form data? or How do I handle file uploads to my app? provided by google.
In this document the code says like below,
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
log.warning("Got a form field: " + item.getFieldName());
} else {
log.warning("Got an uploaded file: " + item.getFieldName() +", name = " + item.getName());
// You now have the filename (item.getName() and the
// contents (which you can read from stream). Here we just
// print them back out to the servlet output stream, but you
// will probably want to do something more interesting (for
// example, wrap them in a Blob and commit them to the
// datastore).
Here am not understanding how to wrap them as blob and commit them to the datastore. can anyone suggest me how to solve this. Does this way stores the file as blob values in google app engine?
Upvotes: 1
Views: 482
Reputation: 80330
InputStream is = item.openStream();
try {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime, fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] buffer = new byte[BUFFER_SIZE];
int readBytes;
while ((readBytes = is.read(buffer)) != -1) {
writeChannel.write(ByteBuffer.wrap(buffer, 0, readBytes));
}
writeChannel.closeFinally();
String blobKey = fileService.getBlobKey(file).getKeyString();
} catch (Exception e) {
e.printStackTrace(resp.getWriter());
}
Upvotes: 1