Vaadin请求下载pdf返回空指针异常

时间:2016-06-28 20:26:46

标签: pdf download request itext vaadin

我正在尝试创建然后在我的Vaadin应用程序中下载pdf文件(使用vaadin-archetype-application生成),但不知怎的,我无法弄清楚它为什么不工作以及为什么我会得到它例外 以下是我如何构建pdf文件(使用iText):

String fileName = "file.pdf";
String relativePath = VaadinServlet.getCurrent().getServletContext().getRealPath("");

    try {
        Document document = new Document(PageSize.LETTER.rotate());

        PdfWriter.getInstance(document, new FileOutputStream(relativePath + "\\" + fileName));
        document.open();
        table = new PdfPTable(6);
        addMetaData(document);
        addTitlePage(document);
        addContent(document, table);
        System.out.println("dupa add content la scrierea documentului pdf");
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

以下是我尝试下载请求的方法:

if(_ui!=null){
    getUI().getPage().open("http://localhost:8080/project/download?ext=.pdf", "_blank", false);
}

这是我得到空指针异常的行 我用if子句包围它,因为起初我认为_ui参数为null

这是我必须处理请求的代码:

private void registerRequestHandler() {
    // A request handler for generating some content
    VaadinSession.getCurrent().addRequestHandler(new RequestHandler() {
        private static final long serialVersionUID = 1L;

        @Override
        public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
                throws IOException {
            System.out.println("req: "+request.getPathInfo());         //not shown in console
            if ("/download".equals(request.getPathInfo())) {

                // reads input file from an absolute path
                String extension = request.getParameter("ext");
                System.out.println("extension: "+extension);
                String filePath = VaadinServlet.getCurrent().getServletContext().getRealPath("") + "\\file"
                        + extension;
                File downloadFile = new File(filePath);
                FileInputStream inStream = new FileInputStream(downloadFile);

                // if you want to use a relative path to context root:
                System.out.println("filePath = " + filePath);

                // obtains ServletContext
                ServletContext context = VaadinServlet.getCurrent().getServletContext();

                // gets MIME type of the file
                String mimeType = context.getMimeType(filePath);
                if (mimeType == null) {
                    // set to binary type if MIME mapping not found
                    mimeType = "application/octet-stream";
                }
                System.out.println("MIME type: " + mimeType);

                // modifies response
                response.setContentType(mimeType);
                response.setContentLength((int) downloadFile.length());

                // forces download
                String headerKey = "Content-Disposition";
                String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
                response.setHeader(headerKey, headerValue);

                // obtains response's output stream
                OutputStream outStream = response.getOutputStream();

                byte[] buffer = new byte[4096];
                int bytesRead = -1;

                while ((bytesRead = inStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, bytesRead);
                }

                inStream.close();
                outStream.close();

                return true; // We wrote a response
            } else
                return false; // No response was written
        }
    });
}

这部分代码在我的MyUi类中。没有打印出system.out.printlns。 任何帮助/想法/建议深表感谢。

0 个答案:

没有答案