把网站生成快捷方式到桌面

来源:互联网 发布:美工培训视频基础知识 编辑:程序博客网 时间:2024/05/02 03:04
  1. String templateContent = "[InternetShortcut]" +"\n" 
  2.             + "URL= http://www.baidu.com"
  3.     String realfilename = "我的百度" + ".url"
  4.     String upurl = "E:/myworkspace/createhtmlpage/WebRoot"
  5.     System.out.println(upurl); 
  6.     String filename = upurl + "/" + realfilename; 
  7.     File myfile = new File(filename); 
  8.     if(!myfile.exists()){ 
  9.         FileOutputStream fileoutputstream = new FileOutputStream(filename);//建立文件输出流 
  10.         byte tag_bytes[] = templateContent.getBytes(); 
  11.         fileoutputstream.write(tag_bytes); 
  12.         fileoutputstream.close(); 
  13.     } 
  14.     try
  15.         File file = new File(upurl, realfilename); 
  16.         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); 
  17.         byte[] buffer = newbyte[111000]; 
  18.         realfilename = java.net.URLEncoder.encode(realfilename, 
  19.         "UTF-8"); 
  20.         response.reset(); 
  21.         response.setCharacterEncoding("UTF-8"); 
  22.         response.setContentType("application/x-download");//不同类型的文件对应不同的MIME类型 
  23.         response.setHeader("Content-Disposition"
  24.         "attachment; filename=" + realfilename); 
  25.         OutputStream os = response.getOutputStream(); 
  26.         while (bis.read(buffer) > 0) { 
  27.             os.write(buffer); 
  28.         } 
  29.         bis.close(); 
  30.         os.close(); 
  31.         out.clear(); 
  32.         out=pageContext.pushBody(); 
  33.     } catch (Exception e) { 
  34.         e.printStackTrace(); 
  35.     } 

 

方法二、通过JS实现:

<html>
<head></head>

<script language="JavaScript">
function toDesktop(sUrl,sName){
try
{
var WshShell = new ActiveXObject("WScript.Shell");
var oUrlLink = WshShell.CreateShortcut(WshShell.SpecialFolders("Desktop") + "\\" + sName + ".url");
oUrlLink.TargetPath = sUrl;
oUrlLink.Save();
}
catch(e)
{
alert("当前IE安全级别不允许操作!");
}
}
</script>

 

<body>
<input name="btn" type="button" id="btn" value="新客网" onClick="toDesktop('http:\//www.xker.com/','新客网')">
<input name="btn" type="button" id="btn" value="C盘" onClick="toDesktop('file:\//C:','C盘')">

</body>
</html>

 

原创粉丝点击