1

I have written a Java web application that copies a file from the server to the client's machine. The user should be able to open that file on the client side by a click. In my java code I use :

Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+ FilePath);  

Where the FilePath has the client's IP address. This command opens the file but the problem is that the file opens on the server side NOT the client. Does anyone has an idea how I can do this?

Thanks,

robert_x44
  • 9,224
  • 1
  • 32
  • 37
Nick
  • 43
  • 1
  • 3
  • 7
  • 2
    You can't run arbitrary commands on the client side from a web application. What you can do is to let the user download a file in a common format which, when the user chooses to open it, gets opened in the right application through Windows' "Associated Application"-mechanism. – miku Jan 26 '11 at 15:44
  • What's a "web application"? Does it run on the server side (like a servlet or JSP) or on the client side (like an applet)? It's unclear from the answer. How does it "copy" a file to the client's machine? – Sergei Tachenov Jan 26 '11 at 15:46

3 Answers3

2

A Web Server can not access the client's computer. Imagine if Google (or any other site) could just start programs on your machine when you visit their site!

That said, if you need to make it happen, you will have to use some signed browser extension.

I did things like this many years ago with signed Java applets.

Basically, you create a Java Applet, sign it with a certificate and request certain permissions from the client. The client user allows (or denies) the permission and then the process is started.

Here are some ancient resources:

Other technologies that can access the client's computer if permissions are granted include Microsoft's ActiveX and Adobe's Flash / Flex / Air, however I don't have any experience in these technologies that I could share.

John Weisz
  • 30,137
  • 13
  • 89
  • 132
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
2

The code of a Java web application runs at the server machine, produces HTML/CSS/JS and sends it to the webbrowser which runs at the client machine. Java code doesn't run at the client machine, let alone have any direct access to the client environment.

If you want to execute Java code on the client machine, you need to do it inside a signed(!) applet which you then embed in your web page which in turn get downloaded to the client machine and executed over there. Then you can just use Desktop#open() instead of that ugly and platform-specific rundll call which ain't going to work on Linux/Mac clients.

Desktop.getDesktop().open(new File("/path/to/foo.txt")); // Opens notepad on Windows.

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • If using the applet approach, then you'll need to watch out for permissions issues related to the "sand-boxed" environment. – crowne Jan 26 '11 at 15:47
  • 1
    @crowne: that's exactly why the exclamation mark is there behind *"signed"* part in my answer. – BalusC Jan 26 '11 at 15:48
  • Thanks guys, I really appreciate your prompt responses. It seems that I need to use a singed Applet. I wrote this web-app using Java, Spring, Hibernate and JSP. This code is in the ModelAndView handleRequest() method in the server-side. I am wondering if I create a singed Applet then how I can deploy it with my WAR file? – Nick Jan 26 '11 at 18:54
  • Just drop it in public webcontent (among your JSPs). It needs to be public to enduser anyway. – BalusC Jan 26 '11 at 19:04
1

If the snippet of code that you've provided is part of a servlet, then it will execute on the server.
One way to open the file on the client side would be to generate a hyperlink that the client clicks, which should then be received by your web-app and result in the file being streamed back to the clients browser with the correct MIME type in the http header.
The user may be presented with a file open dialog and asked to select the application to use to open the file, but as long as the application is installed on the client, then the MIME types should present the correct application as a default.

crowne
  • 8,456
  • 3
  • 35
  • 50