使用Servlet下载文件时如何使用GWT? [英] How to use GWT when downloading Files with a Servlet?

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

问题描述

我正在创建一个简单的项目,让我可以使用gwt上传和下载文件。我无法下载我的服务器上的文件。

I am creating a simple project that will allow me to upload and download files using gwt. i am having trouble with the downloading of files that are on my server.

对于上传文件,我使用了 http:/ /code.google.com/p/gwtupload/ ,然后按照说明进行操作。我的文件存储在网站容器(硬盘驱动器)之外的服务器上,

For the file upload i used http://code.google.com/p/gwtupload/ and followed the instructions there. My file is stored on the server outside of the website container(on the hard drive),

现在,当涉及到文件的下载时,我希望用户按下一个下载按钮,当前选择的项目将被下载。我不知道这将如何完成

Now when it comes to the downloading of a file, i want a user to press a download button and whatever item is currently selected will download. i dont really know how this will be done

我知道我需要一个下载servlet

i know i need a download servlet

public class DownloadAttachmentServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    super.doGet(req, resp);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    String fileName = (String) req.getSession().getAttribute("fileName");

    YFUser user = (YFUser) req.getSession().getAttribute(TestServiceImpl.SESSION_USER);
    if (user == null)
        throw new ServletException("Invalid Session");

    InputStream in = null;
    OutputStream out = resp.getOutputStream();

    FileInputStream fIn = new FileInputStream(fileName);
    byte[] buffer = new byte[4096];
    int length;
    while ((length = in.read(buffer)) > 0){
        out.write(buffer, 0, length);
    }
    in.close();
    out.flush();

}
}

一个fileName字符串来检索用于测试的文件

for the moment i will just pass a fileName string to retrieve the file for testing

现在我失去了在客户端做什么,我有一个简单的

now i am lost at what to do on the client side, i have a simple

public class DownloadFilePanel extends Composite {


public DownloadFilePanel(final YFUser user , final String fileName){

    final Element downloadIframe = RootPanel.get("__download").getElement();

    VerticalPanel content = new VerticalPanel();
    content.add(new Label("Download For this File : " + fileName));

    Button button = new Button("Download");
    button.addClickHandler(new ClickHandler(){
        @Override
        public void onClick(ClickEvent event) {
            // i do not know what to do here
    });
            content.add(button);
    initWidget(content);


}


}


$ b上面的$ b

是一个简单的窗口小部件,它将提供一个面板,它将允许基于fileName

above is a simple widget that will supply a panel that will allow for the download of a file based on a fileName

下载文件,如上所示,我不知道该怎么做才能下载文件

as you can see above, i do not know what to do to be able to download the file

有没有可以指出我正确的方向?

is there any one that can point me in the right direction?

推荐答案

在客户端,只需创建一个常规的< a href =path / to / servlet> 标签。如果要动态创建,您可以使用 Anchor 类。当用户点击链接时,浏览器将自动下载文件。

On the client side, just create a regular <a href="path/to/servlet"> tag. You can use the Anchor class if you want to dynamically create it. When the user clicks the link, the browser will automatically download the file.

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

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