Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download file from MySql database

Status
Not open for further replies.

puk284

Programmer
Apr 25, 2010
1
GB
Hi,

I have written the following code to download a file from a MySql database using a Servlet.The problem i have is that i havnt done it right. Can someone please point me in the right direction. Any help will be greatly appreciated. Thank you for your help.

Java:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/
Servlet implementation class datadownload
/
public class datadownload extends HttpServlet {
private static final long serialVersionUID = 1L;

/
@see HttpServlet#HttpServlet()
/
public datadownload() {
super();
// TODO Auto-generated constructor stub
}

/
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

}

/
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String connectionURL = "jdbc:mysql://localhost/filestore";
Connection con = null;
Statement stmt= null;
ResultSet res = null;
response.setContentType("application/octet-stream");

try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(connectionURL, "root", "");
stmt=con.createStatement();
res =stmt.executeQuery("select * from save_file where id='15'");
if (res .next()) {
Blob File=res .getBlob("file");
InputStream x=File.getBinaryStream();
int size=x.available();
ServletOutputStream Fileout = response.getOutputStream();
FileInputStream f = new FileInputStream("");
int nextByte = f.read();

while ( nextByte != -1 ) {
out.write(nextByte);
nextByte = f.read();
}
out.flush();
}
}




catch(Exception e){
System.out.println("Exception :"+e);
}
finally{
try{
stmt.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
}
 
Code:
InputStream x=File.getBinaryStream();
int size=x.available();
ServletOutputStream Fileout = response.getOutputStream();
FileInputStream f = new FileInputStream("");
int nextByte = f.read();

while ( nextByte != -1 ) {
out.write(nextByte);
nextByte = f.read();
}
replaced by
Code:
FileInputStream x=File.getBinaryStream();
int size=x.available();
ServletOutputStream Fileout = response.getOutputStream();
int nextByte = x.read();
while ( nextByte != -1 ) {
out.write(nextByte);
nextByte = x.read();
}

InputStream is an abstract class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top