• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Display Image in JSP using servlet

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nur Azie
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing ... is the code written enough to cater exeption thrown
during run time error ? eg connection down, network down etc


tq
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
reply
    Bookmark Topic Watch Topic
  • New Topic