gpt4 book ai didi

java - Servlet 混合 header 和内容并在输出中写入相同的两次?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:56:05 25 4
gpt4 key购买 nike

我已经实现了行为不稳定的 servlet,有时它会在内容中混合 header 并写入相同的内容两次。

有时它返回的文件包含混合了如下内容的响应 header :

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

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

我也在 Tomcat6 和 Jetty 上进行了测试,在这两种情况下都没有向响应内容注入(inject) HTTP-Header,但是 HTTP-Header 是错误的并返回错误的文件名,文件内容是正确的文件。我注意到 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 ();
}
}
}

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

最佳答案

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

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

总而言之,尝试删除 out.close()response.setContentLength()

关于java - Servlet 混合 header 和内容并在输出中写入相同的两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13647807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com