• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to delete a file which was created in server after redirecting it to clientside.  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below code It writes the excel file into the server and then redirects it to the client(user) with dialog-box for saving in local desktop. How Can we delete the file once response was send to client.

The below code is for creating file in server and redirect it to client.

String p = getServletContext().getRealPath("");
lstr_app = request.getContextPath();
File outputFile = new File(p+"\\Coo_"+ strDate_MMDDYY+".xls");
FileOutputStream out = new FileOutputStream(outputFile);
wb.write(out);

rs2.close(); // close Resultset
out.close();
dr.closeConnection(); // close connection
response.sendRedirect(response.encodeURLlstr_app+"/Coo_"+strDate_MMDDYY+".xls"));
please any one can suggest regarding this
Thanks for advance
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:

outputFile.delete();
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by m sreedhar:
How Can we delete the file once response was send to client.


Not easily, because you normally won't even notice that the client has requested the file. In fact, it could even happen that the client never *does* request the file.

What you probably want to do instead is not writing the xls file on the servers harddisk and then redirect the client to it, but writing the xls file directly into the servlet response stream.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!