• 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

retrieve image in JSP

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all;

i ve been trying to figure out on how to retrieve image (from database) in jsp page for few days now.
and now what i have so far, i ve managed to retrieve the image succesfully, however, when this happened, all my other contents are all replaced by this image,
i already tried the response.setContentType("text/html") to make other contents visible, but, when i do this, error page was displayed. here is my codes:

<%
byte[] imgData = profile.getAvatar(); // the profile.getAvatar() is the converted blob to byte array from the services

if (profile.getAvatar()!= null) {

File file = File.createTempFile("Avatar", ".png"); // trying to create temperorary file to store the image first
FileOutputStream os =new FileOutputStream(file);
os.write(imgData);

OutputStream ot = response.getOutputStream();
response.setContentType("image/png");
ot.write(imgData);

System.out.println("temperory file--->" +file.getAbsolutePath());
%>

<input type="image" name="avatar" id="avatar" >


<%
}else{
if (profile.getAvatar() == null){
%>
<input type="image" name="avatar" id="avatar"src="img/no_avatar.png" />
<% }
}

%>

i i think my problem is from my <img src ? <br /> if i can figure out the right url address i can retrieve it successsfully. <br /> <br /> hope to receive respond from you guys asap, thanks in advance >
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no need to create a temporary file. Just create a servlet to serve up the image data from the DB as its response and reference it from the image tag.

You should be be doing any database access from a JSP.
 
derick nial
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your fast respond. Do you have any example/ links on how might this servlet look like?
 
Bear Bibeault
Sheriff
Posts: 67747
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
Here's one example: http://simple.souther.us/ar01s10.html
 
derick nial
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Bear Bibeault, i already studied and tried to implement the method from the example you shared,and yet it still not working, do you have any other ways? please
 
Bear Bibeault
Sheriff
Posts: 67747
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
If it's not working, you fix it rather than just trying to find another method.

Can you be more specific about what's not working? Are you sure you are streaming the content correctly (and from a servlet, not a JSP) and setting the response headers correctly?
 
derick nial
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so sorry for that, it just that my dateline is in next few days time. anw here is from my servlet:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// Parse URI to find the filename
String uri = request.getRequestURI();
String[] tokens = uri.split("/");
String filename = "";

for(int i = 2; i < tokens.length; i++){
filename += ("/" + tokens[i]);
}

// Set Content type
String contentType = getServletContext().getMimeType(filename);

// Stream the image
BufferedInputStream in = null;
try
{
in = new BufferedInputStream(getServletContext().getResourceAsStream("/WEB-INF/" + filename));

response.setContentType(contentType);
response.setHeader("Content-Disposition", " inline; filename=" + filename);

ServletOutputStream out = response.getOutputStream();

byte[] buffer = new byte[4 * 1024];

int data;
while((data = in.read(buffer)) != -1)
{
out.write(buffer, 0, data);
}
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
return;
}
finally
{
try
{
in.close();
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}

and this is my jsp:

<%
byte[] imgData = profile.getAvatar();
response.setContentType("text/html");

try{
if (profile.getAvatar()!= null) {

String fileName = request.getParameter("imagepath");
response.setContentType("image/png");
BufferedOutputStream o = new BufferedOutputStream(response.getOutputStream());
o.write(imgData);
System.out.println("fileName----->"+fileName);
%>
<input type="image" name="avatar" id="avatar" >

<% }else{
if (profile.getAvatar() == null){
// %>
<input type="image" name="avatar" id="avatar"src="img/no_avatar.png" /> }
<% }
}
} catch(IOException e){

}finally{
// close the streams
}
%>

can you tell me what went wrong? :/
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic