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() + "<br>");

                    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>

Recommended Answers

All 7 Replies

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

commented: This is not constuctive here -1

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

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

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.