Here's some code I wrote a while ago. Note that I pulled this from
docs I was working on about saving attached Images in the datastore
and not a test case, so it may not work out of the box without some
tweaking. Give it a try (and if it's broken, fixes appreciated!):

http://pastie.org/865494

public class IncomingMailHandlerServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        try {
            MimeMessage message = new MimeMessage(session,
request.getInputStream());

            InputStream inputStream = message.getInputStream();

            ByteArrayDataSource inboundDataSource = new
ByteArrayDataSource(inputStream, message.getContentType());
            Multipart inboundMultipart = new MimeMultipart(inboundDataSource);

            for (int i = 0; i < inboundMultipart.getCount(); i++) {
                BodyPart part = inboundMultipart.getBodyPart(i);

                if (part.getDisposition() == null) {
                    // This is just a plain text part
                } else if (part.getDisposition().equals("attachment")) {
                    // Create a new ByteArrayDataSource with this part
                    MimeBodyPart inboundMimeBodyPart = (MimeBodyPart) part;

                    // The call to getContentType here may return a
filename along with content type. Ex:
                    //     image/jpeg; name="filename.jpg"
                    // It doesn't seem to affect display in a browser
but you may wish to sanitize it
                    String contentType = inboundMimeBodyPart.getContentType();

                    InputStream is = part.getInputStream();

                    byte[] rawData, buffer = new byte[8192];
                    int len;
                    ByteArrayOutputStream output = new ByteArrayOutputStream();

                    try {
                        while ((len = is.read(buffer, 0,
buffer.length)) != -1) output.write(buffer, 0, len);
                        rawData = output.toByteArray();
                    } finally {
                        output.close();
                    }

                    PersistenceManager pm = PMF.get().getPersistenceManager();

                    Image image = new Image(new Blob(rawData), contentType);
                    try {
                        pm.makePersistent(image);
                    } finally {
                        pm.close();
                    }

                }

            }

        } catch (MessagingException e) {
            throw new ServletException(e);
        }
    }

}


On Fri, Mar 5, 2010 at 5:41 AM, thierry Le conniat <thlec...@euriware.fr> wrote:
> Hello,
> my application receive by mail, pictures which are encoding in jpg.
> I would like to make some analysis in this picture (represented by an
> array of  byte ), comparison column by column, but GAE'S  Image API
> don't provide such function and many classes from javax.io... are not
> authorized .
>
> So, how can I do  ?
>
> Below the source code where i would like to
>
> ...
> byte b[] = null ;
> if (myDataHandler != null && myDataHandler.getDataSource() != null)
> {                                       {
>                InputStream myInputStream = myDataHandler.getInputStream();
>
>                log.info("myInputStream.available " + 
> myInputStream.available());
>                b= new byte[myInputStream.available()];
>                myInputStream.read(b);
>
>                // analysis of b
> }
>
> thanks for jour help.
>
> bye
>
>
> --
> 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.
>
>



-- 
Ikai Lan
Developer Programs Engineer, Google App Engine
http://googleappengine.blogspot.com | http://twitter.com/app_engine

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