java程序关联应用打开指定文件

很简单 创建文件对象之后根据路径打开文件即可

1、代码:

public  String  openFile(@RequestParam( value="id",required=false)Integer id) {
		TiJian tijian = tijianService.selectByPrimaryKey(id);
		String url;
		String tips= "1"; 
		//如果查询到文件路径了,有文件就打开,没有就返回2
		if(tijian.getFujianid()!=null) {
			url = tijian.getFujianid();
			try {
				File file = new File(url);// 创建文件对象
				Desktop.getDesktop().open(file); // 启动已在本机桌面上注册的关联应用程序,打开文件文件file。
			}catch(Exception e){
				System.err.println(e);
			}
			tips = "2";
		}
		return tips;
	}

2、效果

原文地址:https://www.cnblogs.com/Mr-gaoxin/p/12073266.html