Results 1 to 2 of 2
- 07-21-2009, 10:15 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
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?
- 09-27-2009, 02:54 AM #2
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
-
deselecting a button after the click.
By ramsrocker in forum New To JavaReplies: 10Last Post: 02-15-2009, 06:52 AM -
Double click radio button
By mandragora in forum AWT / SwingReplies: 11Last Post: 11-10-2008, 11:06 AM -
Swing - catching click button event
By Java Tip in forum Java TipReplies: 0Last Post: 03-11-2008, 11:03 PM -
AWT - catching click button event
By Java Tip in forum Java TipReplies: 0Last Post: 03-11-2008, 11:02 PM -
How to perform some event to button click
By eva in forum AWT / SwingReplies: 2Last Post: 01-16-2008, 12:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks