Results 1 to 2 of 2
  1. #1
    violeta is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default Generate csv file on a button click

    Hello everyone,
    I have a web page where a user can choose a group name, and then he's sent to another jsp where I display members for the chosen group based on a query result. I want the user to be able to click on a button that will let him save the result as csv file. I tried to create the file when the jsp loads; however, there was a problem when more than one user clicked on the button at the same time, because the file was getting overwritten and only one user was getting file with the right result. When 100 users click on the button, I don't want to save 100 files to the server. Is there any efficient way to do it?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    not sure why would would need to write to a csv file on the server in the first place, couldn't the generator just stream the csv content back to the client in the response ?

    but if you do need to invoke some external utility to render a csv file, instead of it using a hard coded /tmp/file.csv path, which yes, gets stomped on when more than one concurrent user does the request,
    could you at the start of the request, generate a unique file name. then instead of redirecting to the url of the csv file (which you must be doing, to need to have the file there in the first place?) , have the servlet stream the generated file back, then delete the file after.

    ..psudocode..
    Java Code:
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
      // generate unique csv file name
     String csvFileName =  "/tmp/report-" + String.valueOf(new Date().getTime()) + ".csv"
    // render to this file
      // open this file
      InputStream in = new FileInputStream(csvFileName);    
      OutputStream out =   response.getOutputStream();
      response.setContentType("text/csv");
      // while not end of input stream  {
        // read bits, write to output stream
      } // while
      out.flush();
      in.close();
      // delete the generated file.
      new File(csvFileName).delete();
    }

Similar Threads

  1. deselecting a button after the click.
    By ramsrocker in forum New To Java
    Replies: 10
    Last Post: 02-15-2009, 06:52 AM
  2. Double click radio button
    By mandragora in forum AWT / Swing
    Replies: 11
    Last Post: 11-10-2008, 11:06 AM
  3. Swing - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:03 PM
  4. AWT - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:02 PM
  5. How to perform some event to button click
    By eva in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •