【问题标题】:What triggers the servletResponse for it to send the data/file to the client是什么触发 servletResponse 将数据/文件发送到客户端
【发布时间】:2016-08-30 16:54:23
【问题描述】:

我正在开发一个创建报告并将其发送给客户的应用程序。我使用了以下内容:XDocReport、Grails。

这是我的代码:

   def OutputStream outputStream = response.getOutputStream();

    try {

        def InputStream inn = new FileInputStream(new File(invoiceExporterService.getTemplatePath(templateName)));
        def IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inn, TemplateEngineKind.Freemarker);
        def FieldsMetadata metadata = report.createFieldsMetadata();
        metadata.load("invoiceDetails", CustomerInvoiceDetail.class, true);

        IContext context = report.createContext();
        context = invoiceExporterService.prepareDataForExport(context, id);
        report.process(context, outputStream);


    } catch (IOException io) {

        println "**" + io.properties + "**"
        return false;
    } catch (XDocReportException xd) {

        println "**" + xd.properties + "**"
        return false;
    } catch (Exception e) {

        println  "**" + e.properties + "**"
        return false;
    }


    def String fileName = DataFormatUtil.formatFileName(invoiceExporterService.getOutputFilename(), "docx");
    response.setContentType("application/octet-stream");

// response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); println 文件名 response.setHeader("Content-Disposition", "Attachment;Filename=${fileName}"); outputStream.flush();

现在的问题是,它没有将文件发送给客户端。它正在发送不是我需要的文件的其他内容。

我的问题是:

1) 给定上面的代码,有什么问题?错误的原因是什么? 2) 需要执行什么触发器或代码行才能将文件发送到客户端?

【问题讨论】:

    标签: servlets grails


    【解决方案1】:

    奇怪的是,我可以通过在此行之前设置 content-type 和 disposition type 来使我的代码工作:

    def OutputStream outputStream = response.getOutputStream();
    

    但是,问题仍然存在。 servletResponse 向客户端发送数据需要执行的触发器/代码行是什么?

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      相关资源
      最近更新 更多