NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Problem sending images to browser

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
kawalya



Joined: 07 Sep 2009
Posts: 12

PostPosted: Tue Apr 13, 2010 4:43 pm    Post subject: Problem sending images to browser Reply with quote

Hi folks,
I am having a little problem in Netbeans 6.5.

I am in the JSF managed bean;
I call
Code:
System.getProperties(“user.dir”)

to get the current working directory and get
Code:
/home/dk/glassfish-v2.1/domains/domain1/config


So I upload an image file and create a copy in the file system relative to the above ie
Code:
web/Images/myImage.jpg

Checking in the application server, the image actually exists on the above path.

So I store another copy of the image bytes, the above path, and other image parameters in the database.

I direct
Code:
<h:graphicImage>
to the above url to get the image.

Running the application, the image is not displayed, but an anchor thumbnail showing there is supposed to be an image on the page.

I click on the displayed thumbnail and choose properties to get the image location and get
Code:
location = http://localhost:8080/MyImages/faces/+/home/dk/glassfish-v2.1/domains/domain1/config/web/Images/myImage.jpg


I right click again and choose view image and get
Code:
   HTTP Status 404 -
type Status report
message
descriptionThe requested resource () is not available.
   Sun GlassFish Enterprise Server v2.1


So how do I then direct the system to find the image?

Any help will be appreciated.
Thanks.
Back to top
kawalya



Joined: 07 Sep 2009
Posts: 12

PostPosted: Thu Apr 15, 2010 12:11 pm    Post subject: Reply with quote

This was solved.
The problem was creating files in the application server as opposed to the project folder.
Back to top
kawalya



Joined: 07 Sep 2009
Posts: 12

PostPosted: Thu Apr 15, 2010 12:11 pm    Post subject: Reply with quote

This was solved.
The problem was creating files in the application server as opposed to the project directory.
Back to top
kawalya



Joined: 07 Sep 2009
Posts: 12

PostPosted: Wed Apr 28, 2010 4:11 pm    Post subject: Reply with quote

I think a better aproach is even using a servlet ie
The database entity POJO is as below
Code:

public class DavisContent implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "content_id")
    private String contentId;
    @Basic(optional = false)
    @Column(name = "content_type_id")
    private String contentTypeId;
    @Basic(optional = false)
    @Lob
    @Column(name = "content")
    private byte[] content;
      .
      .
}

Then the image loader servlet
Code:
private DavisContent davisContent = null;

    @PersistenceUnit(unitName = "DavisBlobsPU")
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public DavisContent findDavisContent(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(DavisContent.class, id);
        } finally {
            em.close();
        }
    }

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
 //This is the parameter from the h:graphicImage tag
        String file = request.getParameter("file");        davisContent = findDavisContent(file);
       
        try {
            if(davisContent.getContentTypeId().contains("image")){
            response.getOutputStream().write(davisContent.getContent());
            }
        } finally {
            //out.close();
        }
    }

The servlet configuration in web.xml
Code:
<servlet>
        <servlet-name>ImageServlet</servlet-name>
        <servlet-class>dave.ImageServlet</servlet-class>
    </servlet>
<servlet-mapping>
        <servlet-name>ImageServlet</servlet-name>
        <url-pattern>/image/*</url-pattern>
    </servlet-mapping>

Then finally one of the columns in the h:dataTable
Code:

<h:column>
                            <f:facet name="header">
                                <h:outputText value="Content"/>
                            </f:facet>
                            <h:graphicImage height="50" width="40" value="/image?file=#{item.contentId}">
                                </h:graphicImage>
                        </h:column>
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo