943,928 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 6824
  • JSP RSS
Feb 16th, 2009
1

Store and retrieve image from mysql db with JSP

Expand Post »
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

JSP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 9
Solved Threads: 0
Newbie Poster
gusfraba is offline Offline
3 posts
since Feb 2009
Feb 16th, 2009
-1

Re: Store and retrieve image from mysql db with JSP

Have you tried
this.ShootMySelf(this.myGun);
Reputation Points: 9
Solved Threads: 2
Newbie Poster
danielernesto is offline Offline
16 posts
since Nov 2007
Feb 16th, 2009
0

Re: Store and retrieve image from mysql db with JSP

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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Feb 16th, 2009
0

Re: Store and retrieve image from mysql db with JSP

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
Reputation Points: 9
Solved Threads: 0
Newbie Poster
gusfraba is offline Offline
3 posts
since Feb 2009
Feb 25th, 2009
0

Re: Store and retrieve image from mysql db with JSP

hey m a biotechnologist. i want to develop a database . can u tell what would be easy to learn.. JSP or PHP???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nm.nehamehta11 is offline Offline
1 posts
since Feb 2009
Feb 25th, 2009
0

Re: Store and retrieve image from mysql db with JSP

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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Feb 25th, 2009
0

Re: Store and retrieve image from mysql db with JSP

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
Reputation Points: 9
Solved Threads: 0
Newbie Poster
gusfraba is offline Offline
3 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: how to insert data in mysql with jsp
Next Thread in JSP Forum Timeline: importing sql query result sets into java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC