[appengine-java] Re: How to return a file from a servlet
Many web servers have a configured map between file extensions and content types and that's how the browser knows how to handle a file if no content type was specified in the HTTP. However, you don't know if all your user's web servers are configured in such a way and that is why it is better to specify the content type before sending out the content. Jorge Gonzalez On Nov 30, 11:25 am, Prashant antsh...@gmail.com wrote: thanks a lot guys. is it necessary to use resp.setContentType(image/gif); even if url end with .gif ? -- 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.
[appengine-java] Re: How to return a file from a servlet
On Nov 30, 11:23 am, Prashant antsh...@gmail.com wrote: Hi, I have a servlet with request handler */file/** . I want to return a file * /theme/bg.gif* for all */file/*.gif *. I can check for .gif extension then how do i send */theme/bg.gif*, for */file/*.gif* without sending a redirect ? Do a forward? RequestDispatcher dispatcher = request.getRequestDispatcher (whereEver); dispatcher.forward(request, response); Cheers Philippe -- 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.
[appengine-java] Re: How to return a file from a servlet
One more thing - do not forget to tell the App Engine that the files you want to read by getResourceAsStream() are resource files. Include the following into your WEB-INF/appengine-web.xml: resource-files include path=/theme/*.gif / /resource-files Vaclav On Nov 30, 2:31 pm, bysse erik.byst...@gmail.com wrote: This should get you going: InputStream resourceAsStream = getServletContext().getResourceAsStream (pathToImage); // use stream to read image data ... resp.setContentType(image/gif); resp.setContentLength(imageData.length); outputStream.write(imageData, 0, imageData.length); /Erik On Nov 30, 11:23 am, Prashant antsh...@gmail.com wrote: Hi, I have a servlet with request handler */file/** . I want to return a file * /theme/bg.gif* for all */file/*.gif *. I can check for .gif extension then how do i send */theme/bg.gif*, for */file/*.gif* without sending a redirect ? 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 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.
[appengine-java] Re: How to return a file from a servlet
This should get you going: InputStream resourceAsStream = getServletContext().getResourceAsStream (pathToImage); // use stream to read image data ... resp.setContentType(image/gif); resp.setContentLength(imageData.length); outputStream.write(imageData, 0, imageData.length); /Erik On Nov 30, 11:23 am, Prashant antsh...@gmail.com wrote: Hi, I have a servlet with request handler */file/** . I want to return a file * /theme/bg.gif* for all */file/*.gif *. I can check for .gif extension then how do i send */theme/bg.gif*, for */file/*.gif* without sending a redirect ? 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 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.
Re: [appengine-java] Re: How to return a file from a servlet
Or, something similar, using the IOUtils class from Commons I/O ( http://commons.apache.org/io/): InputStream resourceAsStream = getServletContext().getResourceAsStream.(pathToImage); resp.setContentType(image/gif); IOUtils.copy(resourceAsStream,resp.getOutputStream); Vince On Mon, Nov 30, 2009 at 8:31 AM, bysse erik.byst...@gmail.com wrote: This should get you going: InputStream resourceAsStream = getServletContext().getResourceAsStream (pathToImage); // use stream to read image data ... resp.setContentType(image/gif); resp.setContentLength(imageData.length); outputStream.write(imageData, 0, imageData.length); /Erik On Nov 30, 11:23 am, Prashant antsh...@gmail.com wrote: Hi, I have a servlet with request handler */file/** . I want to return a file * /theme/bg.gif* for all */file/*.gif *. I can check for .gif extension then how do i send */theme/bg.gif*, for */file/*.gif* without sending a redirect ? 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 google-appengine-j...@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.comgoogle-appengine-java%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en. -- 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.
Re: [appengine-java] Re: How to return a file from a servlet
thanks a lot guys. is it necessary to use resp.setContentType(image/gif); even if url end with .gif ? -- 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.
Re: [appengine-java] Re: How to return a file from a servlet
I'm not absolutely sure it's required, but I think in practice you should always specify the content type. Note that you can get most common types via a lookup based on the file extension: String contentType = getServletContext().getMimeType( imagePath ); You can add additional mime types to web.xml and the above code will use them, too (in addition to the common ones). Vince On Mon, Nov 30, 2009 at 12:25 PM, Prashant antsh...@gmail.com wrote: thanks a lot guys. is it necessary to use resp.setContentType(image/gif); even if url end with .gif ? -- 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.