How to use GWT when downloading Files with a Servlet?

Posted by molleman on Stack Overflow See other posts from Stack Overflow or by molleman
Published on 2010-05-30T00:49:27Z Indexed on 2010/05/30 0:52 UTC
Read the original article Hit count: 630

Filed under:
|
|
|

Hello Guys

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about gwt