• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieve image in JSP

 
derick nial
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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 >
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
 
derick nial
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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? :/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic