aspose file tools*
The moose likes Struts and the fly likes retrieve image in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "retrieve image in JSP" Watch "retrieve image in JSP" New topic
Author

retrieve image in JSP

derick nial
Greenhorn

Joined: Sep 26, 2013
Posts: 15
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

Joined: Jan 10, 2002
Posts: 60996
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
derick nial
Greenhorn

Joined: Sep 26, 2013
Posts: 15
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

Joined: Jan 10, 2002
Posts: 60996
    
  65

Here's one example: http://simple.souther.us/ar01s10.html
derick nial
Greenhorn

Joined: Sep 26, 2013
Posts: 15
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

Joined: Jan 10, 2002
Posts: 60996
    
  65

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

Joined: Sep 26, 2013
Posts: 15
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? :/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieve image in JSP