• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Download file from Mysql database

 
Puk Akhimien
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34218
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puk Akhimien wrote: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.

What does "haven't done it right" mean? Please TellTheDetails. Did you get an error message?
 
Puk Akhimien
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run it nothing happens. I am supposed to get a dialogue box that asks me if i want to download or open the file. This occurs when a user clicks on the download link.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you done so far to debug this? is the servlet being invoked? is the response being sent?
 
Puk Akhimien
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is the code that i have so far im trying to combine the two so that when a user clicks a down load link they will get a dialogue box that asks if they want to open or save the file

[code = java ]

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=1");
if (res.next()) {
Blob file=res.getBlob("file");
InputStream x=file.getBinaryStream();
int size=x.available();
byte b[]= new byte[size];
x.read(b);
}
}catch(Exception e){
System.out.println("Exception :"+e);
}
finally{
try{
stmt.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}


private void downloadFile(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {

// Set the content type to the byte-stream type (since we don't know it)
// see http://en.wikipedia.org/wiki/MIME_type for meaning of MIME type
response.setContentType("application/octet-stream");

// Set header: allows us to specify the download filename to the browser
// see http://en.wikipedia.org/wiki/List_of_HTTP_headers for header types
response.setHeader("Content-Disposition", "attachment; filename=" + new File(fileName).getName());

ServletOutputStream out = response.getOutputStream();

// Now copy requested file to the response output stream
// prefix filename with location of our files
FileInputStream f = new FileInputStream(fileName);

int nextByte = f.read();
// note: there is a faster way to do this using alternative read() methods

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

}
}

[/code]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags and please make sure that your code is properly indented. Otherwise, few people will take the time to look at it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ans you still haven't answered my questions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic