File download to client... new Java user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    File download to client... new Java user

    I'm a seasoned .net guy - new to Java. I'm Trying to do the simplest of things but can't figure it out. In my web form I want to create a csv file (from a json string) and present it to the user to download (I do not want to specify a directory - let the user do that)

    I have this working code which writes my file to a directory but really all I want is for it to download in the browser - NOT write to a filepath. How is this done?

    Code:
         JSONObject output = new JSONObject(jsonString);
         JSONArray docs = output.getJSONArray("myData");
         File file=new File("C:\\Work\\fromJSON.csv"); //DON'T WANT TO SPECIFY A DIRECTORY
         String csv = CDL.toString(docs);
         FileUtils.writeStringToFile(file, csv);
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Can't you just write the content to the response's output stream like you would in ASP.NET?

    -Frinny

    Comment

    • yarbrough40
      Contributor
      • Jun 2009
      • 320

      #3
      funny enough that didn't seem to work when I tried it but perhaps I wasn't doing it correctly - Thanks Frinny... I will revisit that path

      Comment

      • yarbrough40
        Contributor
        • Jun 2009
        • 320

        #4
        no luck. I can't even figure out how to do it with a simple text file.. This little sample writes my file out to a directory. How would I write it out to the client for download?... man I feel like an idiot : )
        Code:
             String data = "My Text To Put In The File";
             byte[] byteData = data.getBytes();
             
             OutputStream out = new FileOutputStream("MyFile.txt");
             out.write(byteData);
             out.close();

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I'm not seeing you using response.out as the stream.

          Do you have access to a response object?

          Comment

          • yarbrough40
            Contributor
            • Jun 2009
            • 320

            #6
            I figured it out finally. I am using this within a Wavemaker project. Wavemaker has a bool property I did not see called "DownloadFi le" set that to true and it all works. I just LOVE when stupid little problems consume 3 full days : )
            -
            Once again, thanks for looking at it with me Frinny!

            Comment

            Working...