0

I am working on project which needs to display images and text on the browser.I have stored the image in the database in the blob field. I am extracting the blob image from the database and storing it in a byte field. And then using the fileoutputStream to store image in the webcontent folder. To fetch the image i am providing the url of the image to the src tag.

code for DAO class is

          while (rs.next()) 
            {
                Profile = new UserProfilePojo();
                Profile.setImage1(rs.getBlob(14));
                list.add(Profile);
             }
           rs.close();  

code written in pojo class is

       public byte[] getImage()
        {
            return barr;
        }

        public void setImage(Blob image1)
        {
             try 
             {
                this.barr =image1.getBytes(1,(int)image1.length());                   
                byte[] thumbbarr=resizeImageAsJPG(barr,200);
                FileOutputStream fout=new FileOutputStream("E:\\my work\\redirecttest\\WebContent\\"+idm+fname+".jpeg");             
                fout.write(thumbbarr);
                fout.close();

             }
             catch(NullPointerException | SQLException | IOException n)
             {
                n.printStackTrace(); 
             }

on the jsp page it is written as

          <img src="${Profile[0].id}${Profile[0].fname}imp.jpeg">

Although i am able to fetch the image but i have to refresh the webcontent folder after page load and then refresh the page to display the image.

I just wanted to ask that am i able to run this when the project will be uploaded to the remote server. If so then what should be the location of the fileoutputstream. And if it won't work then please give me some suggestion to display image and data simultaneously on the browser from database.

Thanks

Ankit Sahu
  • 85
  • 4
  • 17
  • what will happen if two user will request for image at same time ? what i can see is same image to both – Keval Apr 16 '15 at 06:34
  • @Keval Yes of course, That's what i am trying to do. But the problem is saving image inside the web content folder is a wrong approach so i want to save it outside the web content Folder. I have set the context path but still i am facing the problem of fetching the image. could you please help me regarding this. Thanks! – Ankit Sahu Apr 23 '15 at 05:20

1 Answers1

2
  • You can write two separate call for fetch data and image
  • Make these two calls as asynchronize
  • It solves your problem and data and image will load simultaneously.
Ranjeet
  • 636
  • 6
  • 12
  • Actually after researching on this problem i found that saving the image fetched from the Blob field into the webcontent folder is a wrong approach. So i am changing the plan. Now i am saving it out side the webcontent but the problem is i am unable to access those file directly as i am able to do in webcontent folder. – Ankit Sahu Apr 23 '15 at 05:17
  • @AnkitSahu I think It's not good approach to store image outside WebContent folder. – Ranjeet Apr 23 '15 at 05:36
  • i have gone through Answer of [This question](http://stackoverflow.com/questions/18664579/recommended-way-to-save-files-uploaded-to-a-tomcat-servlet/) which clearly specify that saving dynamic images inside webcontent folder is not a good idea. As i will create problem during the redeployment. – Ankit Sahu Apr 23 '15 at 05:56