Thanks! I also thought that. But I have 3 upload on the same page,
which I think is that maybe now send all three forms (doPost) that
have the same name.

Here, I think I save the image

                        BookImage myImage = new BookImage(bookid, email, 
imageBlob);

                        // persist image
                        PersistenceManager pm = 
Commons.instance().getPersistenceManager();
                        pm.makePersistent(myImage);

Thanks!

On Oct 22, 8:48 am, Didier Durand <durand.did...@gmail.com> wrote:
> Hi,
>
> I think the issue is when you store the image not when you retrieve
> it:the published code doesn't show how you store the image.
>
> regards
> didier
>
> On Oct 22, 8:29 am, erikzamith <eice...@hotmail.com> wrote:
>
> > > Not really what happens, apparently does not save the image (blob), or
> > > email, or the idimage. because when you try to display it, is null.
> > > This is the doGet
> > > public void doGet(HttpServletRequest req, HttpServletResponse resp)
> > >                         throws ServletException, IOException {
>
> > >                 String email = req.getParameter("email");
> > >                 String bookid = req.getParameter("bookid");
> > >                 BookImage imagen = getImage(email, bookid );
>
> > >                  if (imagen != null  && imagen.getImage() != null) {
>
> > >                                 Blob imageBlob = imagen.getImage();
>
> > >                                 // serve the first image
> > >                                 resp.setContentType("image/jpg");
> > >                                 
> > > resp.getOutputStream().write(imageBlob.getBytes());
> > >                         } else {
> > >                                 // If no image is found with the given 
> > > title, redirect the user
> > >                                 // to
> > >                                 // a static image
>
> > >                                 resp.sendRedirect("images/no_image.png");
> > >                         }
>
> > >         }
>
> > private BookImage getImage(String email, String bookid) {
> >                 PersistenceManager pm = 
> > Commons.instance().getPersistenceManager();
> >                 Query query = pm.newQuery(BookImage.class, "email == 
> > '"+email+"' &&
> > bookid == '"+bookid+"'");
> >             query.setRange(0, 1);
> >             try {
> >                         List<BookImage> results = (List<BookImage>) 
> > query.execute();
> >                         if (results.iterator().hasNext()) {
> >                     // If the results list is non-empty, return the first
> > (and only)
> >                     // result
> >                     return results.get(0);
> >                 }
> >             } finally {
> >                 query.closeAll();
> >             }
>
> >             return null;
> >         }
>
> > > On 22 oct, 05:34, Didier Durand <durand.did...@gmail.com> wrote:
>
> > > > Hi Erik,
>
> > > > To check (and then limit) the format, you can extract the picture from
> > > > the mail as a byte[] and then use this byte[] to create an image via
> > > > ImagesService.makeImage(byte[]) and then do Image.getFormat() to see
> > > > if you accept the format or not.
>
> > > > regards
> > > > didier
>
> > > > On Oct 21, 1:43 pm, erikzamith <eice...@hotmail.com> wrote:
>
> > > > > Hello,
> > > > > I have an image that would save the user (email) and a idimagen.
> > > > > I use a form to send email and idimagen (Book). But do not 
> > > > > persistence.
>
> > > > > I also want to know how to limit the types of images (jpg, png and gif
> > > > > only).
>
> > > > > This is my
>
> > > > > public class Upload extends HttpServlet {
> > > > > @Override
> > > > > public void doPost(HttpServletRequest request, HttpServletResponse 
> > > > > res)
> > > > > throws ServletException, IOException {
>
> > > > > Long bookid = -4L;
> > > > > String email = "";
> > > > > Blob imageBlob = null;
> > > > > try{
> > > > > ServletFileUpload upload = new ServletFileUpload();
> > > > > FileItemIterator iter = upload.getItemIterator(request);
> > > > > while(iter.hasNext()){
> > > > > FileItemStream imageItem = iter.next();
> > > > > if(imageItem.isFormField()) {
> > > > > String name = imageItem.getFieldName();
> > > > > InputStream imgStream = imageItem.openStream();
>
> > > > > byte [] buffer = new byte[1024];
> > > > > int i=0;
> > > > > while(true) {
> > > > > int v = imgStream.read();
> > > > > if(v == 0 || v == -1)
> > > > > break;
> > > > > buffer[i] = (byte) v;
> > > > > i++;}
>
> > > > > String value = new String(buffer,0,i);
>
> > > > > if("bookid".equals(name)){
> > > > > bookid = Long.valueOf(value);
> > > > > System.out.println("bookid: "+bookid);}
>
> > > > > if("email".equals(name))
> > > > > email = value;
> > > > > System.out.println("email: "+email);} else {
>
> > > > > InputStream imgStream = imageItem.openStream();
> > > > > imageBlob = new Blob(IOUtils.toByteArray(imgStream));}
> > > > > }
>
> > > > > // construct our entity objects
>
> > > > > BookImage myImage = new BookImage(bookid, email, imageBlob);
>
> > > > > // persist image
> > > > > PersistenceManager pm = Commons.instance().getPersistenceManager();
> > > > > pm.makePersistent(myImage);
>
> > > > > // respond to query
> > > > > res.setContentType("text/plain");
> > > > > res.getOutputStream().write("OK!".getBytes());
>
> > > > > } catch(FileUploadException e){
>
> > > > > e.printStackTrace();
>
> > > > > }
> > > > > }
> > > > > }
>
> > > > > But when I show the picture, no picture, not saved any.

-- 
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 google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to