【发布时间】:2021-03-15 15:12:56
【问题描述】:
我正在尝试打开已从存储库中导出的 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 reader 中打开 pdf 文件,但浏览器失败并出现此错误。
请帮助我了解我在哪里出错了。
【问题讨论】:
-
如何将pdf文件写入响应?你漏掉了重要的部分
-
@f1sh 对不起,这是我第一次编写这样的代码,所以我不确定如何实际执行。如果你能在这里指导我,那就太好了。
-
您需要您的服务器以 pdf 文件响应。您设置了响应标头,但您的代码从不将 pdf 数据写入响应。
-
哦!人。太感谢了。我现在已经修好了。如果你能写下你的评论作为答案,我会接受。
-
好的,我写了这个作为答案!
标签: java pdf browser inline content-disposition