?
Solved

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

Posted on 2003-02-23
6
Medium Priority
?
195 Views
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.
0
Comment
Question by:kaushikgmehta
[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
6 Comments
 
LVL 17

Expert Comment

by:dorward
ID: 8002656
0
 
LVL 7

Expert Comment

by:Big_Red_Dog
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
0
 

Accepted Solution

by:
ccmehil earned 400 total points
ID: 8017697
Hello,

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

Craig

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">

<html>
<head>
     <title>Untitled</title>
</head>

<body>

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

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

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

</form>


</body>
</html>

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);

fileData.read(buffer);
fileData.close();

uploadContent = new String(buffer); // New String object
/*
 * Create your specific DB INSERT statment here
*/
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      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.

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 1

Expert Comment

by:Lycaon
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.

James
0
 

Author Comment

by:kaushikgmehta
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.
0
 

Expert Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 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