我目前正在处理的程序允许用户浏览服务器上的文件然后下载它们。为了下载,我写了一个JSP来将内容上传到客户端。它传递文件的名称,然后发回其内容。我现在的代码对文本文件非常有用,但是当我尝试下载然后打开一个word文档时,我理解一个二进制文件,我得到一些文本,一些奇怪的方块,以及来自MS Word的错误信息。我不确定为什么它错误地读取二进制数据。任何帮助将不胜感激。
<%@ page import="java.io.*" %>
<%
final File APP_ROOT = new File(com.mdi.core.config.ProcessConfiguration.getInstance().getStringValue("APP_ROOT"));
File f = new File(request.getParameter("path"));
File parent = f;
boolean valid = false;
while(!valid && (parent = parent.getParentFile()) != null)
{
valid = parent.equals(APP_ROOT);
}
if(!valid)
{
response.sendError(403, "You can not download this file!");
return;
}
response.addHeader("Content-Disposition","attachment; filename="+f.getName());
response.setContentLength((int)f.length());
OutputStream os = response.getOutputStream();
response.setContentType(java.net.URLConnection.guessContentTypeFromName(f.getName()));
InputStream fin = null;
try
{
fin = new FileInputStream(f);
byte[] buff = new byte[512];
int read;
while((read = fin.read(buff)) != -1)
os.write(buff, 0, read);
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if(fin != null) fin.close();
}
%>
此外,我在标准输出中收到此错误。
java.net.ProtocolException: Exceeded stated content-length of: '40220' bytes
at weblogic.servlet.internal.ServletOutputStreamImpl.checkCL(ServletOutputStreamImpl.java:200)
at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:146)
at jsp_servlet._mdi._html._file_upload.__default_file_upload._jspService(__default_file_upload.java:101)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at com.mdi.core.utils.http.SecurityFilter.doFilter(SecurityFilter.java:154)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at com.mdi.core.utils.http.ResponseHeaderFilter.doFilter(ResponseHeaderFilter.java:69)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
<Jun 27, 2014 10:46:07 AM EDT> <Error> <HTTP> <BEA-101083> <Connection failure.
java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '39942' bytes instead of stated: '40220' bytes.
at weblogic.servlet.internal.ServletOutputStreamImpl.ensureContentLength(ServletOutputStreamImpl.java:446)
at weblogic.servlet.internal.ServletResponseImpl.ensureContentLength(ServletResponseImpl.java:1432)
at weblogic.servlet.internal.ServletResponseImpl.send(ServletResponseImpl.java:1511)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1462)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
Truncated. see log file for complete stacktrace
>
<Jun 27, 2014 10:46:07 AM EDT> <Error> <HTTP> <BEA-101104> <Servlet execution in servlet context "ServletContext@1188950474[app:Trade module:TA15 path:/TA15 spec-version:null]" failed, java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '39942' bytes instead of stated: '40220' bytes..
java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '39942' bytes instead of stated: '40220' bytes.
at weblogic.servlet.internal.ServletOutputStreamImpl.ensureContentLength(ServletOutputStreamImpl.java:446)
at weblogic.servlet.internal.ServletResponseImpl.ensureContentLength(ServletResponseImpl.java:1432)
at weblogic.servlet.internal.ServletResponseImpl.send(ServletResponseImpl.java:1511)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1462)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
Truncated. see log file for complete stacktrace
答案 0 :(得分:1)
非常重要的是删除JSP自己的空白输出:
<%@ page contentType="application/octet" import="java.io.*" %><%
%>
没有空格,最后一行没有换行符。 提示:使用servlet。
不知怎的,os.flush()
仍然需要完成。
@BoristheSpide评论后:忘记冲洗。我认为这些空间引起了一些问题,将内容类型添加为二进制数据。