0

I have implemented a function that let's users save content from a hashmap in an excel file. The code to do so is server-side. I want the user to be able to save the file in an optional file location on his/her own computer.

I have read quite a few post on stack overflow, where specifics on how to do so is discussed. But I can't see the whole picture.

Any htought on how I should tackle this problem?

I am using GWT and apache poi for the excel file generation.

John
  • 157
  • 5
  • 13
  • You will need to implement a servlet. Something like this: http://stackoverflow.com/questions/1442893/implementing-a-simple-file-download-servlet – Christian Kuetbach Nov 25 '14 at 18:37
  • @CHristian - That is what I have read as well. But as I am new to gwt and somehwat Java, I can't seem to get the whole picture. All discussions I have found only covers some specific problem using a servlet. Any guide to how to implement a servlet? – John Nov 25 '14 at 18:49
  • Is it correct that I need to keep my excel file generation server side? – John Nov 25 '14 at 18:50

2 Answers2

0

-Create the servlet on Server side, use the ServletOutputStream

-Access the servlet url from GWT Client, when you call the servlet url from the client it'll open a dialog to save the file.

See the comment of Manik Chand https://groups.google.com/forum/#!topic/google-web-toolkit/tX5reaOhtew

gromm
  • 16
0

You can do something like this:

public class FileServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    if (request.getRequestURI().contains("/excel")) {

      String id = request.getParameter("id");         
      // Prepare your file for id = 123

      response.setHeader("Content-Disposition", "attachment;filename=myFile.xls");
      response.setContentType("application/vnd.ms-excel");

      OutputStream outputStream = response.getOutputStream();
      // document is byte[] - this is your file.
      outputStream.write(document);
      outputStream.close();
    }
  }
}

Then you need to map this servlet in your web.xml file (for example, to "/file" handler). Finally, you present a link to your users which may look like this:

https://mysite.com/file/excel/?id=123

When a user clicks on this link, the browser will ask this user where to save the file.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • at the moment I have a server-side class that receives a hashmap from client-side. I need to change this class to a servlet? Call the servlet as I am calling the class? Save the excel file on the server and then request the excel file on servers save-file location? – John Nov 25 '14 at 19:28
  • It depends on how long it takes to create the Excel file. One option is to send the hashmap the way you do it now. When a file is ready, save it to memcache, Blobstore or Cloud Storage, and respond to the client. When a client gets a response, it shows a download link to a user. If the file can be generated very fast, you can replace your existing class with this servlet. – Andrei Volgin Nov 25 '14 at 19:40
  • so it is needed to create a physical file on the server? – John Nov 25 '14 at 19:45
  • It depends on your use case. If a user may come back later to download the file, you need to store it somewhere. If you respond right away, you need to send the byte array in your response - there is no need to store it as a file. – Andrei Volgin Nov 25 '14 at 19:55
  • This where I am lost. So I serialize the excel file and send it back to the client over the network? – John Nov 25 '14 at 20:05
  • You use the servlet that I provided in my response: set the content headers on your response and write the byte array to the output stream. – Andrei Volgin Nov 25 '14 at 20:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65641/discussion-between-klelund-and-andrei-volgin). – John Nov 25 '14 at 20:29
  • I think I got it now. Just not sure what to do with outputStream.write(document); – John Nov 25 '14 at 21:45