gpt4 book ai didi

java - 文件过早结束 SAXParseException HttpServletRequest

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

运行 xml-rcp 服务器和客户端。尝试将请求记录到服务器。但出现文件过早结束错误。谁能看出我做错了什么吗?

 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

ServletInputStream sis = req.getInputStream();
int len = req.getContentLength();
byte[] bytes = new byte[len];

int offset = 0;
int numRead = 0;
while ((offset < bytes.length) && (numRead=sis.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

if (offset < bytes.length) {
throw new IOException("Could not complete reading request body");
}

sis.close();

FileOutputStream f = new FileOutputStream(new File("C:\\Users\\test\\file.log"), true);
f.write(bytes);
f.flush();
f.close();

super.doPost(req, res);
}

[ fatal error ]:1:1:文件过早结束。

redstone.xmlrpc.XmlRpcException:解析期间发生问题 在 redstone.xmlrpc.XmlRpcParser.parse(来源未知) 在 redstone.xmlrpc.XmlRpcDispatcher.dispatch(来源未知) 在 redstone.xmlrpc.XmlRpcServer.execute(来源未知) 在 redstone.xmlrpc.XmlRpcServlet.doPost(来源未知) 在 com.devices.test.servlet.MyXmlRpcServlet.doPost(MyXmlRpcServlet.java:93) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:820) 在 org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:565) 在 org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:479) 在 org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1031) 在 org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406) 在 org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:965) 在 org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) 在 org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111) 在 org.eclipse.jetty.server.Server.handle(Server.java:349) 在 org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:452) 在 org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:894) 在 org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:948) 在 org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:857) 在 org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) 在 org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77) 在 org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609) 在 org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:45) 在 org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599) 在 org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534) 在 java.lang.Thread.run(Thread.java:722)引起的:org.xml.sax.SAXParseException;行号:1;列数:1;文件提前结束 在com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1236) ... 26 更多

解决方案:

 import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
CustomHttpServletRequestWrapper wrapper = new CustomHttpServletRequestWrapper((HttpServletRequest) req);

ServletInputStream sis = wrapper.getInputStream();
String StringFromInputStream = IOUtils.toString(sis, "UTF-8");
LOG.info( "request:");
LOG.info( StringFromInputStream );
super.doPost(wrapper, res);
}


public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper
{

private final String body;
private final Logger LOG = LoggerFactory.getLogger( CustomHttpServletRequestWrapper.class );

public CustomHttpServletRequestWrapper( final HttpServletRequest request )
{
super( request );

final StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;

try
{
final InputStream inputStream = request.getInputStream();

if (inputStream != null)
{
bufferedReader = new BufferedReader( new InputStreamReader( inputStream ) );

final char[] charBuffer = new char[128];
int bytesRead = -1;

while (( bytesRead = bufferedReader.read( charBuffer ) ) > 0)
{
stringBuilder.append( charBuffer, 0, bytesRead );
}
}
else
{
stringBuilder.append( "" );
}
}
catch (final IOException ex)
{
LOG.error( "Error reading the request body..." );
}
finally
{
if (bufferedReader != null)
{
try
{
bufferedReader.close();
}
catch (final IOException ex)
{
LOG.error( "Error closing bufferedReader..." );
}
}
}

body = stringBuilder.toString();
LOG.info( "body:" );
LOG.info( body );
}

@Override
public ServletInputStream getInputStream() throws IOException
{
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( body.getBytes() );

final ServletInputStream inputStream = new ServletInputStream()
{
@Override
public int read() throws IOException
{
return byteArrayInputStream.read();
}
};

return inputStream;
}

}

最佳答案

您已经消耗了随请求发送的所有数据,因此 super.doPost(...) 方法没有任何剩余!

您可以将 HttpServletRequestWrapper 传递给 super.doPost(...)。它应该包装原始请求,但返回一个不同的 InputStream,该输入流由您已在此方法中读取的字节数组支持。另请注意,还需要包装可从请求中获取的读取器 (HttpServletRequest#getReader())。

另请参阅How to log response content from a java web server ,您可以在那里找到有关日志记录另一面的完整代码示例。我认为它可以很容易地满足您的需要。

关于java - 文件过早结束 SAXParseException HttpServletRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19928454/

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