Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 8th, 2003, 12:38 AM
Registered User
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to add attachment facility with JSP page.

Hello All,

I made a database driven JSP page, now i am takeing
some data in text from user and save it to database it works fine.

now i want to give facility of attachment,

means user can attach the file and i can store that
file in database (MS SQL Server 7.0)

so please tell how i can do that thing.

1] how to save attachment file to database.

If you have code then please write to me.

Please do replay at ft212@rediffmail.com, monika_jain_99@yahoo.com

Best Regards,
Old July 8th, 2003, 12:48 AM
Registered User
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

Please tell me the complit thing for file upload and download
from client to Server and Server to Client.

Best regards,
Old July 8th, 2003, 01:22 AM
Authorized User
Join Date: May 2003
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts

If I am not wrong with file attachment your are referring to file upload. This can be done in JSP by reading the file form the request input stream and after that you can perform any of the operations like write the file to some path on the server or store it in database. The following HTML code has to be inserted to make the user select a file and upload it in database:

<FORM METHOD=Post ENCTYPE='multipart/form-data' ACTION=”some jsp or servlet”>
<INPUT TYPE="file" name="uploadfile">
<INPUT TYPE="submit" value="Upload">

In the action attribute of the FORM tag you can specify the JSP or Servlet which will handle the file read write code.

For file reading
ServletInputStream stream = Request.getInputStream();
byte [] data = new byte[128];
int byteread;
StringBuffer buffer = new StringBuffer();
while((byteread = readstream.readLine(data,0,data.length)) != -1)
String str1 = new String(data);
buffer = buffer.append(str1);

The above code will read the content of the file and store it in a StringBuffer and it is up to you to use it. The browser also send some extra information(like the file name, it’s content type etc.) with the file content so make sure you remove this information before storing the file contents.

Hope this helps
Best Regards

Yashraj Chauhan
Java\J2EE Specialist
Wiley Support Team
Old July 8th, 2003, 02:49 AM
Registered User
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks a lot for replay!

but please tell me how I can store it to database,for
storing files i have Binary type column in MS Sql

please tell me how to store it in Database, from
client machine, and who we can send back to user while
when he wants to download the file.

basically i am tring to make Customer Support System.

Please do replay
Old July 8th, 2003, 07:32 AM
Registered User
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts


Reading file is ok, but can you tell me how I can store it into Database.

Here there is 2 possibility.

[1] I can store in Database ( MS SQL Server have Binary data type) .
[2] at server having folder there we can store.

So please tell me how I can store the complete file to database, ( file may have any type , rem.doc,
 Rem.jpg, rem.fax) after reading it in buffer how to insert into database.

Insert into dbo.personal ( file ) value ( file )

May be it can be done by BLOB but I don’t know how ? please tell me with code.

Do replay !
Old December 7th, 2004, 03:07 AM
Registered User
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

hi there,
 i have been able to upload a file on the server side and also able to bput the file in the database in a varchar field..(if anyone needs the code mail me at phredus@rediffmail.com)but things start to fail when the blob's come into picture.

What i did was i created a table with column of type BLOB in oracle sql database. but when i try putting the file n the blob it gives an error "Error in getting request param-->ORA-01465".. if someone knows how to do the stuff , ur help would truly be appriciated.


Old December 8th, 2004, 01:50 PM
Friend of Wrox
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts

I have done this with blobs (jpegs).
The real pain was stripping the headers out after the POST action.
I used a tutorial on the SUN Java site to learn how to parse the data, and got some other bits of code to put it into a database.
Come to think of it, putting it in the database was a pain as well.
I outlined what I did in the following thread:


Hope this helps, ultimately I dont think you are going to find a complete solution posted anywhere, believe me I looked!

Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection facility AnjuSujith ASP.NET 2.0 Professional 6 November 27th, 2007 01:13 PM
how to send email with attachment using JSP kirtesh4u J2EE 0 May 19th, 2007 01:17 AM
Add GIU in JSP Befekadu Java GUI 0 March 10th, 2005 12:55 PM
Next Page >> facility crmpicco Classic ASP Databases 7 March 10th, 2005 08:59 AM
How to add attachment facility with JSP page. monika_jain_99 J2EE 2 July 9th, 2003 12:34 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.