使用默认程序运行文件并等待编辑结束
我想使用默认程序打开从服务器下载的文件,并等待编辑完成(将更改后的文件上传到服务器上)。我以这样的方式做到了这一点:
public void init() {
try {
int fileId = Integer.valueOf(this.getParameter("id"));
System.out.println("Downloading");
String filePath = downloadFile(fileId);
String[] cmd = { "cmd.exe", "/C", "start /wait " + filePath };
System.out.println("Opening");
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
System.out.println("Uploading");
uploadFile(filePath, fileId, address, session);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
除了文件扩展名不与任何程序关联的情况之外,这工作得非常好。 Windows 显示对话框用于选择程序,客户端做出选择后过程结束,文件立即上传到服务器上。知道如何解决吗?
I want to open file dowloaded from server with default program and wait until edit is completed (to upload changed file on server). I made this in such way:
public void init() {
try {
int fileId = Integer.valueOf(this.getParameter("id"));
System.out.println("Downloading");
String filePath = downloadFile(fileId);
String[] cmd = { "cmd.exe", "/C", "start /wait " + filePath };
System.out.println("Opening");
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
System.out.println("Uploading");
uploadFile(filePath, fileId, address, session);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
This is working preety good except the case if file extension is not associated with any program. Windows display dialog for chooseing program and after client make a choice process is ended and file is immediately upload on server. Have any idea how to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是,如果没有与扩展关联的应用程序,则会生成一个单独的进程来询问用户要使用哪个应用程序。不幸的是,您可以通过 Java 做的事情不多,除非您确切知道要调用哪个应用程序,否则如果文件编辑器在启动之前未与文件关联,您将无法监视文件编辑器。
这是一项操作系统功能,没有简单的方法可以解决它。根据文件类型,您可能想要“猜测”用户将使用什么应用程序并直接调用该应用程序而不是命令解释器,但这是非常危险且容易出错的。
The problem here is that if there is no associated application with the extension, a seperate process is spawned to ask the user what application to use. There is unfortunately, not much you can do from Java, unless you know exactly what application to call, you won't be able to monitor the file editor if it's not associated with the file prior to launch.
This is an OS feature and there's no easy way around it. Depending on the file type, you might want to "guess" what application the user will use and call that directly instead of the command interpreter, but that is very risky and error prone.