我有一个用户输入数据(开始日期,结束日期等)的GWT页面,然后通过RPC调用将此数据发送到服务器。在服务器上,我想用POI生成Excel报告,并让用户将该文件保存在本地机器上。使用GWT下载动态文件
这是我的测试代码流文件返回给客户端,但由于某种原因,我认为它不知道如何当我使用RPC流文件到客户端:
public class ReportsServiceImpl extends RemoteServiceServlet implements ReportsService {
public String myMethod(String s) {
File f = new File("/excelTestFile.xls");
String filename = f.getName();
int length = 0;
try {
HttpServletResponse resp = getThreadLocalResponse();
ServletOutputStream op = resp.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
resp.setContentType("application/octet-stream");
resp.setContentLength((int) f.length());
resp.setHeader("Content-Disposition", "attachment; filename*=\"utf-8''" + filename + "");
byte[] bbuf = new byte[1024];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
op.write(bbuf, 0, length);
}
in.close();
op.flush();
op.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
return "Server says: " + filename;
}
}
我已经阅读互联网上的某个地方,你不能用RPC做文件流,我必须为此使用Servlet。是否有任何示例如何使用Servlet以及如何从ReportsServiceImpl调用该servlet。我真的需要制作一个servlet,还是有可能使用我的RPC进行流式处理?
请更详细地阐述这个问题。 “它不知道”不是真正的描述。究竟发生了什么?究竟发生了什么? – BalusC 2010-05-12 20:52:48
@sri的答案很有意义。现在轮到我发表一些评论:1)'DataInputStream'超级。只需使用直接的'FileInputStream'。毕竟你*只需要''InputStream'类中定义的'read()'方法。 2)'in!= null'检查也是superflous,因为这是**永不**空(你使用'new'创建了一个新的,永远不能为空)。 3)'Content-Disposition'头文件在'filename'部分看起来不正确。要想了解如何执行* basic *文件服务,您可能会发现[本文](http://balusc.blogspot.com/2007/07/fileservlet.html)有用。祝你好运。 – BalusC 2010-05-12 22:33:01