Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Upload/Download Files to SQL Server Database

Posted on 2004-10-15
8
Medium Priority
?
761 Views
Last Modified: 2012-05-05
How to upload and download any type of file (.doc, .pdf, .jpg, etc.) to SQL Server database using JSP and Java? We are using JRun 4.0 and SQL Server version 8.0. Any helpful hints and codes would be greatly appriciated. Thank you.
0
Comment
Question by:jspmw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
8 Comments
 

Expert Comment

by:kheefatt
ID: 12326139
I can help u out in the uploading to the database but I'm not very sure about downloading it. Hope u can share some points if u found it useful. Get back to you with the codings later
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 2000 total points
ID: 12327746
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12327825
For downloading a file, basically the idea is usage of  BLob

After uploading a file, you need to convert the cotent into ByteArrayInputStream/byte array  to upload it into database


byte[] my_byte_array = ....  //data that is coming from upload compnent
  PreparedStatement statement = connection.prepareStatement(
      "INSERT INTO MyTable ( my_binary_column ) VALUES ( ? )");
  statement.setBytes(1, my_byte_array);
  statement.execute();




Here is a idea to download a file from database

 BLOB is a reference to data in a database. This example demonstrates how to retrieves bytes from a BLOB.

    try {
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT my_binary_column FROM MyTable ");
   
        if (rs.next()) {
            // Get the BLOB from the result set
            Blob blob = rs.getBlob("my_binary_column");
   
             
            // Get bytes from the BLOB using a stream
            InputStream is = blob.getBinaryStream();
            int b = is.read();
        }
    } catch (IOException e) {
    } catch (SQLException e) {
    }


here are some more example

http://www.jguru.com/faq/view.jsp?EID=1325
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/ic2924/index.htm?info/rzaha/PutGetBlobs.htm
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12327884
http://jakarta.apache.org/commons/fileupload/ is one of the good file upload componenets


for get to mention using jakarta HttpClient aslo you can upload file - this is also good to choose the API
http://jakarta.apache.org/commons/httpclient/
Example : http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21170748.html
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12407812
Hi,

I just  ur question in feedback.  I suggest you to ask questions  directly in question thread itself. Otherwise it is difficult to me because i do not get any notifications if u ask in feedback area


Coming to ur question,

try creating ur table as follows

fileid
filename
blob
extension
  (or)
contenttype


While inserting itself try inserting extension of file or contenttype. This is upto you to take the decision whether to use extension or contenttype.

now while fetching the blob

if u use Contennttype in database table, try some thing like this

<%
response.setContentType(resultset.get("contentttype"));
%>


Hope u get some Idea. If not try asking here itself. Not in feedback section ;-)

BTW, which upload component you are using??

Regards
Sudhakar.
0
 
LVL 2

Expert Comment

by:javazoom
ID: 12419745
For download, you could try Download4J :
http://www.javazoom.net/jzservlets/download4j/download4j.html
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
What we learned in Webroot's webinar on multi-vector protection.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question