Step1:Defining a data class named Photo
public class Photo{
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Long id;

        @Persistent
        private Blob photo;
        ...
        Set & Get
        ...
}


Step2:Defining a DAO PhotoDAO.java
public class PhotoDao {
        private static PhotoDao _instance = null;

        public static PhotoDao getInstance() {
                if (_instance == null) {
                        _instance = new PhotoDao();
                }
                return _instance;
        }

        //
        public String insertPhoto(Photo photo) {
                PersistenceManager pm = PMF.get().getPersistenceManager();
                try {
                        pm.makePersistent(photo);
                } finally {
                        pm.close();
                }
                return photo.getId().toString();
        }
}


Step3:UploadServlet.java

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

UploadServlet.java
        ...
        ...
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iterator = null;
        try {
                iterator = upload.getItemIterator(req);
                } catch (FileUploadException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
                try {
                        while (iterator.hasNext()) {
                                FileItemStream item = iterator.next();
                                InputStream stream = item.openStream();
                                if (item.isFormField()) {
                                                        // Handle form field
                                } else {
                                // Handle the uploaded file

                                Blob bImg = new 
Blob(IOUtils.toByteArray(stream));
                                Photo photo = new Photo(bImg);
                                String pid = 
PhotoDao.getInstance().insertPhoto(photo);
                                        
//resp.sendRedirect("admin?type=photo&option=list");
                                }

        }
        ...
        ...


Step4: Show the photo which you upload.  PhotoSerlvet.java

public class PhotoServlet extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                        throws IOException {
                String id = req.getParameter("id");
                ...
                Photo photo = 
PhotoDao.getInstance().getById(Long.parseLong(id));
                ...
                Blob b = photo.getPhoto();
                resp.setContentType("image/jpeg;charset=utf-8");
                resp.getOutputStream().write(b.getBytes());
                resp.getOutputStream().close();
        }
}

demo: http://mimaiji.appspot.com/photo?id=4001

On 3月14日, 下午1时12分, nag <nagarjuna...@gmail.com> wrote:
> Hi
> I am trying to build online reg form
> can u help me how to upload image files to app engine.
>
> First: is there any way to upload images in app engine?
>
> thanks
> Nag

-- 
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