- String templateContent = "[InternetShortcut]" +"\n"
- + "URL= http://www.baidu.com";
- String realfilename = "我的百度" + ".url";
- String upurl = "E:/myworkspace/createhtmlpage/WebRoot";
- System.out.println(upurl);
- String filename = upurl + "/" + realfilename;
- File myfile = new File(filename);
- if(!myfile.exists()){
- 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 = newbyte[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();
- }
方法二、通过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>