Most people I know have some kind of web access to their email. For my personal emails, I use yahoo mail. Hotmail, Google Mail, and AOL are all examples of such a service. With these services, you can view you email wherever you have web access. Sometimes, you can even access your email on your mobile phone.
When an email has an attachment, there is usually the option to download the attachment to your local hard drive. So if you receive a .doc attachment, you can save the file to disk and view it with Microsoft Word. If You receive a PDF, you can open it with Adobe Acrobat.
But what if you don't have these programs on your machine? Or what if you don't want to take the extra steps to download the attachment and launch the viewer? Yahoo Mail has a neat "Preview" feature that allows you to look at the first page of a Word Document. Google doesn't really offer anything.
Here's a neat solution for viewing attachments *inline* along with the message. No downloads, no plug-ins, no third party applications. All we need to do is convert the attachment on the server to a web-viewable image format...like PNG or JPEG.
First we need to learn how to convert multiple document formats to a web-viewable format.
Here's a bare bones servlet that will do just that. I'm using Snowbound Software's RasterMaster Imaging SDK for Java to do the document conversion. The resulting Servlet code is quite simple. In the next post, We'll look into creating a very basic web-mail servlet
public class Document2PNGServlet extends HttpServlet
{
private final static int PNG_FORMAT = 43;
private final static String IMAGE_DIRECTORY = "c:/images/";
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Snowbnd snow = new Snowbnd();
// get the page number to convert from the page parameter
int pageIndex = Integer.parseInt(request.getParameter("page"));
// get the filename to convert from the filename parameter
String filename = IMAGE_DIRECTORY + request.getParameter("filename");
// create a temporary byte array that is at least as big as the expected
// output size
byte[] tmpOutputBytes = new byte[15000000];
// decompress the specified page
snow.IMG_decompress_bitmap(filename, pageIndex);
// write out the page to a byte array in PNG format
int outputSize = snow.IMG_save_bitmap(tmpOutputBytes, PNG_FORMAT);
// copy only the data portion to our output array
byte[] outputBytes = new byte[outputSize];
System.arraycopy(tmpOutputBytes, 0, outputBytes, 0, outputSize);
// set the content type of the image data to be sent to the browser
response.setContentType(getServletConfig().getServletContext().getMimeType(".png"));
response.setContentLength(outputSize);
// send the bytes to the response object
sendBytes(outputBytes, response);
}
/**
* This method will send the contents of the byte array to the servlet
* response output stream.
*
* @param bytes the output byte array
* @param response the HttpServletResponse object to write to
* @throws IOException
*/
protected void sendBytes(byte[] bytes, HttpServletResponse response)
throws IOException
{
ServletOutputStream servletoutputstream = response.getOutputStream();
servletoutputstream.write(bytes);
servletoutputstream.flush();
}
}
-Alex
I'm having great trouble forwarding attachments. What's the solution?
Posted by: Gloria | September 24, 2007 at 03:57 PM