what are the codes for uploading and downloading files in and from database?


what are the codes for uploading and downloading files in and from database?

We are designing a student's website where they can get and post study materials. We actually need the code to upload and download files in and from the database. we will be very gratefull if you provide us those with a bit of explanations.

Thank you.

View Answers

April 16, 2011 at 12:53 PM


<%@ page language="java" %>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<table border="0" bgcolor=#ccFDDEE>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
<td colspan="2" align="center">&nbsp;</td>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
<td colspan="2" align="center">&nbsp;</td>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>


<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
String saveFile="";
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 = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
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;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b>
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/test";
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
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();
System.out.println("Uploaded successfully !");
catch(Exception e){e.printStackTrace();}
<a href="downloadFile.jsp?f=<%=saveFile%>">Download File</a>

April 16, 2011 at 12:54 PM


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

<%@ page import="java.net.*"%>
public static String getMimeType(String fileUrl)
throws java.io.IOException, MalformedURLException
String type = null;
URL u = new URL(fileUrl);
URLConnection uc = null;
uc = u.openConnection();
type = uc.getContentType();
return type;

String file=request.getParameter("f");
File f = new File (file);
String filename=f.getName();
String type=getMimeType("file:"+file);

ServletOutputStream output = response.getOutputStream();
String connectionURL = "jdbc:mysql://localhost:3306/test";
String Content=new String("");
Connection con=DriverManager.getConnection(connectionURL,"root","root");
Statement st=con.createStatement();
ResultSet rst= st.executeQuery("select * from file");
}catch(Exception e){
System.out.println("Exception caught"+e.getMessage());
byte requestBytes[] = Content.getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(requestBytes);

response.setContentType (type);
response.setHeader ("Content-Disposition", "attachment; filename=\""+filename+"\"");byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) > 0){
output.write(buf, 0, len);


Related Tutorials/Questions & Answers:
what are the codes for uploading and downloading files in and from database?
files uploding and downloading codes
Uploading files
codes for banking databases
codes for banking databases
codes for banking databases
uploading and copying files
FORM Tag Correctly for Uploading Files.
How To Write the FORM Tag Correctly for Uploading Files?
Uploading Multiple Files Using Jsp
is it possible to connect the multiple databases from the hibernate application
downloading a file directly from mysql using java
Uploading file in servlet from a html form
what is wrong with my JSP codes for updating a form?
getting files from VSS
What is docker from scratch
Java: Searching a string from doc files
Create xsd files from database tables
Uploading Multiple Files Using Jsp
error in uploading image from jsp to oracle 10g database
how to copy files from remote location to local using java?
find jar file name from multiple set of jar files
retrieve multiple columns values from multiple csv files in java
Databases supported by Jdeveloper?
java program to read multiple files from a directory and display them on a jframe
reading multiple files from a directory and writing them into a single file
Uploading File on Server
How to run java swing application from jar files .
How to read PDF files created from Java thru VB Script