gpt4 book ai didi

java - 使用 JSP 上传 Word 文档

转载 作者:行者123 更新时间:2023-12-01 12:48:28 26 4
gpt4 key购买 nike

我目前正在开发的程序允许用户浏览服务器上的文件,然后下载它们。对于下载,我编写了一个 JSP 将内容上传到客户端。它传递文件的名称,然后发回其内容。我下面的代码目前非常适合文本文件,但是当我尝试下载并打开一个 Word 文档(据我所知是一个二进制文件)时,我收到一些文本、一些奇怪的方 block 和来自 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

最佳答案

非常重要的是去掉JSP自己的空白输出:

<%@ page contentType="application/octet" import="java.io.*" %><%
%>

最后一行没有空格,也没有换行符。提示:使用 servlet。

不知何故,仍然需要完成os.flush()

来自@BoristheSpide 的评论:忘记冲洗。我认为空格引起了一些问题,将内容类型添加为二进制数据。

关于java - 使用 JSP 上传 Word 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24454343/

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