Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Answers Viewqa JDBC upload ,download and update file document

Have Programming Question? Ask it here!

upload ,download and update file document
2 Answer(s)      5 years and 3 months ago
Posted in : JDBC

View Answers

May 23, 2009 at 9:46 AM

Hi Friend,

Try the following code to upload the word file:


<%@ page language="java" %>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>

<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<center><table border="2" >
<tr><center><td colspan="2"><p align="center"><B>UPLOAD THE FILE</B><center></td></tr>
<tr><td><b>Choose the file To Upload:</b>
<td><INPUT NAME="file" TYPE="file"></td></tr>
<tr><td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>


<%@ page import="*" %>
<%@ page import="java.sql.*" %>

String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead =, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));

%><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b>
<% out.println(saveFile);%></td></tr></table>
Connection connection = null;
String connectionURL = "jdbc:mysql://";;
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try {
connection = DriverManager.getConnection(connectionURL, "root", "root");
File f = new File(saveFile);
psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
fis = new FileInputStream(f);
psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
int s = psmnt.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
else {
System.out.println("unsucessfull to upload file.");
catch(Exception e){e.printStackTrace();}

May 23, 2009 at 9:47 AM


Now to download the word file, try the following code:

<%@page import="java.sql.*"%>
<%@page import="*"%>
<%@page import="javax.servlet.*"%>
ServletOutputStream output = response.getOutputStream();
String connectionURL = "jdbc:mysql://";;;;
String Content=new String("");
Connection con=DriverManager.getConnection(connectionURL,"root","root");
Statement st=con.createStatement();
ResultSet rst= st.executeQuery("select file_data from file where id=25 ");
}catch(Exception e){
System.out.println("Exception caught"+e.getMessage());
byte requestBytes[] = Content.getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(requestBytes);
response.setHeader("Content-disposition","attachment; filename=" +"file.doc");
byte[] buf = new byte[1024];
int len;
while ((len = > 0){
output.write(buf, 0, len);


Related Tutorials/Questions & Answers:
upload ,download and update file document - JDBC
upload ,download and update file document   hi sir.i am creating one application in jsp in which i need to upload the word document file which is nearly 1 mb.again i want to update that document with another whose size is 1.1 mb
file upload download - Java Beginners
file upload download  how to upload and download files from one system to another using* and* only please send me code
Upload and download file - JSP-Servlet
the word document file: 1)page.jsp Display file upload form............ Now to download the word document file, try the following code...Upload and download file  What is JSP code to upload and download
upload and download a file - Java Beginners
upload and download a file  how to upload a file into project folder... the following code: 1)page.jsp: Display file upload form to the user UPLOAD THE FILE   Choose the file To Upload
File Upload And download JSP Urgent - JSP-Servlet
File Upload And download JSP Urgent  Respected Sir/Madam, I... for it.. In the same application, I need the coding for File Upload and File Download in JSP.. In the Admin window, There must be "Upload" provision where admin
java code to upload and download a file - Java Beginners
java code to upload and download a file  Is their any java code to upload a file and download a file from databse, My requirement is how can i... and Download visit to : http
code to upload and download a file - Java Magazine
code to upload and download a file  Is their any code to upload a file and download a file from databse, My requirement is how can i store two... user send one a.doc file to x ,he has to download his a.doc another user sends
upload and download mp3
upload and download mp3   code of upload and download Mp3 file in mysql database using jsp and plz mention which data type used to store mp3 file in mysql database
Upload and Download multiple files
Upload and Download multiple files  Hello Sir/Madam, I need a simple code for upload and download multiple files(it may be image,doc... link:
upload and download video
upload and download video  how to upload and download video in mysql...;Display file upload form to the user</TITLE></HEAD> <BODY> <FORM... the file To Upload:</b> </td> <td><INPUT NAME="file" TYPE="file
upload and download files - JSP-Servlet
upload and download files  HI!! how can I upload (more than 1 file) and download the files using jsp. Is any lib folders to be pasted? kindly... and download files in JSP visit to :
Photo upload, Download
Photo upload, Download  Hi I am using NetBeans IDE for developing application for java(Swings). And i am using MySQL as backend database. My...) { File myFile = chooser.getSelectedFile(); try { image
file download
file download  I uploaded a file and saved the path in database. Now i want to download it can u plz provide code
File Download in jsp
File Download in jsp  file upload code is working can u plz provide me file download
Upload and Download in JSP - JSP-Servlet
Upload and Download in JSP  Respected Sir/Madam, I am... and downloading a file in JSP.. When the admin clicks upload, he must be able to upload.... Please visit for more information.
File Upload
File Upload  when i execute the image upload to mysql database it shows an exception that file path cannot be found
Upload and Download Large files in jsp
Upload and Download Large files in jsp  I am not able to download large files (>200mb) from any server. I need a code to download and upload large files (atleast 4 gb) to a server using jsp page
how to upload and download images in java?
how to upload and download images in java?  what is the code....   Upload and Download images: 1)page.jsp: <%@ page language="java" %> <HTML> <HEAD><TITLE>Display file upload form
how to upload and download images using buttons in jsp?
how to upload and download images using buttons in jsp?  how to upload and download images using buttons in jsp
Struts File Upload and Save
Struts File Upload and Save  ... regarding  "Struts file upload example". It does not contain any... example will  provide you with the code to upload the file ,in the upload
upload and retrieve a word document - JSP-Servlet
you.  Hi friend, To upload a file in data base steps to be remember...; 2.Create a form in jsp page "page.jsp" : Display file upload form to the user UPLOAD THE FILE   Choose the file To Upload
file upload plugin in jquery
file upload plugin in jquery  file upload plugin in jquery   You can use the following code to use the JQuery plugin. $(document).ready(function() { $("#uploadbutton").jsupload({ action: "
File Upload
File Upload  Hi I need script / string in PHP where I uploaded the file in html frontend and it should sent the uploaded file as attachment to my email address
file upload
file upload  how to recieve the uploaded file on the next jsp page for modification if its uploaded to the previous page using a from
File Upload in Struts.
File Upload in Struts.  How to do File Upload in Struts
php download file script
php download file script  PHP script to download file in IE
Download PDF file
Download PDF file  How to download PDF file with JSF
upload and save the file
upload and save the file  upload and save a file from a computer to specific location of remote computer using jsp
Uploading and download pdf or .txt file.
Uploading and download pdf or .txt file.  I want admin user to upload pdf file into database and the users can download those pdf format from database
jsp upload file to server
jsp upload file to server  How to create and upload file to server in JSP?   Find the given example that explains how to upload single and multiple file on server using JSP
Download file - JSP-Servlet
Servlet download file from server  I am looking for a Servlet download file example
php download file code
php download file code  PHP code to download the files from the remote server
how to download file
how to download file  How do I let people download a file from my page
php download file browser
php download file browser  limiting downloading file system via browser only through my web application
Download a file - Java Beginners
Download a file   Hi, I need a java code to download a file from a website and before downloading it should give me an option to browse and select the location to put the file. Thanks in advance
servlet file upload
servlet file upload  please tell me the complete code to upload a file on localhost using servlet
how to download a file from a folder??
how to download a file from a folder??  i can upload any kind of files to a folder, and only the path of the file is saved into the database, now how a client can download the file from my folder. pls provide me the jsp code
AJAX file upload
AJAX file upload If you want to upload file asynchronously means without... Demo URL Description You need to create a form with file fields you wish to upload and define
upload a file - Framework
upload a file  Hi to All, I need to upload a file from the client and that upload file should be sent to our co., mail please if any one provide.... Thankyou in Advance.  Hi friend, For upload a file in JSP visit
Upload file to MS Access
Upload file to MS Access  Iam beginner and need to upload file to msaccess using jsp . Iam using netbeans platform. Kindly explain do I need to mention Attachment format in the table
File Download Security
File Download Security   Hello, I trying to create an application in PHP to prevent or provide file download security. So, I need the help of senior... files downloading. Please read this article about File Download Security
Struts upload file - Framework
Struts upload file  Hi, I have upload a file from struts... and send to file upload to get the sheets and data in that sheets Thanks.  Hi friend, For upload a file in struts visit to : http
Upload file with ather Fieldn
Upload file with ather Fieldn  hello i want upload file with other record to database for my project,like that (FileID, Filename,Filedescrption,Filedate,file_upload) see( upload file with path) i can send data to database
File Upload and Retrive files
File Upload and Retrive files  Can any body help me am getting an error in uploading file. into mysql database.... thank's in advance
file upload in spring - Spring
file upload in spring  Is it necessary to use Multipart support in spring framework for file uploads.If not please let me know the other ways to do
Multiple File Upload in PHP
Multiple File Upload in PHP  Hi, I am beginner in PHP scripting language. I am very interested to learn PHP application. So, can anyone explain or provide related reference about how to Multiple file upload in PHP. Thanks
File upload in JSP
File upload in JSP  hi! In my previous interview i got two questions which i could not answer regarding file upload and annotations. I want to know which is the best method can be used for file upload. Whether moving them
file download in struts - Struts
file download in struts  Hello... I've designed an application where i need to download an xml file by creating it... so...i used dom for it.. I... an xml file from database.. Everything's going on well... Now what my problem is i
how to update the text file?
how to update the text file?  if my text file contains a string and integer in each line say,: aaa 200 bbb 500 ccc 400 i need a java code to update the integer value if my input String matches with the string in file. please
csv file download
csv file download  Hello Every one, when user clicks download button I want to retrieve data from mysql database table in csv format using java.if you know please send code. Thanks in Advance   Please visit