只要访问本页面就可以自动在桌面创建快捷方式,当然,其他路径也可以。如果想点击按钮生成快捷方式,把按钮的链接设置成本页面即可!
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.io.*"%>
<%@page import="javax.swing.filechooser.*"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String templateContent = "[InternetShortcut]" + "\n" + "URL=http://admin.weiscloud.com/";
String realfilename = "XXXX" + ".url";
FileSystemView fsv = FileSystemView.getFileSystemView();
String upurl = fsv.getHomeDirectory().toString();
String filename = upurl + "/" + realfilename;
File myfile = new File(filename);
System.out.println(12);
if(!myfile.exists()){
System.out.print(11);
FileOutputStream fileoutputstream = new FileOutputStream(filename);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
try {
File file = new File(upurl, realfilename);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[111000];
realfilename = java.net.URLEncoder.encode(realfilename,
"UTF-8");
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");//不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition",
"attachment; filename=" + realfilename);
OutputStream os = response.getOutputStream();
while (bis.read(buffer) > 0) {
os.write(buffer);
}
bis.close();
os.close();
out.clear();
out=pageContext.pushBody();
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html> </span>
2第二种方式是:下载jshortcut-0_4.jar这种方式在服务器上是有的问题的
@RequestMapping("/deskIco")
@ResponseBody
public ResponseBean deskIco(HttpServletRequest request,String ico) {
ResponseBean rb=new ResponseBean();
try{
FileSystemView fsv = FileSystemView.getFileSystemView();
String writeFolderPath = fsv.getHomeDirectory().toString()+"\\"; //这便是读取桌面路径的方法了
String jarFileName = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";//建立快捷方式后鼠标放到上面的时候现实的文件所存位置
// create lnk file
JShellLink link = new JShellLink();
link.setFolder(writeFolderPath); //创建的快捷方式所存在的位置,路径要真实路径,放到快速启动栏里面
link.setName("XXX后台"); //快捷方式的名称
String savefile=PathUtil.getWEB_ROOT(request)+PathUtil.PATH_SEPARATOR+"images"+PathUtil.PATH_SEPARATOR+"ico"+PathUtil.PATH_SEPARATOR+ico+".ico";
System.out.print(savefile);
link.setIconLocation(savefile);//图片位置
link.setPath(jarFileName);
link.setArguments("http://test2.weiscloud.com/");//设置执行参数
link.save();
link.load();
rb.setMessage("桌面图标创建成功");
}catch(Exception e){
e.printStackTrace();
rb.setErrorMessageByKey("SERVICE_EOR");
}
return rb;
}