Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Store a PDF in a mySQL DB using HTML jQuery and Java  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to store a pdf (each pdf will be < 5 pages maximum) in a mySQL database (yes I know this is not recommended). The column is defined as mediumblob. The test pdf is 198 KB. I get the error "Errors occurred in addMedicalPlan: java.io.FileNotFoundException: " followed by a dump of the file " JVBERi0xLjcKCjQgMCBvYmoKKElkZW50aXR5KQplbmRvYmoKNS ......" and at the end "(The filename or extension is too long)".

I have managed to do this with images and have been trying to re-purpose that code to store the pdf; by researching articles and other questions. Unfortunately, for me, most of the example relate to PHP. I have been able to read the pdf and display it in the page with this code:

HTML:



jQuery:



Do I need to strip off  "data:application\/(pdf);base64", i.e.,  img2 = (img1.getAttribute('src')).replace(/^data:application\/(pdf);base64,/, ""); for the pdf? I do similar for storing my images (i.e., img2 = (img1.getAttribute('src')).replace(/^data:image\/(png|jpg|jpeg|gif);base64,/, "");). Is this superfluous for pdf files?

I then pass the pdf to be stored with ajax:



And the server side java is:




I am not a programmer and am doing this pro-bono and learning as I go for my organisation.

Kind regards,

Glyn


 
Glyndwr Bartlett
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I ended up doing.

HTML:



jQuery:



SQL:

             
 
Rancher
Posts: 515
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Take a look in to pdfBox.
I never used it to save a pdf to db, not sure if you're will be able to, I did use it before to convert a pdf in to a buffered image to show it in a javaFx Imageview, which you would be able to safe
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of interest why do you need to Base64 encode it and then decode it to store?

You could just save it as a Blob without this extra step. I don't know your use case but I have never had to Base 64   encode manually except for when sending files to rest endpoints.
 
Daniel Demesmaecker
Rancher
Posts: 515
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to save anything. I would create a seperate folder on your server and just store the url's
 
Beeimbolo Paul
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at apache commons file upload or something similar?

It's going to help reduce the boiler plate code you're writing.

If you want to send the pdf back as base64 you could just save the string as a Clob in the database instead.
 
Beeimbolo Paul
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:You don't need to save anything. I would create a seperate folder on your server and just store the url's



How does that work if you have multiple load balanced servers?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!