Solved

Upload/Download Files to SQL Server Database

Posted on 2004-10-15
8
709 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 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
Industry Leaders: 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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting NoSuchMethodError on accessing a spring mvc project 3 181
struts hibernate integration example 28 157
withoutString  challenge 40 223
jsp login check 12 64
Determining the an SCCM package name from the Package ID
Check out this step-by-step guide for asking an anonymous question on Experts Exchange.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

732 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