我无法通过Java在浏览器中打开.pdf
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了我无法通过Java在浏览器中打开.pdf,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2030字,纯文字阅读大概需要3分钟。
内容图文
![我无法通过Java在浏览器中打开.pdf](/upload/InfoBanner/zyjiaocheng/696/0507b58b3784459f8afc640685404776.jpg)
我正在尝试打开在浏览器中使用iText库创建的pdf文件,但失败.
这是我用来发送到浏览器的代码
File file = new File(path);
try{
//InputStream stream=blob.getBinaryStream();
InputStream streamEntrada = new FileInputStream(file);
//ServletOutputStream fileOutputStream = response.getOutputStream();
PrintWriter print = response.getWriter();
int ibit = 256;
while ((ibit) >= 0)
{
ibit = streamEntrada.read();
print.write(ibit);
}
response.setContentType("application/text");
response.setHeader("Content-Disposition", "attachment;filename="+name);
response.setHeader("Pragma", "cache");
response.setHeader("Cache-control", "private, max-age=0");
streamEntrada.close();
print.close();
return null;
}
catch(Exception e){
return null;
}
}
我尝试了FileOutputStream,但没有用.我很绝望.
谢谢.
现在,我正在尝试这种方式,但是没有用:
公共类MovilInfoAction扩展DownloadAction {
protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//Here the creation of the PDF
//Storing data
PdfData dPdf = pdf.drawPDF(terminal);
String path = dPdf.getPath();//Path
String name = dPdf.getName()+".pdf";//Pdf′s name
String contentType = "application/pdf";//ContentType
response.setContentType(contentType);
response.setHeader("Content-Disposition","attachment; filename="+name);
response.setHeader("Cache-control", "private, max-age=0");
response.setHeader("Content-Disposition", "inline");
File file = new File(path);
byte[] pdfBytes = es.vodafone.framework.utils.Utils.getBytesFromFile(file);
return new ByteArrayStreamInfo(contentType, pdfBytes);
}
protected class ByteArrayStreamInfo implements StreamInfo {
protected String contentType;
protected byte[] bytes;
public ByteArrayStreamInfo(String contentType, byte[] bytes) {
this.contentType = contentType;
this.bytes = bytes;
}
public String getContentType() {
return contentType;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}
}
}
解决方法:
您可以将mimetype指定为application / text,而应将其指定为application / pdf.
内容总结
以上是互联网集市为您收集整理的我无法通过Java在浏览器中打开.pdf全部内容,希望文章能够帮你解决我无法通过Java在浏览器中打开.pdf所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。