Java – how to use GWT when downloading files using servlet?

I'm creating a simple project that allows me to upload and download files using GWT I can't download files from my server

For uploading files, I used http://code.google.com/p/gwtupload/ , then follow the instructions My files are stored in the website container outside the server (on the hard disk),

Now, when it comes to downloading files, I want the user to press a download button and any currently selected items will be downloaded I really don't know what to do

I know I need a servlet to download

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,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,length);
    }
    in.close();
    out.flush();

}
}

For now, I'll pass a filename string to retrieve the file for testing

Now that I've lost what to do on the client, 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);


}


}

The above is a simple widget that will provide a panel that allows you to download files based on filename

As mentioned above, I don't know what to do to download the file

Can anyone point me in the right direction?

Solution

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>