Solved

Upload/Download Files to SQL Server Database

Posted on 2004-10-15
8
674 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
  • 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 500 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now