Servlet混合标题和内容,并在输出中写入两次相同的内容?

Ser*_*yan 8 java synchronization servlets

我已经实现了行为不稳定的servlet,有时它会在内容中混合标题并将其写入两次.

有时它返回的文件包含由内容混合的响应标头,如下所示:

Server: Apache-Coyote/1.1
: W/"43-1353687036000"
DatCCoonntenntt--DDiissppoosittiioonn: : atatatacehnmte;n tf;i lfenlaemnea=m20=12201112211127325421_4W1_Wirnkgi_nSgc_Seern.xnlsx
sx
Content-Typ-eT: ype: applaipcatciaoti/on/toctestt-rstare
am
ConCtoententy-pTeype: appalicatcion/oon/octet-setarm
m
CCoonntent-Lnegtht h: 4199

Date: te: FriF,r i2,3  2No vNo2v0 120162: 215:25 :G4M2T 
....
File content bytes ...

And again same header and content
Run Code Online (Sandbox Code Playgroud)

更新 *这种情况发生在Tomcat7*上

我也测试了Tomcat6和Jetty,在这两种情况下都没有注入HTTP-Header来响应内容但是HTTP-Header是错误的并返回错误的文件名,文件内容是正确的文件.我注意到,当返回transfer-encoding被分块时,servlet会发生错误的返回.

当我删除标题内容和第二部分字节时,它是有效文件.可能是同步问题吗?

更新 这里是servlet的完整源代码:

public class ExcelDownloadServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger
            .getLogger (ExcelDownloadServlet.class);


    @Override
    protected void doGet (HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        try
        {
            TransactionId transactionId = getTransactionId (request);
            String fileName =
                    request.getParameter (GlobalConstants.EXCEL_FILE);
            ExcelDownloadType downloadType =
                    ExcelDownloadType
                            .valueOf (request
                                    .getParameter (GlobalConstants.EXCEL_DOWNLOAD_TYPE));
            ActionContextFactory actionContextFactory =
                    ApplicationContext.getContext ()
                            .getActionContextFactory ();
            //suppress warning. HttpServletRequest.getLocales does not support generics
            @SuppressWarnings("unchecked")
            ActionContext actionContext =
                    actionContextFactory.create (request.getSession ()
                            .getId (), Collections.<Locale> list (request
                            .getLocales ()));
            GetExcelDataResponse dataResponse =
                    new GetExcelData (transactionId, fileName, downloadType)
                            .execute (actionContext);
            writeToResponse (response, dataResponse.getFileName (),
                    dataResponse.getData ());
        }
        catch (InvalidSessionException e)
        {
            LOG.error ("Invalid session in Excel download", e);
            throw new ServletException (e);
        }
        catch (ActionException e)
        {
            LOG.error ("Could not download into excel.", e);
            throw new ServletException (e);
        }
    }

    protected TransactionId getTransactionId (HttpServletRequest request)
    {
        return RequestParameterDeserializer.<TransactionId> deserialize (
                request, GlobalConstants.TRANSACTION_ID);
    }

    protected void writeToResponse (HttpServletResponse response,
            String rawFileName, byte[] data) throws IOException
    {
        ServletOutputStream sout = null;
        try
        {            
            response.setContentType ("application/octet-stream");
            response.setContentLength (data.length);
            // removing blanks from the file name, since FF cuts file names
            // otherwise.
            String fileNameWithTime = rawFileName.replaceAll (" ", "_");
            response.setHeader ("Content-Disposition", "attachment; filename="
                    + fileNameWithTime);
            sout = response.getOutputStream ();
            sout.write (data, 0, data.length);
        }
        finally
        {
            if (sout != null)
            {
                sout.close ();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

UPDATE *调用来自GWT应用程序,当生成带有所需参数的servlet的URL并在IFrame中设置,然后servlet调用和文件正在下载.有什么建议吗?*

Car*_*ini 2

很久以前我也遇到过类似的问题。事实证明,关闭 ServletOutputStream 会触发请求流上的意外行为。

Servlet 不应该关闭提供 OutputStream 的容器。另一个问题可能是手动设置内容长度,容器有责任生成正确的值。

总结一下,尝试删除out.close()response.setContentLength()