当我执行这个jsp文件时,我能够以pdf格式下载该文件。但是当我试图打开它时,显示的错误是不支持的格式。但是,如果我尝试在记事本中打开下载的pdf文件,我会得到我想要的数据。但我应该以解密的形式获取它。所以请帮我打开pdf文件查看数据..
<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>
答案 0 :(得分:3)
主要是出了什么问题,你在PDF输出周围也在输出HTML。确保您以<%
开头,并以%>
结尾,而不是最终换行符;这是好的风格,即使response.reset()
可能在这里发挥作用。
此外content.getBytes("Cp1252")
(Windows Latin-1,ISO-8859-1的扩展名)。否则,平台编码是默认的。
似乎你输出字符串Content
而不是pdf(文件名)。
File file = request.getServletContext().getRealPath("/pdfsdir/" + filename);
FileInputStream byteStream = new FileInputStream(file);
由于PDF是二进制的,因此不需要setCharacterEncoding。 但是,对于文件,您可以设置Content-Length标头。
要填写个性化数据,需要更多。
早期做:
File file = request.getServletContext().getRealPath("/pdfsdir/" + filename);
if (!file.canRead()) {
// Log this with filename...
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
long fileLength = file.length();
if (fileLength > Integer.MAX_VALUE) { // Too large
// Log this with filename...
response.sendError(HttpServletResponse.SC_CONFLICT);
return;
}
int length = (int) fileLength;
后:
response.setHeader("Content-disposition",
"attachment; filename=" + filename);
DO
response.setContentLength(length);
FileInputStream byteStream = new FileInputStream(file);
BufferedInputStream bufStream = new BufferedInputStream(
byteStream);
ServletOutputStream responseOutputStream = response
.getOutputStream();
final int BLOCK_SIZE = Math.min(16*1024, length);
byte[] data = new byte[BLOCK_SIZE];
while (length > 0) {
int nread = bufStream.read(data, 0, Math.max(length, block.length));
if (nread <= 0) {
// Something fishy happened, upload of other smaller version.
break;
}
length -= nread;
responseOutputStream.write(data, 0, nread);
}
bufStream.close();