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

x
?
Solved

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

Posted on 2003-02-23
6
Medium Priority
?
197 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
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
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.

 
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

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