开发者

How to use GWT when downloading Files with a Servlet?

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.

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

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜