java 浏览器 pdf_我无法通过Java在浏览器中打开.pdf

我正在尝试打开在浏览器中使用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);

}

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值