【发布时间】:2011-06-01 07:31:47
【问题描述】:
我有一个扩展 RemoteServiceServlet 的类。这个类有几个方法,在一个方法中,我使用 getThreadLocalResponse() 来获取当前调用的 HttpServletResponse,然后在响应中写入一个 File。代码如下:
File aFile = new File("c://test.txt");
int iBufferSize = 1000;
int iLength = 0;
HttpServletResponse resp = getThreadLocalResponse();
ServletOutputStream op = resp.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType(aFile.getName());
resp.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
resp.setContentLength((int) aFile.length());
resp.setHeader("Content-Disposition", "attachment; filename=\"" + aFile.getName() + "\"");
byte[] xbuf = new byte[iBufferSize];
DataInputStream in = new DataInputStream(new FileInputStream(aFile));
while ((in != null) && ((iLength = in.read(xbuf)) != -1))
{
op.write(xbuf, 0, iLength);
}
in.close();
op.flush();
op.close();
但是,总是会发生错误。调试后发现写响应的时候抛出了异常。
我没有重写doGet和doPost,因为还有一些其他的方法,我不希望每个对类的请求都可以调用这段代码。
但是如果我在这个类中创建一个单独的 Servlet 或覆盖 doGet 或 doPost,它就可以正常工作。
有人知道为什么吗?当我们使用 getThreadLocalResponse() 时,GWT RemoteServiceServlet 是否支持在响应中写入 Stream ?
谢谢!
【问题讨论】: