Here is my summary after series of trials
1. I can't use the Blobstore api because I'm using GWT for the view
2. From my understanding of the Fileservice API, it requires that I need to
have gotten the bytes
of the data before it can be written to the FileWriteChannel. One option is
through the Blobstore
API, but like I said, I'm unable to use Blobstore with GWT. Though it is
possible via RPC, but its
implementation will allow a refresh to another url such as /serve?blob-key.
This will break my
design design pattern as all I want is that after the upload, the user
remains on the same GWT Place.
I don't know how to do this. I need help.
3. I tried to the following code, but i wasn't able to persist the bytes in
the buffer:
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws IOException, ServletException {
HttpSession session = request.getSession();
log.info(request.toString());
try {
ServletFileUpload upload = new ServletFileUpload();
response.setContentType("multipart/form-data");
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
log.info("logging stream " + stream.toString());
if (item.isFormField()) {
log.warning("Got a form field: " + item.getFieldName());
} else {
log.warning("Got an uploaded file: " + item.getFieldName() +
", name = " + item.getName());
int len;
byte[] buffer = new byte[100000];
len = stream.read(buffer, 0, buffer.length);
ListingFile listingfile = new ListingFile();
listingfile.setTitle(item.getName());
listingfile.setImageType(item.getContentType());
listingfile.setImage(buffer); // setImage holds byte[] in the
Entity. Couldn't be persisted
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(listingfile);
}
finally {
pm.close();
}
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
}
Regards
On Thu, Apr 7, 2011 at 5:48 AM, Robert Kluin <[email protected]> wrote:
> Hi,
> You can't stream data from App Engine. The response is buffered
> until your servlet returns before sending the response.
> http://code.google.com/appengine/docs/java/runtime.html#Responses
>
> You might want to look at using the Blobstore. As Simon mentioned,
> check out the FileService, perhaps it would be useful for building the
> blobs.
>
>
> Robert
>
>
>
>
>
> On Wed, Apr 6, 2011 at 08:23, Kayode Odeyemi <[email protected]> wrote:
> > Hello,
> > In a nutshell, since GAE cannot write to a filesystem, I have decided to
> > persist my data into the
> > datastore (using JDO). Now, I will like to retrieve the data byte by byte
> > and pass it to the client
> > as an input stream. There's code from the gwtupload library (see below)
> > which breaks on GAE
> > because it writes to the system filesystem. I'll like to be able to
> provide
> > a GAE ported solution.
> >
> > public static void copyFromInputStreamToOutputStream(InputStream in,
> > OutputStream out) throws IOException {
> > byte[] buffer = new byte[100000];
> > while (true) {
> > synchronized (buffer) {
> > int amountRead = in.read(buffer);
> > if (amountRead == -1) {
> > break;
> > }
> > out.write(buffer, 0, amountRead);
> > }
> > }
> > in.close();
> > out.flush();
> > out.close();
> > }
> > One work around I have tried (didn't work) is to retrieve the data from
> the
> > datastore as a resource like this:
> > InputStream resourceAsStream = null;
> > PersistenceManager pm = PMF.get().getPersistenceManager();
> > try {
> > Query q = pm.newQuery(ImageFile.class);
> > lf = q.execute();
> > resourceAsStream =
> getServletContext().getResourceAsStream((String)
> > pm.getObjectById(lf));
> > } finally {
> > pm.close();
> > }
> > if (lf != null) {
> > response.setContentType(receivedContentTypes.get(fieldName));
> > copyFromInputStreamToOutputStream(resourceAsStream,
> > response.getOutputStream());
> > }
> > I welcome your suggestions.
> > Regards
> > --
> > Odeyemi 'Kayode O.
> > http://www.sinati.com
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google App Engine" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> > http://groups.google.com/group/google-appengine?hl=en.
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
>
>
--
Odeyemi 'Kayode O.
http://www.sinati.com
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en.