Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

write pdf files whith servlets

 
bruno martins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i can�t display my pdf file whith this code:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.sql.*;
public class testepdf extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://sb/cmv?user=amd&&password=123";
String n1_ = "";

Connection con1;
Statement sta1;
String sql1 = "SELECT * FROM files";
PrintWriter out = response.getWriter();
response.setContentType("application/pdf");
try {
Class.forName(driver);
con1=DriverManager.getConnection(url);
sta1 = con1.createStatement();
ResultSet rs = sta1.executeQuery(sql1);

response.setHeader("Content-disposition",
"inline; filename=\"Acta.pdf\"");
rs.next();
Blob blob = rs.getBlob(3);
if (blob != null)
{
int iLen = (int)blob.length();
response.setContentLength(iLen);
ByteArrayOutputStream output = new ByteArrayOutputStream(iLen);
output.write(blob.getBytes(1, iLen), 0, iLen);
out.write(output.toString());
}

rs.close();
sta1.close();
con1.close();

}
catch(java.lang.ClassNotFoundException e) {
out.println("ERRO na ClassNotFound: " + e.getMessage());
return;
}
catch(SQLException ex) {
out.println("ERRO na ligacao :" + ex.getMessage());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what actually happens when you try? Exceptions? Errors in log file? Work with us here - my crystal ball is in the shop.
 
bruno martins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no Exceptions,no Errors,but the pdf file that was create don�t show anything
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a known problem with some browsers if you don't set the content-length header.
And you can't fake it - it has to be the true content-length.
 
Timo Hahn
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't use a printWriter. pdf normaly is binary data.
try something like tihs:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic