将本网站保存到桌面

时间:2022-05-11 04:04:32

<%@page import="java.io.*"%>  
<%@page import="javax.swing.filechooser.*"%>  

  
<%  
String title = request.getParameter("param1");
String realPath1 = "http://"  +  request.getServerName()  +  ":"  +  request.getServerPort()  +  request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);  
    String templateContent = "[InternetShortcut]" + "\n" + "URL= "+realPath1 + "\n" + "IconFile=images/favicon.ico" + "\n" + "IconIndex=1";
    String realfilename = title+" " + ".url"; 
    realfilename = realfilename.replaceAll(",","");
    FileSystemView fsv = FileSystemView.getFileSystemView();      
    String upurl = fsv.getHomeDirectory().toString();     
    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 = 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();     
    }   
  
%>