本篇內(nèi)容主要講解“HttpClient遠(yuǎn)程請(qǐng)求struts2的文件流并實(shí)現(xiàn)下載的方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“HttpClient遠(yuǎn)程請(qǐng)求struts2的文件流并實(shí)現(xiàn)下載的方法”吧!
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、壽陽(yáng)ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的壽陽(yáng)網(wǎng)站制作公司
HttpClientUtil工具類:
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.net.URI; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; public class HttpClientUtil { private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); /** * 封裝HTTP POST方法 * * @param * @param * @return * @throws ClientProtocolException * @throws IOException */ public static String post(String url, MapparamMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpPost.setConfig(requestConfig); List formparams = setHttpParams(paramMap); UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8"); httpPost.setEntity(param); HttpResponse response = httpClient.execute(httpPost); // logger.info("************{}", response); String httpEntityContent = getHttpEntityContent(response); // logger.info("************{}", httpEntityContent); httpPost.abort(); // logger.info("************{}", httpEntityContent); return httpEntityContent; } /** * 封裝HTTP POST方法 * * @param * @param * @return * @throws ClientProtocolException * @throws IOException */ public static HttpResponse postForResponse(String url, Map paramMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpPost.setConfig(requestConfig); List formparams = setHttpParams(paramMap); UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8"); httpPost.setEntity(param); HttpResponse response = httpClient.execute(httpPost); return response; } /** * 封裝HTTP POST方法 * * @param * @param (如JSON串) * @return * @throws ClientProtocolException * @throws IOException */ public static String post(String url, String data) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpPost.setConfig(requestConfig); httpPost.setHeader("Content-Type", "text/json; charset=utf-8"); httpPost.setEntity(new StringEntity(URLEncoder.encode(data, "UTF-8"))); HttpResponse response = httpClient.execute(httpPost); String httpEntityContent = getHttpEntityContent(response); httpPost.abort(); return httpEntityContent; } /** * 封裝HTTP GET方法 * * @param * @return * @throws ClientProtocolException * @throws IOException */ public static String get(String url) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(); //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpGet.setConfig(requestConfig); httpGet.setURI(URI.create(url)); HttpResponse response = httpClient.execute(httpGet); String httpEntityContent = getHttpEntityContent(response); httpGet.abort(); return httpEntityContent; } /** * 封裝HTTP GET方法 * * @param * @param * @return * @throws ClientProtocolException * @throws IOException */ public static String get(String url, Map paramMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(); //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpGet.setConfig(requestConfig); List formparams = setHttpParams(paramMap); String param = URLEncodedUtils.format(formparams, "UTF-8"); httpGet.setURI(URI.create(url + "?" + param)); HttpResponse response = httpClient.execute(httpGet); String httpEntityContent = getHttpEntityContent(response); httpGet.abort(); return httpEntityContent; } /** * 封裝HTTP PUT方法 * * @param * @param * @return * @throws ClientProtocolException * @throws IOException */ public static String put(String url, Map paramMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(url); //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpPut.setConfig(requestConfig); List formparams = setHttpParams(paramMap); UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8"); httpPut.setEntity(param); HttpResponse response = httpClient.execute(httpPut); String httpEntityContent = getHttpEntityContent(response); httpPut.abort(); return httpEntityContent; } /** * 封裝HTTP DELETE方法 * * @param * @return * @throws ClientProtocolException * @throws IOException */ public static String delete(String url) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpDelete httpDelete = new HttpDelete(); //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpDelete.setConfig(requestConfig); httpDelete.setURI(URI.create(url)); HttpResponse response = httpClient.execute(httpDelete); String httpEntityContent = getHttpEntityContent(response); httpDelete.abort(); return httpEntityContent; } /** * 封裝HTTP DELETE方法 * * @param * @param * @return * @throws ClientProtocolException * @throws IOException */ public static String delete(String url, Map paramMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpDelete httpDelete = new HttpDelete(); //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpDelete.setConfig(requestConfig); List formparams = setHttpParams(paramMap); String param = URLEncodedUtils.format(formparams, "UTF-8"); httpDelete.setURI(URI.create(url + "?" + param)); HttpResponse response = httpClient.execute(httpDelete); String httpEntityContent = getHttpEntityContent(response); httpDelete.abort(); return httpEntityContent; } /** * 設(shè)置請(qǐng)求參數(shù) * * @param * @return */ private static List setHttpParams(Map paramMap) { List formparams = new ArrayList (); Set > set = paramMap.entrySet(); for (Map.Entry entry : set) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } return formparams; } /** * 獲得響應(yīng)HTTP實(shí)體內(nèi)容 * * @param response * @return * @throws IOException * @throws UnsupportedEncodingException */ public static String getHttpEntityContent(HttpResponse response) throws IOException, UnsupportedEncodingException { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream is = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); String line = br.readLine(); StringBuilder sb = new StringBuilder(); while (line != null) { sb.append(line + "\n"); line = br.readLine(); } return sb.toString(); } return ""; } }
spring跨域請(qǐng)求數(shù)據(jù):
/** * 跨域請(qǐng)求數(shù)據(jù) * @param model * @param code * @return */ @RequestMapping(value = "/getDataFromCost") public void getDataFromCost(ModelAndView model,Long eprjInfoId,Long cprjInfoId,Integer uploadType,String loginname,String t,String h,String eprjAuthMap){ GenericIdentityBean identityBean = this.genGenericIdentity((IdentityBean)model.getModel().get(WebReviewParam.MODEL_IDENTITY)); UserObject user = userSessionService.getUser(identityBean); MappostData = new HashMap<>(); //costHost = "http://localhost:8080/cost"; String url = costHost+"/token2"; if(user != null) { Map paramsObject; try { paramsObject = this.goToCost(model, 1l); postData.put("loginname", paramsObject.get("loginname")+""); postData.put("t", paramsObject.get("t")+""); postData.put("h", paramsObject.get("h")+""); postData.put("eprjInfoId", "0"); postData.put("uploadType", "50"); postData.put("cprjInfoId", cprjInfoId+""); postData.put("isChina", "false"); postData.put("remoteUrl", "down"); } catch (Exception e1) { e1.printStackTrace(); } String filename = "備份文件導(dǎo)出.hwp"; if(!UnitChange.isEmpty(loginname)){ try { filename = URLDecoder.decode(loginname, "utf-8"); } catch (Exception e) { } } try { HttpResponse postForResponse = HttpClientUtil.postForResponse(url, postData); HttpEntity entity = postForResponse.getEntity(); if(entity != null) { InputStream is = entity.getContent(); HttpServletResponse response = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getResponse(); String contentType = "application/x-hwp"; String contentDisposition="attachment;"; filename = URLEncoder.encode(filename, "UTF-8"); contentDisposition+="filename=\""+filename+"\""; response.setContentType(contentType); response.setHeader("accept-ranges", "bytes"); response.setHeader("Content-Disposition", contentDisposition); try { ServletOutputStream outputStream = response.getOutputStream(); IOUtils.copy(is, outputStream); outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }
//獲取跨域跳轉(zhuǎn)需要的一些參數(shù)
/** * 跳轉(zhuǎn)造價(jià)詳細(xì)審核頁(yè)面 * @return * @throws Exception */ @ResponseBody @RequestMapping(value = "/goToCost") public MapgoToCost(ModelAndView model,Long eprjInfoId) throws Exception{ GenericIdentityBean identityBean = this.genGenericIdentity((IdentityBean)model.getModel().get(WebReviewParam.MODEL_IDENTITY)); UserObject user = userSessionService.getUser(identityBean); if(UnitChange.isEmpty(user.getSecretKey())) { user.setSecretKey(UuidUtil.uuid()); userService.update(identityBean, user.getId(), user); } Map returnMap = new HashMap<>(); returnMap.put("loginname", user.getUsername()); long t = new Date().getTime(); returnMap.put("t", t); String hash = MD5.encode16((user.getUsername()+user.getSecretKey()+t).getBytes(Charset.forName("UTF-8"))); returnMap.put("h", hash); returnMap.put("costHost", costHost); returnMap.put("costHost2", GlobalParameters.getServerUrl()+"/cost"); //跳轉(zhuǎn)造價(jià)路徑直接取當(dāng)前域名+/cost returnMap.put("returnCode",EprjParam.SUCCESS); return returnMap; }
html頁(yè)面和JS:
function downloadFile(url,paramObj){ var xhr = new XMLHttpRequest(); // xhr.open('GET', url, true); // 使用GET方式比較簡(jiǎn)單,參數(shù)直接附在URL上 xhr.open('POST', url, true); //POST的格式相對(duì)比較靈活,參數(shù)可以有比較多的形式,例如JSON,表單FORM等 xhr.responseType = "blob"; // 返回類型blob // xhr.setRequestHeader("Content-Type","application/json");//提交的數(shù)據(jù)為json格式 xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //后臺(tái)有使用JWT的token驗(yàn)證,會(huì)從請(qǐng)求頭中讀取"X-token"的值,所以這里在請(qǐng)求頭加入"X-token" xhr.setRequestHeader("X-token", 'Bearer ' + localStorage.getItem(SAVE_AUTH)); // 定義請(qǐng)求完成的處理函數(shù) xhr.onload = function () { jQuery.messager.progress('close'); // 請(qǐng)求完成 if (this.status === 200) { // 返回200 var blob = this.response; var reader = new FileReader(); reader.readAsDataURL(blob); // 轉(zhuǎn)換為base64,可以直接放入a表情href reader.onload = function (e) { // 轉(zhuǎn)換完成,創(chuàng)建一個(gè)a標(biāo)簽用于下載 var a = document.createElement('a'); a.download = paramObj["cprjName"]+".hwp"; //可以通過(guò)導(dǎo)出空文件確認(rèn)文件最小的長(zhǎng)度為700000,如果長(zhǎng)度過(guò)小,則表示遠(yuǎn)程拋出異常 if(e.target.result.indexOf("data:application/x-hwp") != -1 && e.target.result.length > 700000){ a.href = e.target.result; $("body").append(a); // 修復(fù)firefox中無(wú)法觸發(fā)click a.click(); $(a).remove(); }else{ $.messager.show({ icon: "info", msg: "備份文件大小異常,可重新嘗試備份操作!", position: "topCenter" ,timeout:1000, height:80}); } } }else{ $.messager.show({ icon: "info", msg: "備份文件大小異常,可重新嘗試備份操作!", position: "topCenter" ,timeout:1000, height:80}); } }; // 發(fā)送ajax請(qǐng)求,案例中我們使用POST的請(qǐng)求格式,參數(shù)類型為JSON var param = {}; xhr.send(null); jQuery.messager.progress({ title:'請(qǐng)稍候...', text:'正在備份導(dǎo)出,請(qǐng)稍候...', interval:800 }); } function myDownFun(paramObj){ var url = "./iface/getDataFromCost?cprjInfoId="+paramObj["cprjInfoId"]; var rowData = $('#gcxh').treegrid('getSelected'); url += "&loginname="+encodeURIComponent(encodeURIComponent(rowData.name)); var postData = {}; postData["cprjName"] = rowData.cprjNameText; downloadFile(url,postData); // openPostWindow(url,data); } //本來(lái)可以使用form表單提交的方式,或者a連接的href進(jìn)行導(dǎo)出,只是這兩種方式不好捕捉和處理遠(yuǎn)程請(qǐng)求的異常exception //下載 function openPostWindow(url,obj) { var formStr = ''; //設(shè)置樣式為隱藏,打開(kāi)新標(biāo)簽再跳轉(zhuǎn)頁(yè)面前,如果有可現(xiàn)實(shí)的表單選項(xiàng),用戶會(huì)看到表單內(nèi)容數(shù)據(jù) formStr = ''; $('body').append(formStr); $('#downTable')[0].submit(); $('#downTable').remove(); }
跨域訪問(wèn)的方法struts2
text/html returnCode,message,data.* false true ${contentDisposition} ${contentType} inputStream /default
java(struts2):
public String token2(){ if(VerifyString.isEmpty(loginname) || eprjInfoId == null) { return "login"; } if (tstruts2的xml請(qǐng)求配置文件
${contentDisposition} ${contentType} inputStream 文件流:
/** * 導(dǎo)出文件 * @return */ @Permission(authorize=Permission.AUTHORIZE_EXPORT) public String download() { Boolean isError=false; ActionContext context=ActionContext.getContext(); if (context==null) { return ActionSupport.ERROR; } Integer fileLength=0; HttpServletRequest request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST); HttpServletResponse response=(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);; try { String filename = ""; switch (this.uploadType) { case 50:// 建設(shè)項(xiàng)目 工程項(xiàng)目備份導(dǎo)出 { this.inputStream=cprjInfoService.backup(cprjInfoId, eprjInfoId); if(this.inputStream != null){ fileLength=this.inputStream.available(); int minLength=700000;//普通工程大小 //備份的建設(shè)項(xiàng)目或工程項(xiàng)目小于700000,代表備份文件有異常,報(bào)錯(cuò) if(fileLength"); out.println(""); out.println(""+this.message+""); out.println(""); out.println("