获取动态路劲指向的资源(jsp页面生成html 静态化;获取网站页面源码)

/*
     * ur 指向jsp的路劲
     * request requst对象
     * id    公司的id 根据公司ID来创建文件夹
     * htmlName  生成的html名字
     */
    public static void createHtml(String ur,HttpServletRequest request,int id,String htmlName){
        try {
            URL url = new URL(ur);  
            URLConnection urlConnection = url.openConnection();  
            urlConnection.setConnectTimeout(6000);  
            urlConnection.setReadTimeout(6000);  
            String type = urlConnection.getContentType();  
           // System.out.println(type);  
            urlConnection.connect();  
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            int httpResult = httpURLConnection.getResponseCode();
            if (httpResult != HttpURLConnection.HTTP_OK) {
                System.out.println("连接失败!路劲错误!");
            } else {
                System.out.println("连接成功!");
//                  int contentLength = urlConnection.getContentLength();  
//                  if(contentLength > 0) {  
                        InputStream raw = urlConnection.getInputStream();  
                        InputStream in = new BufferedInputStream(raw);  
                        byte[] data = new byte[100000];  
                        int bytesRead = 0;  
                        int offset = 0;  
                        while(offset < 1000) {  
                            bytesRead = in.read(data, offset, data.length - offset);  
                            if(bytesRead == -1) {  
                                break;  
                            }  
                            offset += bytesRead;  
                        }  
                        in.close();  
                        raw.close();  
                        String str = new String(data, type.split("charset=")[1]);
                        String fp = request.getSession().getServletContext().getRealPath("/")+"html\\"+id;
                        File f = new File(fp);
                        if(!f.exists()){
                            f.mkdir();
                        }
                        String fpp = fp+"\\"+htmlName+".html";
                        f = new File(fpp);
                        if(f.exists()){
                            f.delete();
                        }
                        f.createNewFile();
                        FileWriter fw = null;
                        fw = new FileWriter(f);
                        RandomAccessFile mm = null;
                        mm = new RandomAccessFile(f, "rws");
                        mm.write(str.getBytes("utf-8"));
                        mm.close();
                       System.out.println(htmlName+".html生成成功");
            }

                
//          }  
              
            }catch(Exception e) {  
               System.out.println(htmlName+".html生成失败!");
            }  
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值