javas

javas

View Answers

April 17, 2009 at 5:12 PM

Hi Friend,

Here is the sample code to solve your problem.
If you have saved your word,pdf,image file content into a blob type object in the database then you can try this code as given below:

//Code Starts

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowData extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
ServletOutputStream out = response.getOutputStream();
String connectionURL = "jdbc:mysql://192.168.10.112:3306/test";;
String Content=new String("");
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection(connectionURL,"root","root");
Statement st=con.createStatement();
ResultSet rst= st.executeQuery("select data from users where id=1");
if(rst.next())
{
Content=rst.getString("content");
}
con.close();
}catch(Exception e){
System.out.println("Exception caught"+e.getMessage());
}
byte requestBytes[] = Content.getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(requestBytes);
response.reset();
response.setContentType("application/doc");
response.setHeader("Content-disposition","attachment; filename=" +"Wordfile.doc");
byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) > 0){
out.write(buf, 0, len);
}
bis.close();
response.getOutputStream().flush();
}
}

I hope this would be helpful to you.

Thanks









Related Tutorials/Questions & Answers:
javas - JSP-Servlet

Ads