tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Konstantin Kolinko" <knst.koli...@gmail.com>
Subject Re: image download
Date Sat, 27 Sep 2008 01:57:50 GMT
2008/9/27 Dave <javaone9@yahoo.com>:
> 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.
>

You do not close the streams properly, thus they disappear without
being flushed.

Read at least the Java Tutorial at java.sun.com [1]. Pay attention to
the "finally" keyword.

[1] http://java.sun.com/docs/books/tutorial/essential/index.html

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