How can a user download a file in client side (Google Web Toolkit)

前端 未结 4 1884
执笔经年
执笔经年 2020-11-30 12:58

I\'m using GWT(Google Web Toolkit) to make a website. I need to show a table to the user, and let the user download the contents of the table.

On the client side, h

4条回答
  •  情话喂你
    2020-11-30 13:22

    You REALLY need to distinguish between GWT client side java code and server side java code.

    On the client side in your GWT Java Code

    String url = GWT.getModuleBaseURL() + "downloadService?fileInfo1=" + fileInfo1;
    Window.open( url, "_blank", "status=0,toolbar=0,menubar=0,location=0");
    

    On server side in your non-gwt Java code-

    In web.xml

    
        downloadService
        AAA.BBB.CCC.DownloadServlet
    
    
    
        downloadService
        //downloadService
    
    

    In server package code a servlet

        public class DownloadServlet extends HttpServlet{
        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
            {
                String fileName = req.getParameter( "fileInfo1" );
    
                int BUFFER = 1024 * 100;
                resp.setContentType( "application/octet-stream" );
                resp.setHeader( "Content-Disposition:", "attachment;filename=" + "\"" + fileName + "\"" );
                ServletOutputStream outputStream = resp.getOutputStream();
                resp.setContentLength( Long.valueOf( getfile(fileName).length() ).intValue() );
                resp.setBufferSize( BUFFER );
                //Your IO code goes here to create a file and set to outputStream//
    
            }
        }
    

    Ensure you push your file contents to **outputStream** .

提交回复
热议问题