Solved

Upload/Download Files to SQL Server Database

Posted on 2004-10-15
8
690 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse problems view not showing all the time 4 88
iterator example remove 8 74
HTML5 vs JSP - REST based application development 4 1,048
own marker interface 1 54
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
SEO can be a real minefield to navigate, but there are three simple ways to up your SEO game just be re-assessing your content output.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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