0

サーブレットを介してExcelドキュメントを生成しています。応答をクライアント(IE8)に返送すると、[開く/保存]ダイアログが表示されますが、ユーザーはアクションを実行する前に選択肢を2回クリックする必要があります。これはFirefoxでは起こりません。なぜこれが起こっているのか分かりません。以下は、適切なストリームを作成する関連コードです。

resultExcelXMLが含まれています。

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=TestFile.xls");

InputStream in = new ByteArrayInputStream(result.toString().getBytes("UTF-8"));
ServletOutputStream out = response.getOutputStream();

try
{
    byte[] outputByte = new byte[4096];

    while(in.read(outputByte, 0, 4096) != -1)
        out.write(outputByte, 0, 4096);
}
finally
{
    in.close();
    out.flush();
    out.close();
}

編集 オプションをクリックする前に5秒以上待つことは問題なく機能することに気づきました。オプションをすぐにクリックすると、2回しか尋ねられないようです。

4

1 に答える 1

1

このコードは、私のアプリケーションのすべてのタイプのファイルでうまく機能します

  InputStream in = blob.getBinaryStream();
  // Output the blob to the HttpServletResponse

  String codedfilename = "";
  //this code resolves the issue with the encoding of the downloaded filename
  String agent = request.getHeader("USER-AGENT");
  if (null != agent && -1 != agent.indexOf("MSIE"))
  {
    codedfilename = URLEncoder.encode(/*here goes the filename*/, "UTF8");
    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition","attachment;filename=" + codedfilename);
  }
  else if (null != agent && -1 != agent.indexOf("Mozilla"))
  {
    response.setCharacterEncoding("UTF-8");
    //It does not seem to make a difference whether Q or B is chosen
    codedfilename = MimeUtility.encodeText(rset.getString("FILE_NAME"), "UTF8", "B");
    response.setContentType("application/force-download");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + codedfilename + "\"");
  }

  BufferedOutputStream out =
      new BufferedOutputStream(response.getOutputStream());
  byte by[] = new byte[32768];
  int index = in.read(by, 0, 32768);
  while (index != -1) {
      out.write(by, 0, index);
      index = in.read(by, 0, 32768);
  }
  out.flush();

それを試して、私たちに知らせてください

于 2012-07-03T14:43:58.113 に答える