8

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, how can a user download a file when they press the "download" button?

The "Download" button has an onClick() listener. And the client side class extends Composite.

I've tried to make the class extend HttpServlet, but it becomes too complicate.

I already read posts here:

  1. http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/
  2. How to use GWT when downloading Files with a Servlet?

But I still don't know how can I provide downloadable file to the user on the client side.

Community
  • 1
  • 1
Seongeun So
  • 807
  • 1
  • 10
  • 17

4 Answers4

26

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

<servlet>
    <servlet-name>downloadService</servlet-name>
    <servlet-class>AAA.BBB.CCC.DownloadServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>downloadService</servlet-name>
    <url-pattern>/<gwtmodulename>/downloadService</url-pattern>
</servlet-mapping>

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** .

Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
appbootup
  • 9,537
  • 3
  • 33
  • 65
  • Yes you're right. I've tried to do my own code, but writing file on client side is impossible. I solve this problem similar with this. Anyway, Thanks for answering. – Seongeun So Dec 06 '12 at 17:10
  • Instead of creating a new servlet you could reuse one of your GWT service servlets just overriding `javax.servlet.http.HttpServlet#doGet` method – Jaime Hablutzel Nov 15 '13 at 01:48
  • 1
    What is getfile() ? It's not defined here. – Stealth Rabbi Mar 20 '15 at 13:07
  • This doesn't actually work for me. I see a new window open up (about:blank), and the file downloads there to the file system, but is not displayed in the browser. – Stealth Rabbi Sep 30 '16 at 16:44
3

If you know the path of the file, Code snippet is shown below.

button.addClickHandler(new ClickHandler()  
{ 

    @Overrid
    public void onClick(ClickEvent event) 
    {
        Window.open(GWT.getHostPageBaseURL() + "/file.rar", "name", "enabled");
    }
});
Abhijith Nagaraja
  • 3,370
  • 6
  • 27
  • 55
Adarsha
  • 895
  • 4
  • 7
  • Thanks for your comment but I already consider about this. Problem is that I can't use file writing routine in client side(It can be compiled, but cause error - module loading error) – Seongeun So Dec 05 '12 at 16:30
  • You can't write the file on client side by code, stuff like this is not allowed on the browser. You need to to let the browser download the file, and the user specify the download location. – Spiff Dec 06 '12 at 07:17
  • You're right. I solve this problem similar way of this. Anyway, Thanks for answering. – Seongeun So Dec 06 '12 at 17:10
0

You can try ClientIO to read and write files on the client with GWT

http://www.emitrom.com/blog/client-io

0

To complete the answer of number one item in the io part...

you can refer to this link

http://www.programcreek.com/2009/02/java-convert-a-file-to-byte-array-then-convert-byte-array-to-a-file/

or refer to this code

File file = new File(enter the filepath here)
FileInputStream fis = new FileInputStream(file);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                try {
                    for (int readNum; (readNum = fis.read(buf)) != -1;) {
                        bos.write(buf, 0, readNum); //no doubt here is 0
                        //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
                        System.out.println("read " + readNum + " bytes,");
                    }
                } catch (IOException ex) {
                    System.err.println("unable to convert to bytes");
                }
                byte[] bytes = bos.toByteArray();
                outputStream.write(bytes);
                outputStream.flush();
                outputStream.close();

hope it helps!

George
  • 263
  • 5
  • 17