简体   繁体   中英

How to capture the event of closing an opened excel file in java web application

I'm working on a java web application. For security reason, when user clicking on the open file link from the webpage, the application will copy the file from the security folder to a temp folder, where everyone have the access, then open it. After user finish reading the excel file and close it, I need to capture this closing event and delete the file from temp folder, so that no other people could see the file.

By open excel file from javascript in jsp page, the code is as below:

var myApp = new ActiveXObject("Excel.Application");
myApp.visible = true;
myApp.workbooks.open(fileNameWithTypeAndPath);  

But I don't know how to capture the closing excel event.


I have tryied to another way by using runtime waitfor(). The code is as below

Runtime rt=Runtime.getRuntime();            
Process proc= rt.exec("cmd.exe /c C:\\peter\\workspace\\Temp\\Translation.xlsx ");                      
InputStream stderr= proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while((line= br.readLine())!=null)
{
    System.out.println(line);
}
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: "+exitVal);

However this is a web application, when execute

Process proc= rt.exec("cmd.exe /c C:\\peter\\workspace\\Temp\\Translation.xlsx ");

It is running on web server, not user's machine.

Thanks for any suggestion about this problem!

"It is running on web server, not user's machine." 

You answered your own question. It can't be done. All your JSP/Servlet can do is send stuff to the client's webbrowser. It can't access the filesystem. And there is no way Excel is going to send a close event back to you.

You should look into how to properly serve a binary file from a servlet rather than using Javascript to open or attempting to use a commandline call within Java (which just opens it on the server).

But that still won't help with the issue of removing the file from the temp folder on their system when the user closes it. You can't do that. You can set no-cache headers on the servlet that serves the Excel file (if you are serving it properly) but that will only make the browser pull the latest file. It will not make Excel delete from the temp folder when the users closes the file.

Still here is the basic jist of how to properly serve the file:

response.setContentType("application/msexcel");
response.addHeader("Content-Disposition", "filename=filename.xls");

Then write the bytes to response.getOutputStream(). This will not work well with a JSP, so it should be a servlet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM