静态页面发布,清除缓存问题

JAVA实用技巧

2017-06-13

27

0

场景:在web项目中,经常遇到页面放入web容器里面,常常要清空浏览器缓存才能生效?

我们可以通过java的httpclient先发请求进行处理缓存数据。

如下:

package com.ailk.spw.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.dubbo.common.json.JSON;
import com.alibaba.dubbo.common.json.JSONObject;


public class URLUtil {
    public static JSONObject callHttp(String _url) {
        URL url = null;
        HttpURLConnection httpurlconnection = null;
        JSONObject data = null;
        try {
            url = new URL(_url);
            httpurlconnection = (HttpURLConnection) url.openConnection();
            httpurlconnection.setDoOutput(true);
            httpurlconnection.setRequestMethod("POST");
            httpurlconnection.getOutputStream().flush();
            httpurlconnection.getOutputStream().close();
            //获取响应代码
//            int code = httpurlconnection.getResponseCode();
            //获取页面内容
            InputStream in = httpurlconnection.getInputStream();
            BufferedReader breader =
                    new BufferedReader(new InputStreamReader(in, "utf-8"));
            String str = breader.readLine();
            data = new JSONObject();
            data.put("resultCode", "0");
            data.put("resultMsg", str);
        } catch (Exception e) {
            e.printStackTrace();
            data =new JSONObject();
            data.put("resultCode", "1");
            data.put("resultMsg", "系统异常,请稍后再试!");
        } finally {
            if (httpurlconnection != null)
                httpurlconnection.disconnect();
        }
        return data;
    }
}

发表评论

全部评论:0条

houzhe11

JAVA从业者