| Author |
Download file from Mysql database
|
Puk Akhimien
Greenhorn
Joined: Apr 25, 2010
Posts: 3
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26496
|
|
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?
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Puk Akhimien
Greenhorn
Joined: Apr 25, 2010
Posts: 3
|
|
|
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
Joined: Jan 10, 2002
Posts: 56543
|
|
|
What have you done so far to debug this? is the servlet being invoked? is the response being sent?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Puk Akhimien
Greenhorn
Joined: Apr 25, 2010
Posts: 3
|
|
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
Joined: Jan 10, 2002
Posts: 56543
|
|
|
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
Joined: Jan 10, 2002
Posts: 56543
|
|
|
Ans you still haven't answered my questions.
|
 |
 |
|
|
subject: Download file from Mysql database
|
|
|