0

I want to display image from my local location in computer,I am use this code for that it works fine for me,

<%@ page import="java.io.*" %>
<%@page contentType="image/gif" %>
<%
    OutputStream o = response.getOutputStream();
    InputStream is = new FileInputStream(new File("D:/FTP/ECG/ecg.jpg"));
    byte[] buf = new byte[32 * 1024]; 
    int nRead = 0;
    while( (nRead=is.read(buf)) != -1 )
    {
      o.write(buf, 0, nRead);
    }

    o.flush();
    o.close();

%>

My question is that i want to display content with it, and also other thing with it like input box and labels also.

Vijay
  • 8,131
  • 11
  • 43
  • 69
Panchotiya Vipul
  • 1,226
  • 5
  • 17
  • 42
  • Like said, this is to some point correct and is sending your image via HTTP, what's better than using local file URI. Just use @Uooo answer and create a HTML page. Anyway if you are storing images in DB, this is a NO NO, images should not be stored in DB. – Diego C Nascimento Oct 08 '13 at 08:51

2 Answers2

1

What you are doing here is streaming an image to the client. What you need is an HTML document which refers to this image, like:

<img src="path/to/your/jsp">
<p>Some other text</p>
Uooo
  • 6,204
  • 8
  • 36
  • 63
0

Scriptlets = nono, code is much less readable with them. Use JSTL where you can.

To display the actualy image use html tag

<img src="D:/FTP/ECG/ecg.jpg" />

Let's assume you have a page where list of images (loaded from db) is to be displayed.

In your controller in a method that prepares the view:

ModelAndView mv = new ModelAndView("yourView");
mv.addObject("imageList",imageList);
return mv;

imageList is simply a list of filenames (List) which you loaded from the db before.

Then in your jsp you do:

<c:forEach items="${imageList}" var="path">
    <img src="yourPath/${path} />
</c:forEach>
Lenymm
  • 879
  • 1
  • 6
  • 27
  • I want to display image dynamically from my computer ,I know use of img tag but i want to specify only d:/ftp/ecg only and the name of image come from database and display that image from my local pc path – Panchotiya Vipul Jul 30 '13 at 09:35
  • My local path can not work in directly in Eclipse if I give local path then can't display anything – Panchotiya Vipul Jul 30 '13 at 09:47
  • I don't understand but you might want to check this out: http://stackoverflow.com/questions/3153337/how-do-i-get-my-current-working-directory-in-java – Lenymm Jul 30 '13 at 09:59