This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes Display Image in JSP using servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Display Image in JSP using servlet" Watch "Display Image in JSP using servlet" New topic
Author

Display Image in JSP using servlet

Nur Azie
Greenhorn

Joined: Jul 31, 2006
Posts: 4
I have a problem in read and display my image file in servlet
Below is my code ... Any help if this code is not correct.

OutputStream out=response.getOutputStream();

FileInputStream fin=null;
BufferedInputStream fBuf=null;

try
{

fin=new FileInputStream("logo.gif");
fBuf=new BufferedInputStream(fin);
final int BufferLen=2048;
byte[] buf=new byte[BufferLen];
int bytesRead;

while(-1!=(bytesRead=fBuf.read(buf,0,buf.length)))
{
out.write(buf,0,bytesRead);
}

if (out!= null) {

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

}
}
catch(FileNotFoundException fnfe)
{

fnfe.printStackTrace();
}
catch(Exception e)
{
;
e.printStackTrace();
}

finally
{
if (fBuf!=null) fBuf.close();

if (fin!=null) fin.close();
}





thanks
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Go on, give us a clue! What is the problem that you experience? It is important to give people the information they need to help you, otherwise you generally won't get any help. Saying you get "a problem" is not helpful.

Only one small issue was immediately apparent to me, and it's certainly not the cause of your main problem. You check "out" for null at one point, but by then you've already tried to use "out". So you either need to check for null earlier, or not at all. The current check is pointless.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Nur Azie
Greenhorn

Joined: Jul 31, 2006
Posts: 4
Thanks Peter ...
You mean I have to check "out" whether it null or not for the first time?

OutputStream out= null;
out = response.getOutputStream();


Is this correct? The rest follow as the existing code ...

Actually this is done in servlet ... Sometimes it hang or shut down the server.
Any idea with the code I written?


tq
Nur Azie
Greenhorn

Joined: Jul 31, 2006
Posts: 4
One more thing ... is the code written enough to cater exeption thrown
during run time error ? eg connection down, network down etc


tq
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

azie,
Welcome to JavaRanch!

We're pleased to have you here with us in the Servlets forum, but there
are a few rules that need to be followed, and one is that proper names are
required. Please take a look at the
JavaRanch Naming Policy and
adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by azie:
Thanks Peter ...
You mean I have to check "out" whether it null or not for the first time?

OutputStream out= null;
out = response.getOutputStream();


Is this correct?


When you call an API method, you need to find out whether it can return null. If it can, you have to check for null after calling and do the right thing (which you have to decide - there's no fixed rule) if null has been returned. If it can't return null, do not bother checking, as this will waste CPU time and memory, and confuse future maintainers of your software.
Nur Azie
Greenhorn

Joined: Jul 31, 2006
Posts: 4
OutputStream out=response.getOutputStream();

FileInputStream fin=null;
BufferedInputStream fBuf=null;

try
{

fin=new FileInputStream("logo.gif");
fBuf=new BufferedInputStream(fin);
final int BufferLen=2048;
byte[] buf=new byte[BufferLen];
int bytesRead;

while(-1!=(bytesRead=fBuf.read(buf,0,buf.length)))
{
out.write(buf,0,bytesRead);
}

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

}
catch(FileNotFoundException fnfe)
{

fnfe.printStackTrace();
}
catch(Exception e)
{
;
e.printStackTrace();
}

finally
{
if (fBuf!=null) fBuf.close();

if (fin!=null) fin.close();
}


I have changed the code as above. Is that correct?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61316
    
  66

"azie", Ben's request that you change your display name to adhere to JavaRanch standards was not a suggestion. Valid display names are mandatory for participation on the Ranch. Please change your display name as instructed prior to your next post.

Be aware that accounts with invalid display names are removed.

bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: Display Image in JSP using servlet