• Post Reply Bookmark Topic Watch Topic
  • New Topic

upload a file to dabase

 
siddharth paul
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey can any one tell how i can upload a file from jsp and save it into database.And how will i know its saved in database.I am using sql server as database.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

File upload is a task for servlets, not JSPs. See http://faq.javaranch.com/java/FileUpload for further information on Apache Commons FileUpload, a popular library that facilitates this.

File contents are best stored in a DB using a CLOB or BLOB field, depending on whether it's text or binary data. You might also want to read this thread on whether it's a good idea to store files in a DB.
 
siddharth paul
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i didn't helped with the link. I need the code.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need the code.

And we'll help you write it, but we won't do it for you. Did you look at the FileUpload library? Its user guide has extensive explanations and example code.

As to saving to a database, are you familiar with JDBC? If not, you may to start reading here.
[ April 30, 2008: Message edited by: Ulf Dittmer ]
 
siddharth paul
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks.
i know jdbc connection and i have stored a file in sql server database. in the database field of file its written binary data,then how will i know its stored in database or not.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you didn't get an exception while saving it's a good bet that it did get saved. You can always use SQLServer's tools to look at the DB directly to see what did get saved, or use JDBC to retrieve it and compare that with what you wanted to save.
 
siddharth paul
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot
its gud that i didn't got any exception.
Now can you help me how to retrive a file from database to local disk as like we download attachments from mail.
I mean what should i write in code like getBLOB() or anything else.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I think you can get an InputStream from the ResultSet that allows you to read the BLOB. You can then use regular file I/O to write its contents to disk.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!