用户如何在客户端下载文件(Google Web Toolkit) [英] How can a user download a file in client side (Google Web Toolkit)

查看:95
本文介绍了用户如何在客户端下载文件(Google Web Toolkit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GWT(Google Web Toolkit)制作一个网站。
我需要向用户显示一个表,并让用户下载表的内容。



在客户端,用户如何下载一个文件,当他们按下下载按钮?



下载按钮有一个 onClick()监听器。客户端类扩展 Composite



我试图让类扩展 HttpServlet ,但它变得太复杂了。



我已经在这里阅读帖子:


  1. http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/

  2. 如何在使用Servlet下载文件时使用GWT?

但是我仍然不知道如何在客户端向用户提供可下载的文件。

解决方案

您真的需要区分GWT客户端Java代码和服务器端java代码。 / p>

在GWT Java代码的客户端

  String url = GWT.getModuleBaseURL()+downloadService?fileInfo1 =+ fileInfo1; 
Window.open(url,_blank,status = 0,toolbar = 0,menubar = 0,location = 0);

在非gwt Java代码中的服务器端 -



在web.xml中

 < servlet> 
< servlet-name> downloadService< / servlet-name>
< servlet-class> AAA.BBB.CCC.DownloadServlet< / servlet-class>
< / servlet>

< servlet-mapping>
< servlet-name> downloadService< / servlet-name>
< url-pattern> /< gwtmodulename> / downloadService< / url-pattern>
< / servlet-mapping>

在服务器包代码中,一个servlet

  public class DownloadServlet extends HttpServlet {
protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
String fileName = req.getParameter( fileInfo1);

int BUFFER = 1024 * 100;
resp.setContentType(application / octet-stream);
resp.setHeader(Content-Disposition:,attachment; filename =+\+ fileName +\);
ServletOutputStream outputStream = resp.getOutputStream();
resp.setContentLength(Long.valueOf(getfile(fileName).length()).intValue());
resp.setBufferSize(BUFFER);
//您的IO代码到这里创建一个文件并设置为outputStream //

}
}

确保将您的文件内容推送到 ** outputStream **


I'm using GWT(Google Web Toolkit) to make a website. I need to show a table to the user, and let the user download the contents of the table.

On the client side, how can a user download a file when they press the "download" button?

The "Download" button has an onClick() listener. And the client side class extends Composite.

I've tried to make the class extend HttpServlet, but it becomes too complicate.

I already read posts here:

  1. http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/
  2. How to use GWT when downloading Files with a Servlet?

But I still don't know how can I provide downloadable file to the user on the client side.

解决方案

You REALLY need to distinguish between GWT client side java code and server side java code.

On the client side in your GWT Java Code

String url = GWT.getModuleBaseURL() + "downloadService?fileInfo1=" + fileInfo1;
Window.open( url, "_blank", "status=0,toolbar=0,menubar=0,location=0");

On server side in your non-gwt Java code-

In web.xml

<servlet>
    <servlet-name>downloadService</servlet-name>
    <servlet-class>AAA.BBB.CCC.DownloadServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>downloadService</servlet-name>
    <url-pattern>/<gwtmodulename>/downloadService</url-pattern>
</servlet-mapping>

In server package code a servlet

    public class DownloadServlet extends HttpServlet{
    protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
        {
            String fileName = req.getParameter( "fileInfo1" );

            int BUFFER = 1024 * 100;
            resp.setContentType( "application/octet-stream" );
            resp.setHeader( "Content-Disposition:", "attachment;filename=" + "\"" + fileName + "\"" );
            ServletOutputStream outputStream = resp.getOutputStream();
            resp.setContentLength( Long.valueOf( getfile(fileName).length() ).intValue() );
            resp.setBufferSize( BUFFER );
            //Your IO code goes here to create a file and set to outputStream//

        }
    }

Ensure you push your file contents to **outputStream** .

这篇关于用户如何在客户端下载文件(Google Web Toolkit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆