954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Store and retrieve image from mysql db with JSP

I've had nighmares about this since 3 days ago and couldn't find anything by googling so here I am.

As the title says so i need to find a way to store and then retrieve images from a mysql database I have tired the code below to do so (the storing part) but it just saves empty blobs.

I'd apreciate the help and since i haven't seen much about the topic so will a lot of people

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <%
            String description = null;

            MyImage image= new MyImage();

            try
            {
                //Download com.oreilly package
                MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024);
                Enumeration files=multi.getFileNames();
                File file=null;

                for(int i = 0 ; files.hasMoreElements() ; i++)
                {
                    String name=(String)files.nextElement();
                    String filename=multi.getFilesystemName(name);
                    String type=multi.getContentType(name);
                    file=multi.getFile(name);
                    description = multi.getParameter("description");

                    out.println("The File is "+archivo.getCanonicalPath() + "");

                    BufferedImage img = ImageIO.read(file);
                    ByteArrayOutputStream bas = new ByteArrayOutputStream();

                    ImageIO.write(img, "pnm", bas);
                    
                    byte[] data = bas.toByteArray();


                    if(i == 0){
                        image.setLoginImage(data);
                    }
                    if(i == 1){
                        image.setReportImage(data);
                    }
                }

                image.setDescription(description);

               String error = validateFields();

                if(error.equals("")){
                    try{
                        EntityManager em = MyEntityManager.getEntityManager();
                        em.getTransaction().begin();
                        em.persist(image);
                        em.getTransaction().commit();
                    }catch(Exception e){
                        session.setAttribute("Error", e.getMessage());
                    }
                }else{
                    session.setAttribute("Error", error);
                }
            }catch(Exception e){
                session.setAttribute("Error", e.getMessage());
            }
        %>
    </body>
</html>
gusfraba
Newbie Poster
3 posts since Feb 2009
Reputation Points: 9
Solved Threads: 0
 

Have you tried
this.ShootMySelf(this.myGun);

danielernesto
Newbie Poster
16 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
 

Have look at this example . If you modify code correctly you will be able to select image to upload through JSP and submit it to servlet for database insertion

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Thanx Peter_budo I had already seen an example like that one but this one is easier to modify, I'll give it a try

gusfraba
Newbie Poster
3 posts since Feb 2009
Reputation Points: 9
Solved Threads: 0
 

hey m a biotechnologist. i want to develop a database . can u tell what would be easy to learn.. JSP or PHP???

nm.nehamehta11
Newbie Poster
1 post since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

I will say PHP if you looking for faster learning and development cycle as JSP require quite extensive knowledge of Java to be able to comprehend and correctly use other Java web based technologies.
On other hand Java is more secure and provide large scope of add-on technologies that may increase your productivity

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

It depends a lot on you background if you already know java it'll be much easier with jsp but if you're starting from zero php is better, more flexible and there's more documentation about it

gusfraba
Newbie Poster
3 posts since Feb 2009
Reputation Points: 9
Solved Threads: 0
 

On the question here is a good tutorial:
JSF - Displaying images from database in JSF

imdeadsoul
Newbie Poster
3 posts since Apr 2012
Reputation Points: -1
Solved Threads: 1
 

Post: Markdown Syntax: Formatting Help
You