Store and retrieve image from mysql db with JSP

Reply

Join Date: Feb 2009
Posts: 4
Reputation: gusfraba is an unknown quantity at this point 
Solved Threads: 0
gusfraba gusfraba is offline Offline
Newbie Poster

Store and retrieve image from mysql db with JSP

 
0
  #1
Feb 16th, 2009
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

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. </head>
  5. <body>
  6. <%
  7. String description = null;
  8.  
  9. MyImage image= new MyImage();
  10.  
  11. try
  12. {
  13. //Download com.oreilly package
  14. MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024);
  15. Enumeration files=multi.getFileNames();
  16. File file=null;
  17.  
  18. for(int i = 0 ; files.hasMoreElements() ; i++)
  19. {
  20. String name=(String)files.nextElement();
  21. String filename=multi.getFilesystemName(name);
  22. String type=multi.getContentType(name);
  23. file=multi.getFile(name);
  24. description = multi.getParameter("description");
  25.  
  26. out.println("The File is "+archivo.getCanonicalPath() + "<br>");
  27.  
  28. BufferedImage img = ImageIO.read(file);
  29. ByteArrayOutputStream bas = new ByteArrayOutputStream();
  30.  
  31. ImageIO.write(img, "pnm", bas);
  32.  
  33. byte[] data = bas.toByteArray();
  34.  
  35.  
  36. if(i == 0){
  37. image.setLoginImage(data);
  38. }
  39. if(i == 1){
  40. image.setReportImage(data);
  41. }
  42. }
  43.  
  44. image.setDescription(description);
  45.  
  46. String error = validateFields();
  47.  
  48. if(error.equals("")){
  49. try{
  50. EntityManager em = MyEntityManager.getEntityManager();
  51. em.getTransaction().begin();
  52. em.persist(image);
  53. em.getTransaction().commit();
  54. }catch(Exception e){
  55. session.setAttribute("Error", e.getMessage());
  56. }
  57. }else{
  58. session.setAttribute("Error", error);
  59. }
  60. }catch(Exception e){
  61. session.setAttribute("Error", e.getMessage());
  62. }
  63. %>
  64. </body>
  65. </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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: danielernesto is an unknown quantity at this point 
Solved Threads: 1
danielernesto danielernesto is offline Offline
Newbie Poster

Re: Store and retrieve image from mysql db with JSP

 
-1
  #2
Feb 16th, 2009
Have you tried
this.ShootMySelf(this.myGun);
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 4
Reputation: gusfraba is an unknown quantity at this point 
Solved Threads: 0
gusfraba gusfraba is offline Offline
Newbie Poster

Re: Store and retrieve image from mysql db with JSP

 
-1
  #3
Feb 16th, 2009
Originally Posted by danielernesto View Post
Have you tried
this.ShootMySelf(this.myGun);
Mean Motha****er
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,262
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 493
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Store and retrieve image from mysql db with JSP

 
0
  #4
Feb 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 4
Reputation: gusfraba is an unknown quantity at this point 
Solved Threads: 0
gusfraba gusfraba is offline Offline
Newbie Poster

Re: Store and retrieve image from mysql db with JSP

 
0
  #5
Feb 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: nm.nehamehta11 is an unknown quantity at this point 
Solved Threads: 0
nm.nehamehta11 nm.nehamehta11 is offline Offline
Newbie Poster

Re: Store and retrieve image from mysql db with JSP

 
0
  #6
Feb 25th, 2009
hey m a biotechnologist. i want to develop a database . can u tell what would be easy to learn.. JSP or PHP???
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,262
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 493
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Store and retrieve image from mysql db with JSP

 
0
  #7
Feb 25th, 2009
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
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 4
Reputation: gusfraba is an unknown quantity at this point 
Solved Threads: 0
gusfraba gusfraba is offline Offline
Newbie Poster

Re: Store and retrieve image from mysql db with JSP

 
0
  #8
Feb 25th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2252 | Replies: 7
Thread Tools Search this Thread



Tag cloud for JSP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC