gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:07:03 27 4
gpt4 key购买 nike

我试图打开我在浏览器中使用 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,但没有用。我很绝望。

谢谢。

现在,我正在尝试这种方式,但它不起作用:

公共(public)类 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 - 我无法通过 Java 在浏览器中打开 .pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5040776/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com