附件的HTTP响应标头内容处置

Dav*_*vis 21 java ajax jboss richfaces attachment

背景

将XML文档写入浏览器的响应流,并使浏览器显示"另存为"对话框.

问题

请考虑以下download()方法:

  HttpServletResponse response = getResponse();

  BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(
      response.getOutputStream() ) );

  String filename = "domain.xml";
  String mimeType = new MimetypesFileTypeMap().getContentType( filename );

  // Prints "application/octet-stream"
  System.out.println( "mimeType: " + mimeType );

  // response.setContentType( "text/xml;charset=UTF-8" );
  response.setContentType( mimeType );
  response.setHeader( "Content-Disposition", "attachment;filename="
      + filename );

  bw.write( getDomainDocument() );
  bw.flush();
  bw.close();
Run Code Online (Sandbox Code Playgroud)

在Firefox中,XML内容显示在浏览器窗口中.在IE 7中,不显示XML内容 - 您必须查看文档源.这两种情况都不是理想的结果.

该网页使用以下代码作为按钮:

    <a4j:commandButton action="#{domainContent.download}" value="Create Domain" reRender="error" />
Run Code Online (Sandbox Code Playgroud)

所生成的XML 下手<?xml version="1.0"?>,而XML内容类似于:

<schema xmlns="http://www.jaspersoft.com/2007/SL/XMLSchema" version="1.0">
  <items>
    <item description="EDT Class Code" descriptionId="" label="EDT Class Code" labelId="" resourceId="as_pay_payrolldeduction.edtclass"/>
  </items>
  <resources>
    <jdbcTable datasourceId="JNDI" id="as_pay_payrolldeduction" tableName="as_pay.payrolldeduction">
      <fieldList>
        <field id="payamount" type="java.math.BigDecimal"/>
      </fieldList>
    </jdbcTable>
  </resources>
</schema>
Run Code Online (Sandbox Code Playgroud)

更新#1

请注意以下代码行:

response.setHeader( "Content-Disposition", "attachment;filename=" + filename );
Run Code Online (Sandbox Code Playgroud)

更新#2

使用<a4j:commandButton ... />是问题; 定期<h:commandButton .../>按预期执行.使用<h:commandBUtton .../>可防止<a4j:outputPanel .../>刷新任何错误消息.

相关的Seam消息.

哑剧类型

以下mime类型不会触发"另存为"对话框:

  • "application/octet-stream"
  • "text/xml"
  • "text/plain"

哪些更改将导致a4j:commandButton触发"另存为"对话框,以便提示用户保存XML文件(如domain.xml)?

谢谢.

小智 11

既不使用内联; 也不依附; 只是用

response.setContentType("text/xml");
response.setHeader( "Content-Disposition", "filename=" + filename );
Run Code Online (Sandbox Code Playgroud)

要么

response.setHeader( "Content-Disposition", "filename=\"" + filename + "\"" );
Run Code Online (Sandbox Code Playgroud)

要么

response.setHeader( "Content-Disposition", "filename=\"" + 
  filename.substring(0, filename.lastIndexOf('.')) + "\"");
Run Code Online (Sandbox Code Playgroud)


Buh*_*ndi 9

尝试将您的内容类型(媒体类型)更改为application/x-download您的内容处置:attachment;filename=" + fileName;

response.setContentType("application/x-download");
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
Run Code Online (Sandbox Code Playgroud)


Dav*_*vis 7

问题

该代码具有以下问题:

  • Ajax call(<a4j:commandButton .../>)不适用于附件.
  • 必须首先创建输出内容.
  • 显示错误消息也不能使用基于Ajax的a4j标记.

  1. 更改<a4j:commandButton .../><h:commandButton .../>.
  2. 更新源代码:
    1. 更改bw.write( getDomainDocument() );bw.write( document );.
    2. 添加String document = getDomainDocument();到第一行try/catch.
  3. <a4j:outputPanel.../>(未显示)更改为<h:messages showDetail="false"/>.

基本上,删除所有与之相关的Ajax工具commandButton.仍然可以显示错误消息并利用RichFaces UI样式.

参考