tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From T.K.Thiyagarajan <tktra...@gmail.com>
Subject Re: image download
Date Sat, 27 Sep 2008 02:44:34 GMT
My code given below works fine. U can try if u want

public void sendImageFile(HttpServletResponse response)
{
	FileInputStream in=null;
	File f = null;
	try
	{
		f = new File("webapps/Plasma/graphs", "image.gif");
		in = new FileInputStream(f);
				
		response.setContentType("image/gif");
		int size = (int)f.length();
		response.setContentLength(size);
		byte buffer[] = new byte[size];
		in.read(buffer);
		response.getOutputStream().write(buffer);
	}
	catch(NullPointerException e)
	{
	System.out.println(" NP xpn in sendImageFile "+ e.getMessage());
	}
	catch(FileNotFoundException e)
	{
	System.out.println(" FNF Xpn in SendImageFile "+ e.getMessage());
	}
	catch(IOException ee2){ log("Trouble: " +ee2);}
	finally
	{
	try
        	{
			response.getOutputStream().close();
		}
	catch(IOException ee2){ log("Trouble: " +ee2);}
}
}
On Sat, Sep 27, 2008 at 7:03 AM, Dave <javaone9@yahoo.com> wrote:
> For <img src="http://domain.com/servlet/pictures/image.jpg"/>
>
> in servlet get method,
>
> InputStream is = new FileInputStream("/apphome/pictures/image.jpg");
> OutputStream os = response.getOutputStream();
>
> byte[] buffer = new byte[256*1024];  //256k
> while (true) {
>      int n = is.read(buffer);
>      if (n < 0)
>           return;
>       os.write(buffer, 0, n);
> }
>
> is.close();
> os.close();
>
>
> Is this the right way? Sometimes only the half image is shown on web page. Is  there
a more efficient and robust way?  How about for audio/video files?
>
> I want to take at how Tomcat does it. Could anyone tell me which class?
> thanks
> Dave
>
>
>



-- 
With Regards
T.K.Thiyagarajan

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message