中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

http 工具類調用Session 緩存

發布時間:2020-07-27 09:57:55 來源:網絡 閱讀:146 作者:IT達仁 欄目:軟件技術

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<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);        

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

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

    List<String >list = 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<HttpMessageConverter<?>> 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<String,Object> 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<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(map, headers);
RestTemplate restTemplate = getInstance("utf-8");
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, request, String.class);
List<String >list = 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<byte[]> 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<byte[]> rsp = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), byte[].class);
   List<String >list = rsp.getHeaders().get("Set-Cookie");
   if(list != null&&list.size()>0){
     session.setAttribute("oldSysSessionId",list.get(0));
   }
    return rsp;
}
public static ResponseEntity<byte[]> httpDownloadFilePOST(String url) {

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

}

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

慈溪市| 通城县| 武隆县| 宜兰县| 丰城市| 吉水县| 额济纳旗| 新巴尔虎左旗| 昂仁县| 治多县| 扶沟县| 凤城市| 天门市| 房产| 溧水县| 汉阴县| 苏尼特右旗| 岚皋县| 宁晋县| 平果县| 哈密市| 正镶白旗| 洞头县| 瑞安市| 衡水市| 香河县| 民乐县| 金阳县| 松潘县| 郓城县| 长岛县| 额敏县| 曲松县| 萍乡市| 五峰| 仁寿县| 昆山市| 崇信县| 寿宁县| 旬邑县| 公主岭市|