如何在浏览器中打开pdf文件

How to open pdf file in browser

我正在尝试打开一个从存储库导出的 pdf 文件。这是我正在使用的代码:

ConnectionManager con = new ConnectionManager();
String id = request.getParameter("uname"); 
String objname = request.getParameter("pass");
Properties prop = new Properties();
//ResourceBundle resource = ResourceBundle.getBundle("query");
//prop.load(getClass().getResourceAsStream("query.properties"));
String uname = "DmAdmin";
String pass = "<pass>";
String docbase = "QDocs";
String ext = new String();
IDfSession ssn = con.getSession(uname, pass, docbase);
sysObj = (IDfSysObject)ssn.getObject((IDfId)new DfId(id));
//ByteArrayInputStream buf = sysObj.getContent();
//sysObj.getFile("C:\Users\rsaha04\Downloads\"+objname+".pdf");
String path = "C:\Users\rsaha04\Downloads\";
String filename = path + sysObj.getObjectName().toString();

IDfCollection coll = sysObj.getRenditions(null);
if (coll != null)
{
    while (coll.next())
    {
        String format = coll.getString("full_format");
        {
            if (format.equalsIgnoreCase("pdf"))
            {
                ext = "pdf";
                System.out.println("extension set: "+ext);
            }
        }
    }
    
    filename = filename+"."+ext;
    sysObj.getFileEx(filename, ext, 0, false);
}
con.closeConnection(ssn);
//Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+filename);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename='"+filename+"'");

我可以在 adobe acrobat 中打开 pdf 文件 reader 但是浏览器无法打开并出现此错误。

请帮助我了解我哪里出错了。

您需要您的服务器以 pdf 文件响应。您设置了响应 headers,但您的代码从未将 pdf 数据写入响应。

使用

response.write(bytesFromPdfFile)