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

查看:16
本文介绍了使用 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();

}
}

目前我将只传递一个文件名字符串来检索文件进行测试

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);


}


}

上面是一个简单的小部件,它将提供一个面板,允许根据文件名下载文件

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?

推荐答案

在客户端,只需创建一个常规的 标签.如果要动态创建它,可以使用 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天全站免登陆