| | |
Store and retrieve image from mysql db with JSP
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Feb 2009
Posts: 4
Reputation:
Solved Threads: 0
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
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
JSP Syntax (Toggle Plain Text)
<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>
Last edited by peter_budo; Feb 16th, 2009 at 8:26 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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
On other hand Java is more secure and provide large scope of add-on technologies that may increase your productivity
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- JSP database connectivity according to Model View Controller (MVC) Model 2 (JSP)
- Displaying Images( buffered data ) from the Database using Java (Java)
Other Threads in the JSP Forum
- Previous Thread: how to insert data in mysql with jsp
- Next Thread: importing sql query result sets into java
Views: 2252 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for JSP
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient project read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write






