Hi, it seems ok. I use the same way ApacheIO just I have regular UI form. But it's no matter if you fire POST http request from the HTML form or if that is done programatically. Just test and make sure it works.
Sincerely, Alex On Tue, Oct 20, 2009 at 6:17 PM, Houston startup coder < [email protected]> wrote: > > I haven't actually tested this code yet since I got sidetracked with > some other stuff. But I basically worked off of the example farther > down this page: > > http://code.google.com/appengine/kb/java.html > > Then I just modified it to save the file data as an attribute to an > object I want to persist to the datastore. So does anyone know if > this is what I should be doing to have a desktop app upload files in > the background to Google App Engine? Should they be going through a > servlet if it's not an actual human user filling out a file upload > form on a webpage? > > Here's my code: > > > > import org.apache.commons.fileupload.FileItemStream; > import org.apache.commons.fileupload.FileItemIterator; > import org.apache.commons.fileupload.servlet.ServletFileUpload; > > import org.apache.commons.io.IOUtils; > > import java.io.InputStream; > import java.io.IOException; > import java.util.logging.Logger; > > import javax.jdo.PersistenceManager; > import javax.servlet.ServletException; > import javax.servlet.http.HttpServlet; > import javax.servlet.http.HttpServletRequest; > import javax.servlet.http.HttpServletResponse; > > import com.google.appengine.api.datastore.Blob; > import com.ideate.PMF; > > public class FileUploader extends HttpServlet { > private static final Logger log = > Logger.getLogger(FileUploader.class.getName()); > > public void doPost(HttpServletRequest req, HttpServletResponse res) > throws ServletException, IOException { > try { > ServletFileUpload upload = new ServletFileUpload(); > res.setContentType("text/plain"); > > 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). > > Blob uploadedImage = new Blob(IOUtils.toByteArray(stream)); > > Photo photo = new Photo(); > photo.setName(item.getName()); > photo.setImageData(uploadedImage); > > PersistenceManager pm = PMF.get().getPersistenceManager(); > try { > pm.makePersistent(photo); > } finally { > pm.close(); > } > > /* > int len; > byte[] buffer = new byte[8192]; > while ((len = stream.read(buffer, 0, buffer.length)) != -1) > { > res.getOutputStream().write(buffer, 0, len); > } > */ > } > } > } catch (Exception ex) { > throw new ServletException(ex); > } > } > } > > > THE END > > Thanks for any help... > > > > > > On Oct 19, 12:18 pm, Abhinav Lele <[email protected]> wrote: > > Could you share your code for the servlet that handles file uploads. I > have > > been not able to get that working. Thanks in advance > > -- > > Abhinav > > > > -_[No constructors were harmed in the writing of this post. Any > resemblance > > to objects living or dead is purely coincidental]_- > > > > On Mon, Oct 19, 2009 at 7:40 AM, Houston startup coder < > > > > > > > > [email protected]> wrote: > > > > > I need to upload images, sound files and text documents from a mostly > > > standalone client PC application to a GAE app. I'm following the file > > > upload example in the Google App Engine for Java documentation and > > > using the Apache Commons ServletFileUpload to stream in the data and > > > then save it with the PersistenceManager. > > > > > Uploading through a servlet seems just fine, but I took a step back to > > > wonder whether a servlet was the best way to upload these files to GAE > > > since this isn't a human choosing files on a webpage but rather some > > > software programatically sending the files up to the server. Is a > > > servlet my best choice in this case? > > > > > Thanks... > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google App Engine for Java" 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-java?hl=en -~----------~----~----~----~------~----~------~--~---
