网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

http工具类调用Session缓存

package com.amway.pos.common.utils;

公司主营业务:成都网站设计、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出平塘免费做网站回馈大家。

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpSession;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpUtils {

public static String httpPOST(String url,Object object) {

    String objectjson=JSON.toJSONString(object);

    Map map=JSON.parseObject(objectjson);

    HttpHeaders headers = new HttpHeaders();

    //定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

    HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION);

    headers.setContentType(MediaType.APPLICATION_JSON);

    headers.set("cookie",(String) session.getAttribute("oldSysSessionId"));

    HttpEntity> request = new HttpEntity>(map, headers);        

    RestTemplate restTemplate = getInstance("utf-8");

    ResponseEntity stringResponseEntity = restTemplate.postForEntity(url, request, String.class);

    Listlist = stringResponseEntity.getHeaders().get("Set-Cookie");

    if(list != null&&list.size()>0){
        session.setAttribute("oldSysSessionId",list.get(0));
    }

    String jsonResult =stringResponseEntity.getBody();

    return jsonResult;
}

public static RestTemplate getInstance(String charset) {

RestTemplate restTemplate = new RestTemplate();

List> list = restTemplate.getMessageConverters();

for (HttpMessageConverter httpMessageConverter : list) {

    if(httpMessageConverter instanceof StringHttpMessageConverter) {
        ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName(charset));

        break;
    }
}
return restTemplate;

};
public static String httpPOSTUpload(String url,String OPERATOR,String fileLocal,Integer confirmMsg,String userInfo) {
MultiValueMap map= new LinkedMultiValueMap<>();
FileSystemResource resource = new FileSystemResource(new File(fileLocal));
map.add("file", resource);
map.add("confirmMsg", confirmMsg);
map.add("OPERATOR", OPERATOR);
map.add("userInfo", userInfo);
HttpHeaders headers = new HttpHeaders();
//定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION);
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set("cookie",(String) session.getAttribute("oldSysSessionId"));
HttpEntity> request = new HttpEntity>(map, headers);
RestTemplate restTemplate = getInstance("utf-8");
ResponseEntity stringResponseEntity = restTemplate.postForEntity(url, request, String.class);
Listlist = stringResponseEntity.getHeaders().get("Set-Cookie");
if(list != null&&list.size()>0){
session.setAttribute("oldSysSessionId",list.get(0));
}
String jsonResult =stringResponseEntity.getBody();
return jsonResult;
}

public static ResponseEntity httpDownloadFilePOST(String url, HttpHeaders headers){
    try {
        url=URLDecoder.decode(url,"utf-8");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }
    //定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON
   RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
   HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION);
   headers.set("cookie",(String) session.getAttribute("oldSysSessionId"));
    RestTemplate restTemplate = getInstance("utf-8");
   headers.set(HttpHeaders.RANGE,"");
   headers.setAccept(Collections.singletonList(MediaType.ALL));
   ResponseEntity rsp = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), byte[].class);
   Listlist = rsp.getHeaders().get("Set-Cookie");
   if(list != null&&list.size()>0){
     session.setAttribute("oldSysSessionId",list.get(0));
   }
    return rsp;
}
public static ResponseEntity httpDownloadFilePOST(String url) {

    HttpHeaders headers = new HttpHeaders();
   headers.set(HttpHeaders.RANGE,"");
   headers.setAccept(Collections.singletonList(MediaType.ALL));
    return httpDownloadFilePOST(url, headers);
}

}


标题名称:http工具类调用Session缓存
网站网址:http://bjjierui.cn/article/iioieg.html

其他资讯