[英]Struts pdf view showing null pointer exception in response object
我用 iTextpdf 創建了一個 pdf 但是在設置 response.setContentType("application/pdf"); 獲取 null 指針異常
來自 jsp
<form action="pdfReport">
<input formtarget="_blank" type="image" src="${pageContext.request.contextPath}/images/pdf-icon.png" id="pdf" ">
</form>
struts.xml
<action name="pdfReport" class="com.tapal.action.CreateReceiptAction" method="generatePdfReport">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">inline;filename="eTapalReport.pdf"</param>
<param name="bufferSize">1024</param>
</result>
</action>
動作 class
public class CreateReceiptAction extends BaseAction implements ServletRequestAware{
// codes
public String generatePdfReport() {
try {
//generated pdf
String filePathToBeServed = pdfPath+"/"+pdfname;;
File fileToDownload = new File(filePathToBeServed);
InputStream inputStream = new FileInputStream(fileToDownload);
response.setContentType("application/pdf"); // here nullPointer exception
response.addHeader("content-disposition", "inline; filename=eTapalReport.pdf");
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
inputStream.close();
fOut.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
卡得很厲害。 任何幫助,將不勝感激。 謝謝你。
因為它來自同一頁面初始化響應,重置和關閉對我有用
response= ServletActionContext.getResponse();
String filePathToBeServed = pdfPath+"/"+pdfname;;
File fileToDownload = new File(filePathToBeServed);
InputStream inputStream = new FileInputStream(fileToDownload);
response.setContentType("application/pdf");
response.addHeader("content-disposition", "inline; filename=eTapalReport.pdf");
response.reset();
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
inputStream.close();
response.getOutputStream().close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.