0

I'm generating an Excel document via Servlet. When I send the response back to the client (IE8), the "Open/Save" dialog pops up but requires users to click a choice twice before taking action. This doesn't happen in Firefox. I have no idea why this is occurring. Below is the relevant code that creates the appropriate streams.

result contains the Excel XML.

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();
}

EDIT I have noticed that waiting 5+ seconds before clicking an option works just fine. It seems to only ask twice when immediately clicking an option.

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 回答