How to Upload a file and store the contents to the Database?

Using JSP,html or JavaScript how do i upload a file to the webserver like how they do on yahoo or other email service providers and then read the contents and store to the contents to database.The file can be a binary,text,doc,etc which will be known before(mostly binary)hand. Iam using Apache Webserver 3.23 ,MySQL and JSP,Java Beans, HTML,JavaScript.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First of all, Javascript is a client-side language, so we can't use it to do what you want to do.  I did provide a complete PHP/MySQL solution to this here: that you can easily convert to JSP (I don't have time this morning, but if you are a JSP person, you can see how easy it is to convert).  It uploads the file and then does more than you want because it parses a CSV file and inserts the content into the database.  Instead of this, you should just read the file and insert your file as a BLOB (or whatever) into the database.

Please read this discussion: about BLOB files, though

I've done somthing similiar maybe it will help get you started.


Step 1: Ensure that your setup allows for "multipart" form encryption and you either have it set to unlimited or to a specific amount.

Step 2: HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">



<form name="frm" method="get" action="upload.jsp" enctype="multipart/form-data">

<input type="File" name="myupload">

<input type="submit" name="submit" value="Submit">



Step 3: JSP

public String createFile(javax.servlet.http.HttpServletRequest request) {
String uploadContent = "";

try {
String aUploadedFile = request.getParameter("myupload");

// if a filename was given
if ( aUploadedFile != null ) { file = new;
byte buffer[] = new byte[(int)file.length()]; fileData = new;;

uploadContent = new String(buffer); // New String object
 * Create your specific DB INSERT statment here
      java.sql.Connection conn;
         conn = DriverManager.getConnection("jdbc:myurl:port", "username", "password");
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery("insert into <table> ( <field1> ) values ( '" + uploadContent + "' ) " );


} catch(Exception ex) {
System.out.println("Error creating file: " + ex );
return uploadContent; // Return the newly uploaded content to the screen
} // CreateFile

That should be able to help you if not feel free to contact me.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Actually you'd probably want to POST it if you're uploading anything other than very small text files.  GET sends the data via the URL, and any file bigger than a few (ten/hundred) kb will probably generate an error or not be uploaded correctly (or at all), since some browsers impose a URL length limit.

IE 5 and 5.5's limit is about 2k characters, and I think I saw somewhere that 4 and lower is 508 characters.  Not sure about IE 6.

kaushikgmehtaAuthor Commented:
Thnaks for the answer ccmehil. I am very sorry that i delayed in accepting the answer. Thanks to all others who took the pain to answer the question.
No problem, if you need anything else please let me know.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.