tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Johnny Kewl" <j...@kewlstuff.co.za>
Subject Re: image download
Date Sat, 27 Sep 2008 10:30:34 GMT

----- Original Message ----- 
From: "Dave" <javaone9@yahoo.com>
To: "Tomcat Users List" <users@tomcat.apache.org>
Sent: Saturday, September 27, 2008 3:38 AM
Subject: Re: image download


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)
break;
os.write(buffer, 0, n);
}

is.close();
os.close();


--- On Fri, 9/26/08, Dave <javaone9@yahoo.com> wrote:

From: Dave <javaone9@yahoo.com>
Subject: image download
To: "Tomcat Users List" <users@tomcat.apache.org>
Date: Friday, September 26, 2008, 6:33 PM

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

******************

Dave its that buffer n thats wrong....
Also you not going to get much by making it 256 k blocks... you just have to 
match typical web tx size... 4096 is enuf

Use this....

public void outputImage(File thumbfile, String type,
    HttpServletResponse response) throws IOException {
    response.setContentType("image/"+type);
    OutputStream os = response.getOutputStream();
    FileInputStream fin = new FileInputStream(thumbfile);

    byte[] buf = new byte[4096];
    int count = 0;
    while(true) {
        int n = fin.read(buf);
        if(n == -1) {
            break;
        }
        count = count + n;
        os.write(buf,0,n);
    }
    os.flush();
    os.close();
    fin.close();
}

---------
The DefaultServlet.java is the place this happens but it looks nothing like 
it because they doing caching and lots of other stuff...
Just get that setContentType right and it could be video... anything
Its not a bad idea adding setContentSize as well...

enjoy...

---------------------------------------------------------------------------
HARBOR : http://www.kewlstuff.co.za/index.htm
The most powerful application server on earth.
The only real POJO Application Server.
See it in Action : http://www.kewlstuff.co.za/cd_tut_swf/whatisejb1.htm
---------------------------------------------------------------------------




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