Image via HTTP is slow

T

Ted

Hi everyone,

I'm having trouble with a servlet I'm working on. I'm trying to display
an image when you go to a url. When I go to the url on my local
computer it takes about 6 seconds to download an image that is about
20KB. Downloading the image (again, from my local computer) via just
apache (no java) takes no time at all. If I change the code to just
loop through the read and not print anything, it is also quite fast.
Anyone have any idea why this would be so slow?

So in short: it works, but it's too slow, any ideas?

Thanks!
Ted.

This is the code:
-----------------
String fn = new String("image.png");
File f = new File(fn);

response.setContentType(getServletContext().getMimeType(fn));
response.setContentLength((int) f.length());

FileInputStream in = new FileInputStream(f);
OutputStream out = response.getOutputStream();

byte[] buf = new byte[1024];
int cnt = 0;
while((cnt = in.read(buf)) >= 0)
out.write(buf, 0, cnt);

in.close();
out.flush();
out.close();
 
T

Tjerk Wolterink

Ted said:
Hi everyone,

I'm having trouble with a servlet I'm working on. I'm trying to display
an image when you go to a url. When I go to the url on my local computer
it takes about 6 seconds to download an image that is about 20KB.
Downloading the image (again, from my local computer) via just apache
(no java) takes no time at all. If I change the code to just loop
through the read and not print anything, it is also quite fast. Anyone
have any idea why this would be so slow?

So in short: it works, but it's too slow, any ideas?

Thanks!
Ted.

This is the code:
-----------------
String fn = new String("image.png");
File f = new File(fn);

response.setContentType(getServletContext().getMimeType(fn));
response.setContentLength((int) f.length());

FileInputStream in = new FileInputStream(f);
OutputStream out = response.getOutputStream();

byte[] buf = new byte[1024];
int cnt = 0;
while((cnt = in.read(buf)) >= 0)
out.write(buf, 0, cnt);

in.close();
out.flush();
out.close();

just a suggestion:

maybe you should set the ouputstreams buffersize to a bigger value.

And another think:
Why do you do:
String fn = new String("image.png");

this is sufficient
String fn="image.png";
 
T

Ted

maybe you should set the ouputstreams buffersize to a bigger value.


I'll give that a shot.
And another think:
Why do you do:

this is sufficient
String fn="image.png";

(I'm new to Java) Is this a lot better/faster than using new String()
or just a style thing?

Thanks,
Ted.
 
T

Ted

Just a follow up to answer my own question.

I was having this problem with apache 2 and tomcat 5.5. I don't know
apache 2 very well, so I 'downgraded' to apache 1.3.33 and it works
like a charm. (This is all on OS X.)

Ted.
 
T

Ted

Continuing the conversation with myself. It turns out that I spoke too
soon. This works like a charm using tomcat as a server on its own
(Apache-Coyote/1.1). But when it's piped through the older apache, it's
just as slow.

Either way, it doesn't appear to be a java issue at all, so I'll take
this elsewhere.

Thanks,
Ted.
 
K

Kevin McMurtrie

Tor Iver Wilhelmsen said:
You should put a BufferedInputStream around that.

No he shouldn't. It will just use extra memory and perform extra buffer
copies. BufferedXxxxxStream classes are for parsers that operate with
small buffers or single bytes. He already has a 1024 byte buffer.

The only thing I'd do is increase the buffer size to 1500 bytes so it
can create full packets if the output isn't buffered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top