通过使用以下代码,PDF文件正在下载,但是当我尝试打开该PDF文件时,它显示错误:不是pdf或已损坏 这是我的xhtmlcode: RichEditor.xhtml :
<ui:composition template="/WEB-INF/templates/main-template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:define name="header">
Add your header here or delete to use the default
</ui:define>
<ui:define name="content">
<h:form id="form">
<p:editor id="editor" value="#{editorData.data}" width="600"/>
<h:commandButton value="submit" action="#{editorData.save}"></h:commandButton>
</h:form>
<a href="../Editordatapdf" ><img src="D://excel.png" alt="Download" /></a>
</ui:define>
<ui:define name="footer">
Add your footer here or delete to use the default
</ui:define>
</ui:composition>
这是我的以下Managed bean: 的 EditorData.java:
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
public class EditorData {
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public EditorData()
{
}
public String save()
{
HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
session.setAttribute("userdata", data);
return"Success";
}
}
这是我的Servlet页面: 在这里,我将保存用户在我的数据库中提供的数据。而且我想用用户给定的样式(如:字体,颜色等)将数据存储到pdf中。
Editordatapdf.java
import java.io.IOException;
import javax.faces.context.FacesContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import com.primeki.development.utils.DBConnection;
public class Editordatapdf extends HttpServlet {
private static final long serialVersionUID = 1L;
DBConnection dbconnection;
Connection connection;
PreparedStatement ps;
Statement state;
ResultSet rs;
public Editordatapdf() {
super();
// TODO Auto-generated constructor stub
}
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("This is the Servlet Page");
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
javax.servlet.http.HttpSession session=request.getSession();
System.out.println("This is the servlet page data"+ session.getAttribute("userdata"));
dbconnection = new DBConnection();
System.out.println("This is the Servlet Page");
connection = dbconnection.getConnection();
System.out.println("Connection Successfully");
String SqlQuery = " INSERT into richtext (data) VALUES" + "(?)";
System.out.println(SqlQuery);
ps=connection.prepareStatement(SqlQuery);
String data = (String) session.getAttribute("userdata");
ps.setString(1,data);
ps.executeUpdate();
System.out.println("customerstatement is executed Completed..!!!");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=Data.pdf");
PrintWriter out = response.getWriter();
out.println(session.getAttribute("userdata"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我在尝试打开pdf文件时收到错误消息。请帮我解决这个问题
谢谢....