繁体   English   中英

Struts pdf 视图显示 null 指针异常响应 object

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM