when I am executing this jsp file, I am able to download the file in pdf format.but when i ma trying to open it is displaying an error that unsupported format. But if i try to open the downloaded the pdf file in notepad I am getting the my desired data.But I should get it in a decrypted form.so please help me out to open the pdf file for viewing data..
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% String ID = (String) session.getAttribute("ID");
String connectionURL = "jdbc:oracle:thin:@localhost:1521:XE";
String url = request.getParameter("WEB_URL");
String Content = new String("");
Statement stmt = null;
Connection con = null;
String Content = new String("");
String filename = "data" + ID + ".pdf";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(connectionURL, "temp",
"root");
stmt = con.createStatement();
String qry = "select * from form where id='" + ID + "'";
ResultSet rst = stmt.executeQuery(qry);
if (rst.next()) {
Content = " FirstName: " + rst.getString("FirstName")
+ "\r\n" + " LastName: "
+ rst.getString("LastName") + "\r\n" + " Age: "
+ rst.getString("Age") + "\r\n" + " City: "
+ rst.getString("City") + "\r\n" + " Phone: "
+ rst.getString("Phone") + "\r\n" + " ID: "
+ rst.getString("ID");
}
//out.println(Content);
byte requestBytes[] = Content.getBytes();
response.reset();
response.setContentType("application/pdf");
response.setHeader("cache-control", "no-cache");
response.setHeader("Content-disposition",
"attachment; filename=" + filename);
response.setCharacterEncoding("UTF-8");
ByteArrayInputStream byteStream = new ByteArrayInputStream(
requestBytes);
BufferedInputStream bufStream = new BufferedInputStream(
byteStream);
ServletOutputStream responseOutputStream = response
.getOutputStream();
int data = bufStream.read();
while (data != -1) {
responseOutputStream.write(data);
data = bufStream.read();
}
bufStream.close();
responseOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println(" Exception is:" + e);
}
%>
</body>
</html>
Mainly what is wrong, you are outputting HTML too around the PDF output. Make sure that you start with
<%and end with%>without final newline; that is good style, even ifresponse.reset()might function here.Furthermore
content.getBytes("Cp1252")(Windows Latin-1, extension of your ISO-8859-1). Otherwise the platform encoding is default.And it seems you output the string
Contentnot the pdf (filename).As PDF is binary the setCharacterEncoding is not needed.
For a file you might however set the Content-Length header.
For filling in the personalized data one would need more.
Early on do:
After:
do