Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2003-02-23
Medium Priority
Last Modified: 2012-05-04
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.
Question by:kaushikgmehta
LVL 17

Expert Comment

ID: 8002656

Expert Comment

ID: 8002944
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: http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_20510967.html 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: http://www.experts-exchange.com/Databases/Mysql/Q_20465611.html about BLOB files, though

Accepted Solution

ccmehil earned 400 total points
ID: 8017697

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 ) {
java.io.File file = new java.io.File(aUploadedFile);
byte buffer[] = new byte[(int)file.length()];
java.io.InputStream fileData = new java.io.FileInputStream(aUploadedFile);


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.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8038864
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.


Author Comment

ID: 8487405
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.

Expert Comment

ID: 8496039
No problem, if you need anything else please let me know.

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
The viewer will learn how to dynamically set the form action using jQuery.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

580 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