使用GWT下载动态文件

使用GWT下载动态文件

本文介绍了使用GWT下载动态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户输入数据的GWT页面(开始日期,结束日期等),然后这些数据通过RPC调用进入服务器。在服务器上,我想用POI生成Excel报告,并让用户将该文件保存在本地计算机上。



这是我的测试代码,用于将文件传回客户端,但出于某种原因,我认为它不知道如何在使用RPC时将文件流式传输到客户端:

  public class ReportsServiceImpl扩展RemoteServiceServlet实现ReportsService {
public String myMethod(String s){

File f = new File(/ excelTestFile.xls);

String filename = f.getName();

int length = 0;

尝试{
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 ((in!= null)&&((length = in.read(1024);
DataInputStream in = new DataInputStream(new FileInputStream(f));

bbuf))!= -1)){
op.write(bbuf,0,length);
}

in.close();
op。 flush();
op.close();

}
catch(Exception ex){
ex.printStackTrace();
}

returnServer says:+ filename;
}
}

我在互联网上的某个地方读过,你不能用RPC做文件流,我必须使用Servlet。是否有任何有关如何使用Servlet以及如何从 ReportsServiceImpl调用该servlet的示例。我真的需要制作一个servlet吗?或者有可能使用RPC进行流式处理?

解决方案

您必须制作一个常规的Servlet,而您不能流来自 ReportsServiceImpl 的二进制数据。此外,无法从ReportsServiceImpl调用servlet - 您的客户端代码必须直接调用servlet。



在客户端,您必须创建一个正常的锚链接与通过查询字符串传递的参数。类似于< a href =http://myserver.com/myservlet?parm1 = value1& ..< / a>



在服务器端,将您的代码移动到一个标准的Servlet,它不会从 RemoteServiceServlet 继承。从请求对象读取参数,创建excel并将其发送回客户端。浏览器将自动弹出文件下载对话框。


I have a GWT page where user enter data (start date, end date, etc.), then this data goes to the server via RPC call. On the server I want to generate Excel report with POI and let user save that file on their local machine.

This is my test code to stream file back to the client but for some reason I think it does not know how to stream file to the client when I'm using 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;
    }
}

I've read somewhere on internet that you can't do file stream with RPC and I have to use Servlet for that. Is there any example of how to use Servlet and how to call that servlet from ReportsServiceImpl. Do I really need to make a servlet or it is possible to stream it back with my RPC?

解决方案

You have to make a regular Servlet, you cannot stream binary data from ReportsServiceImpl. Also, there is no way to call the servlet from ReportsServiceImpl - your client code has to directly invoke the servlet.

On the client side, you'd have to create a normal anchor link with the parameters passed via the query string. Something like <a href="http://myserver.com/myservlet?parm1=value1&.."</a>.

On the server side, move your code to a standard Servlet, one that does NOT inherit from RemoteServiceServlet. Read the parameters from the request object, create the excel and send it back to the client. The browser will automatically popup the file download dialog box.

这篇关于使用GWT下载动态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:52