Display KM image in JSP

Hi All,
I have image in KM folder. I want to display it in JSP as a html image. I am able to
access the image using below code.
<%
try {
     IUser sapUser = componentRequest.getUser();
     com.sapportals.portal.security.usermanagement.IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);
     IResourceContext resourceContext = new ResourceContext(ep5User);
     String path = "/documents/Images/sample.gif";
     RID imgRID = RID.getRID(path);
     IResource resource = ResourceFactory.getInstance().getResource(imgRID, resourceContext);
     BufferedInputStream bufIn = new BufferedInputStream(resource.getContent().getInputStream());
byte[] imagebyte = new byte[bufIn.available()];
bufIn.read(imagebyte);
catch(Exception e ) { }
%>
<img border="0" width="147" height="66" src="">
Using above code image is stored in a byte array. How to set this as html img tag source.
Thanks
Joe

Hello Sreekanth,
there are two approaches how to get an KM image into your JSP.
<b>1) The easy one</b>
Just put into SRC attribute of your IMG tag next link:
<b>/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/</b>documents/Images/sample.gif
Bold part is an access link to KM root. In this case - be aware, the users have to have access rights to given KM object (could be inherited from parent folder).
<b>2) Through a stream</b>
In this case you don't have to set access rights for a given resource for all users, you will read the image from Runtime (using ice_service context for example).
And you can refer to that image as you refer your application. The only thing you need to do - is to write image byte stream to response in your application. Example:
          ByteArrayOutputStream PictureStream = ...;
          if (request.getParameter("Get_Image") != null) {
                    response.setContentType("image/gif");
                    ServletOutputStream sos = response.getOutputStream();
                    response.setHeader("Content-Disposition", "attachment; filename="image.gif"");
                    response.setHeader("Cache-Control", "no-cache");
                    PictureStream.writeTo(sos);
                    sos.close();
               return;
And then you address this image stream in your SRC attribute of IMG tag following:
<u><i><your application url absolute or relative (relative is siggested)>?Get_Image=true</i></u>
regards,
mz

Similar Messages

  • Problem with displaying BLOB images on JSP page using a servlet

    hi. I have a big problem with displaying BLOB images using JSP. I have a servlet that connects to the oracle database, gets a BLOB image , reads it, and then displays it using a BinaryStream. The problem is , this works only when i directly call that servlet, that is http://localhost:8080/ImageServlet. It doesn't work when i try to use that servlet to display my image on my JSP page (my JSP page displays only a broken-image icon ) I tried several coding approaches with my servlet (used both Blob and BLOB objects), and they work just fine as long as i display images explicitly using only the servlet.
    Here's what i use : ORACLE 10g XE , Eclipse 3.1.2, Tomcat 5.5.16 , JDK 1.5
    here is one of my image servlet's working versions (the essential part of it) :
                   BLOB blob=null;
              rset=st.executeQuery("SELECT * FROM IMAGES WHERE ID=1");
              while (rset.next())
                   blob=((OracleResultSet)rset).getBLOB(2);
              response.reset();
              response.setContentType("image/jpeg");
              response.addHeader("Content-Disposition","filename=42.jpeg");
                    ServletOutputStream ostr=response.getOutputStream();
                   InputStream istr=blob.getBinaryStream(1L);
                    int size=blob.getBufferSize();
              int len=-1;
                    byte[] buff = new byte[size];
                         while ((len=istr.read( buff ))!=-1 ) {
                   ostr.write(buff,0,len);
             response.flushBuffer();
             ostr.close(); and my JSP page code :
    <img src="/ImageServlet" border="0"  > If you could just tell me what i'm doing wrong here , or if you could show me your own solutions to that problem , i would be very greatful ,cos i'm realy stuck here , and i'm rather pressed for time too. Hope someone can help.

    I turns out that it wasn't that big of a problem after all. All i had to do was to take the above code and place it into another JSP page instead of into a servlet like i did before. Then i just used that page as a source for my IMG tag in my first JSP. It works perfectly well. Why this doesn't work for servlets i still don't know, but it's not a problem form me anymore . Ofcourse if someone knows the answer , go ahead and write. I would still appriceatte it.
    here's the magic tag : <img src="ImageJSP.jsp" border="0"  > enjoy : )

  • How to display uploaded image in jsp page.

    Hello,
    I am using struts 1.2.9 and and have uploaded image on the server. Now what I want to do display the image in jsp page after clicking on one link in jsp. I have tried many thing to display image in jsp page. But I am getting an error during displaying image in jsp. I have displayed absolute path in servlet. and used InputStream and outputstream to display image in jsp page.
    Can any one help.
    Thanks in advance
    Manveer Singh

    Follow this. This topic is very popular recently on the forum.

  • How to display stored image in jsp  in ie7???

    i am using internet explore7. i have a problem when i am displaying an image in jsp its not properly coming. this image and image is stored in database.
    image is stored in database using "binarystream" .
    i am just simply calling the image path and using the html image display tag.
    <img src="<%=fileIpath%>" but image is not coming properly but this image is showing properly in lower ie version.
    Can anyone help me???

    can anyone reply this question??Appearently no.
    It might be that the question is not interesting enough to attract people.
    Or it might be that the details you provided do no suffice and some important pieces of information are missing. For eample: what are the contents of the variable? If you save the generated page, what do you get?

  • Any other way to display the images in jsp

    Dear All,
    I had a program in get the image in database and display the image in jsp file.
    Generally we are using
    setContenttype(image) and write the binary values in jsp file,
    is there any other way to store the jpeg file in speciefied folder and view the jsp file

    Is there any other way to play the shuffle in a car if it doesn't have an aux facility???
    There is not.
    Or I wondered if you could play it through a smart tv...or do you need an apple tv box???
    Not with the Apple TV, but if the Smart TV has an AUX port, you can connect it that way.
    B-rock

  • I want to display BLOB image in JSP Using  html tags IMG src=

    GoodAfternoon Sir/Madom
    I Have got the image from oracle database but want to display BLOB image using <IMG src="" > Html tags in JSP page . If it is possible than please give some ideas or
    Send me sample codes for display image.
    This code is ok and working no problem here Please send me code How to display using html tag from oracle in JSP page.
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="javax.swing.ImageIcon;" %>
          <%
            out.print("hiiiiiii") ;
                // declare a connection by using Connection interface
                Connection connection = null;
                /* Create string of connection url within specified format with machine
                   name, port number and database name. Here machine name id localhost
                   and database name is student. */
                String connectionURL = "jdbc:oracle:thin:@localhost:1521:orcl";
                /*declare a resultSet that works as a table resulted by execute a specified
                   sql query. */
                ResultSet rs = null;
                // Declare statement.
                PreparedStatement psmnt = null;
                  // declare InputStream object to store binary stream of given image.
                   InputStream sImage;
                try {
                    // Load JDBC driver "com.mysql.jdbc.Driver"
                    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
                        /* Create a connection by using getConnection() method that takes
                        parameters of string type connection url, user name and password to
                        connect to database. */
                    connection = DriverManager.getConnection(connectionURL, "scott", "root");
                        /* prepareStatement() is used for create statement object that is
                    used for sending sql statements to the specified database. */
                    psmnt = connection.prepareStatement("SELECT image FROM img WHERE id = ?");
                    psmnt.setString(1, "10");
                    rs = psmnt.executeQuery();
                    if(rs.next()) {
                          byte[] bytearray = new byte[1048576];
                          int size=0;
                          sImage = rs.getBinaryStream(1);
                        //response.reset();
                          response.setContentType("image/jpeg");
                          while((size=sImage.read(bytearray))!= -1 ){
                response.getOutputStream().write(bytearray,0,size);
                catch(Exception ex){
                        out.println("error :"+ex);
               finally {
                    // close all the connections.
                    rs.close();
                    psmnt.close();
                    connection.close();
         %>
         Thanks

    I have done exactly that in one of my applications.
    I have extracted the image from the database as a byte array, and displayed it using a servlet.
    Here is the method in the servlet which does the displaying:
    (since I'm writing one byte at a time, it's probably not terribly efficient but it works)
         private void sendImage(byte[] bytes, HttpServletRequest request, HttpServletResponse response) throws IOException {
              ServletOutputStream sout = response.getOutputStream();
              for(int n = 0; n < bytes.length; n++) {
                   sout.write(bytes[n]);
              sout.flush();
              sout.close();
         }Then in my JSP, I use this:
    <img src="/path-to-servlet/image.jpg"/>
    The name of the image to display is in the URL as well as the path to the servlet. The servlet will therefore need to extract the image name from the url and call the database.

  • HELP!!!!Display blob image using JSP

    Hi,
    I am trying to blob image using JSP but it did not display successfully.
    Can someone help, please?
    below are the codes snippet:
    <td>
    <img src="display_image.jsp?ID=<%=request.getParameter("ID") %>" width="115" border="0">
    </td>
    byte[] image_blob = details.getImageByteArray();
    response.setContentType("image/jpeg");
    //response.setContentType("image/gif");
    //BEGINNING OF SECTION TO DISPLAY IMAGE
    /*OutputStream fos = null;
    fos = response.getOutputStream();
    fos.write(image_blob, 0, image_blob.length);
    fos.flush();
    fos.close();*/
    java.io.FileOutputStream fos1 = new java.io.FileOutputStream("C:\\"+id+".jpg");
    fos1.write(image_blob);
    fos1.close();
    OutputStream o = response.getOutputStream();
    System.out.println("o");
    o.write(image_blob);
    System.out.println(image_blob);
    System.out.println("write image");

    Use Java Image or BufferedImage instead of obscure byte array for image data.
    Here's an example code for a JSP document:
    <%
    String wid = request.getParameter("percent");
    String cost = request.getParameter("suchi");
    response.reset(); //IMPORTANT !
    response.setContentType("image/jpeg");
    // for testing purpose
    if (wid == null) wid = "58";
    if (cost == null) cost = "3400";
    float dleng = Float.parseFloat(wid) * 2.6f;
    RenderedImage rimg = drawGraph(dleng);
    OutputStream os = response.getOutputStream();
    try{
      ImageIO.write(rimg, "jpg", os);
    catch(IOException e){
    os.flush();
    %>
    <%!
    public RenderedImage drawGraph(float barLength){
      final int w = 800;
      final int h = 18;
      final int x = 0;
      final int y = 0;
      BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2 = img.createGraphics();
      Color c = new Color(Integer.parseInt("FF9999", 16));
      g2.setColor(Color.yellow);
      g2.fillRect(x, y, w, h);
      g2.setColor(c);
      g2.fillRect(x, y, (int)barLength, h);
      g2.dispose();
      return img;
    %>

  • Displaying an image in jsp / servlet

    Hi, I am trying to display an image from database to be displayed in the servlet
    following is my code
    package image;
    import java.io.*;
    import java.text.*;
    import java.util.*;
    import java.sql.*;
    import java.awt.*;
    import java.awt.image.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.PrintWriter;
    import com.sun.image.codec.jpeg.*;
    public class BlobImageServlet extends HttpServlet
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    String idStr="";
    String buffString="";
    InputStream is;
    byte[] buffer=null;
    Connection conn = null;
    //Statement stmt =null;
    CallableStatement stmt;
    ResultSet rs;
    //BlobStudentInfo bInfo = null;
    //Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
    //idStr = (String)request.getParameter("id");
    //bInfo = (BlobStudentInfo)request.getAttribute("binfo");
    try {
    //stmt = conn.getConn().createStatement();
         Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
         conn = DriverManager.getConnection("jdbc:sybase:Tds:dbserv.com.xyz.edu:4444/mydb","user","pwd");
         stmt = conn.prepareCall("{call dbo.getimage(?)}");
         stmt.setInt(1,10);
         stmt.execute();
    stmt.getMoreResults();
    rs = (ResultSet) stmt.getResultSet();
    while (!rs.isFirst()) {
    rs.next();
    buffer = rs.getBytes("image");
    // rs = stmt.executeQuery("select image from photo where indexing =10");
    if (rs.equals(null)){
         System.out.println("Resultset is not null");
    }else System.out.println("RS is not null");
    //while (rs.next());{
         // buffer = rs.getBytes("image");
    stmt.close();
    rs.close();
    //conn.closeConnection();
    } catch(SQLException sqe){
    while (sqe != null) {
    System.out.println("BlobImageServlet: SQLException: " + sqe.getMessage());
    //e.printStackTrace();
    sqe = sqe.getNextException();
    } catch (Exception e) {
    System.out.println("BlobImageServlet: Exception !!");
    e.printStackTrace();
    //buffer = bInfo.getHeadshot();
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new ByteArrayInputStream(buffer));
    BufferedImage image = decoder.decodeAsBufferedImage() ;
    response.setContentType("image/jpeg");
    // Send back image
    ServletOutputStream sos = response.getOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
    encoder.encode(image); // writes the image to browser
    } //doPost closed
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException
    doPost(request, response);
    and the corresponding procedure is
    create procedure dbo.getimage
    @indexing int
    as
    select image from dbo.photo where indexing = @indexing
    Whenver I try to run the servlet as http://localhost:8080/Test/BlobImageServlet
    I get null pointer exception
    type Exception report
    message
    description The server encountered an internal error () that prevented it from fulfilling this request.
    exception
    java.lang.NullPointerException
         java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:89)
         image.BlobImageServlet.doPost(BlobImageServlet.java:72)
         image.BlobImageServlet.doGet(BlobImageServlet.java:86)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.
    Please let me know if you have any questions.

    When we see the following code
    response.setContentType("image/jpeg");
    we can only view the images from database that were stored of jpeg but not of gif.
    Like gif images stored in binary format doesnt allow us to view back the images instead give some exceptions like
    com.sun.image.codec.jpeg.ImageFormatException: Not a JPEG file: starts with 0x42 0x4d
         sun.awt.image.codec.JPEGImageDecoderImpl.readJPEGStream(Native Method)
         sun.awt.image.codec.JPEGImageDecoderImpl.decodeAsBufferedImage(JPEGImageDecoderImpl.java:210)
         image.BlobImageServlet.doPost(BlobImageServlet.java:67)
         image.BlobImageServlet.doGet(BlobImageServlet.java:80)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
         javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    If I have to veiw a gif image from the database it doesnt allow me to do so giving jpeg exceptions.
    I have put a javascript asking the uploaders to load only jpeg files.
    but i would be happy if it was generic. Is there any solution.
    Regards
    Sandeep.

  • How to display local image using JSP in Struts

    I am trying to display image from my local disk (eg. file://C:/Documents%20and%20Settings/varshaaz/Desktop/DSC_6172.jpg). When I execute my JSP(http://localhost:8080/sharedgreeting/test.jsp) , Image doesn't display instead I see cross icon. Though properties of that icon shows correct file path.
    Content of my JSP is :
    <html>
    <body>
    <img src="file://C:/Documents%20and%20Settings/varshaaz/Desktop/DSC_6172.jpg"/>
    </body>
    </html>

    Store your images in a folder (I've called mine gfx) under the WebContent folder of the WAR and make a call to that folder using a relative link rather than one on your hard disk, if it is a web project.
    WebContent
    --- gfx
    --- stylesheets
    --- etc...
    --- createMeetingOrSomething.jsp
    Then the link in the above jsp will be:
    <img src="gfx/someJPEG.jpg" height="50">Illu
    Message was edited by:
    Illu

  • Getting an image path & display an image  in JSp

    hi
    How to get image & particular image path stored in Database .in Jsp [ Image stored in anther drive ( D:\images) ]
    & my webroot dir in c:\apachetomcat\webapps....
    Any Help will be Appriciated
    [email protected]

    Crossposted in 10 forums.

  • Getting an image path & display an image  in JSp--webserver

    hi
    How to get image & particular image path stored in Database .in Jsp [ Image stored in anther drive ( D:\images) ]
    & my webroot dir in c:\apachetomcat\webapps....
    Any Help will be Appriciated
    [email protected]

    Crossposted in 10 forums.

  • Problem in displaying image on jsp page

    I want to display an image on jsp page.I copied the image the image in WebContent folder.
    I am able to see the image on the design pane when using the following code:-
    <img src="/image.gif" height="50" width="50"> but when i run it in the browser nothing gets diplsyed.
    also when i use the image container and select the source option that image is not displayed in the WebContents file..plz help.

    Hi,
    Seems that the Problem is there with the way how you specifuied the Image relative Path:
    You have specified the below: The opath of image starts with (/) forward slash:
    *<img src="/image.gif" height="50" width="50">*
    It means to access this Image WebLogic Container will form a Path like this:
    http://localhost:7001/image.gif
    BUT may be the image is available inside your Context root: So Change the <Img> tag like following in your JSP:
    *<img src="image.gif" height="50" width="50">*
    (*NOTE:* Never Start your src path with a Preceesing /)
    Now WLE will consider the path like below ..if your Applications Context root is "TestApp"
    http://localhost:7001/TestApp/image.gif
    I am Assuming that Inside the TestApp Application "image.gif" And "your.jsp" jSP pages are Co-Located (Means available in the Same Directory).
    Thanks
    Jay SenSharma
    http://jaysensharma.wordpress.com (WebLogic Magical Stuff)

  • ADF BC:Displaying Image On JSP page

    Hai All
    I'm using ADF BC and JDev 10.1.3.
    I want to show an image on my jsp page from database.
    For this I have written an servlet with the following code
    response.setContentType("image/gif");
    OutputStream os = response.getOutputStream();
    String amDef = "package.AppModuleName";
    String config = "Configuration Name";
    ApplicationModule am = Configuration.createRootApplicationModule(amDef, config);
    ViewObjectImpl vo =
    (ViewObjectImpl)am.findViewObject("ViewObject"); // get view object (the same as used in the table)
    System.out.println("vo:" + vo);
    vo.executeQuery();
    Row row = vo.first();
    BlobDomain image = (BlobDomain)row.getAttribute("field");
    //System.out.println("image:" + image);
    InputStream is = image.getInputStream();
    // copy blob to output
    byte[] buffer = new byte[10 * 1024];
    int nread;
    while ((nread = is.read(buffer)) != -1)
    os.write(buffer, 0, nread);
    os.close();
    Configuration.releaseRootApplicationModule(am, false);
    This servlet working perfectly.The image is displayed on the page while running
    the servlet alone.
    My problem is the image is not got displyed on the jsp page
    I tried following code to call the servlet
    <af:objectImage source="ImageServlet"/>
    <img src="ImageServlet" width="140px" height="50px" align="right">
    Where ImageServlet is the url-pattern in the web.xml for the servlet
    Both method are not working
    Any body please help me.......
    what I'm missing..............
    Is there any other way to display an image on JSP page using backing bean
    method.
    Thanks
    Ans

    HI,
    See: http://kuba.zilp.pl/?id=241
    Kuba

  • Image to JSP file

    I need to display an image to JSP file. This image is generated in Java file and returned as BufferedImage object to JSP.
    The image generated through Java file is not getting stored anywhere. I just need to pass the Image object to the JSP file.
    Please let me know how is it possible.

    Hello, this is a brief code of a simple web service I coded sometime ago, and tested using asp & jsp.
    Servlet class:
    public class ImageServlet extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
    try{
    CheckProducer checkProducer=(CheckProducer)Class.forName(request.getQueryString()).newInstance(); //class name entered in url eg. com.orly_otero.Images.pngImage
    byte[] out_stream=checkProducer.createCheck("dummy"); //Your image-generating class.
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    baos.write(out_stream, 0, out_stream.length);
    //System.err.println(out_stream.length); //Image Size.
    baos.writeTo(response.getOutputStream());
    response.setContentType("image/png");
    catch(Exception e){
    throw new ServletException(e);
    More important code of com.orly_otero.Images.pngImage
    public byte[] createCheck(String name) throws IOException{
    ByteArrayOutputStream stream=new ByteArrayOutputStream();
    try{
    ImageIO.write((java.awt.image.BufferedImage)image, "PNG", stream);
    catch (Exception je){
    throw new IOException(je.getMessage());
    return stream.toByteArray();
    image contains your awt drawed image.
    Hope this help.
    Regards,
    OO

  • Display image in JSP Portlet

    I create a JSP portlet. But The portlet can't display image(gif file, jpg file). I have modified the provider.xml and the following line is added:
    <imageURL>URL_Path</imageURL>
    But, the image still cannot be displayed.
    How can I display image in JSP portlet?

    Leo Cheung,
    You could try the following :
    1. Add a virtual directory path Alias 'imgf' in the Apache configuration file httpd.conf to load the image file. Add the following line under the alias section :
    Alias /imgf/ "<your directory>\images/"
    2. Place your gif/jpg files (eg., work.gif) in the images directory.
    3. Use the IMG tag of HTML :
    <IMG src="/imgf/work.gif" border=0 width=80 height=80> in the JSP file at the location where you need to display the image.
    Hope this helps
    Pushkala

Maybe you are looking for