您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“HttpClient遠程請求struts2的文件流并實現(xiàn)下載的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“HttpClient遠程請求struts2的文件流并實現(xiàn)下載的方法”吧!
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, Map<String, String> paramMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); //設置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpPost.setConfig(requestConfig); List<NameValuePair> 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<String, String> paramMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); //設置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpPost.setConfig(requestConfig); List<NameValuePair> 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); //設置請求和傳輸超時時間 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(); //設置請求和傳輸超時時間 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<String, String> paramMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(); //設置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpGet.setConfig(requestConfig); List<NameValuePair> 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<String, String> paramMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(url); //設置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpPut.setConfig(requestConfig); List<NameValuePair> 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(); //設置請求和傳輸超時時間 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<String, String> paramMap) throws ClientProtocolException, IOException { HttpClient httpClient = HttpClients.createDefault(); HttpDelete httpDelete = new HttpDelete(); //設置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build(); httpDelete.setConfig(requestConfig); List<NameValuePair> 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ù) * * @param * @return */ private static List<NameValuePair> setHttpParams(Map<String, String> paramMap) { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); Set<Map.Entry<String, String>> set = paramMap.entrySet(); for (Map.Entry<String, String> entry : set) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } return formparams; } /** * 獲得響應HTTP實體內(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跨域請求數(shù)據(jù):
/** * 跨域請求數(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); Map<String,String> postData = new HashMap<>(); //costHost = "http://localhost:8080/cost"; String url = costHost+"/token2"; if(user != null) { Map<String, Object> 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 = "備份文件導出.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(); } } }
//獲取跨域跳轉需要的一些參數(shù)
/** * 跳轉造價詳細審核頁面 * @return * @throws Exception */ @ResponseBody @RequestMapping(value = "/goToCost") public Map<String,Object> goToCost(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<String,Object> 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"); //跳轉造價路徑直接取當前域名+/cost returnMap.put("returnCode",EprjParam.SUCCESS); return returnMap; }
html頁面和JS:
function downloadFile(url,paramObj){ var xhr = new XMLHttpRequest(); // xhr.open('GET', url, true); // 使用GET方式比較簡單,參數(shù)直接附在URL上 xhr.open('POST', url, true); //POST的格式相對比較靈活,參數(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"); //后臺有使用JWT的token驗證,會從請求頭中讀取"X-token"的值,所以這里在請求頭加入"X-token" xhr.setRequestHeader("X-token", 'Bearer ' + localStorage.getItem(SAVE_AUTH)); // 定義請求完成的處理函數(shù) xhr.onload = function () { jQuery.messager.progress('close'); // 請求完成 if (this.status === 200) { // 返回200 var blob = this.response; var reader = new FileReader(); reader.readAsDataURL(blob); // 轉換為base64,可以直接放入a表情href reader.onload = function (e) { // 轉換完成,創(chuàng)建一個a標簽用于下載 var a = document.createElement('a'); a.download = paramObj["cprjName"]+".hwp"; //可以通過導出空文件確認文件最小的長度為700000,如果長度過小,則表示遠程拋出異常 if(e.target.result.indexOf("data:application/x-hwp") != -1 && e.target.result.length > 700000){ a.href = e.target.result; $("body").append(a); // 修復firefox中無法觸發(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請求,案例中我們使用POST的請求格式,參數(shù)類型為JSON var param = {}; xhr.send(null); jQuery.messager.progress({ title:'請稍候...', text:'正在備份導出,請稍候...', 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); } //本來可以使用form表單提交的方式,或者a連接的href進行導出,只是這兩種方式不好捕捉和處理遠程請求的異常exception //<a class="easyui-linkbutton" href="${object.publicResourcesUrl }" target="_blank" //title="備份導出" data-options="plain: true,iconCls:'fas fa-file-download e-icon fa-fw'" download>下載</a> function openPostWindow(url,obj) { var formStr = ''; //設置樣式為隱藏,打開新標簽再跳轉頁面前,如果有可現(xiàn)實的表單選項,用戶會看到表單內(nèi)容數(shù)據(jù) formStr = '<form id="downTable" method="POST" action="' + url + '">'; formStr += '<input type="hidden" name="eprjInfoId" value="0" />'; formStr += '<input type="hidden" name="cprjInfoId" value="' + obj["cprjInfoId"] + '" />'; formStr += '<input type="hidden" name="uploadType" value="' + obj["uploadType"] + '" />'; formStr += '</form>'; $('body').append(formStr); $('#downTable')[0].submit(); $('#downTable').remove(); }
跨域訪問的方法struts2
<action name="token2" method="token2" class="cost-importAndExportAction"> <result name="myJson" type="json"> <!-- 遠程請求進度條信息,這里不用管 --> <param name="contentType">text/html</param> <param name="includeProperties">returnCode,message,data.*</param> <param name="ignoreHierarchy">false</param> <param name="enableGZIP" >true</param> </result> <result type="stroageStream"><!-- 遠程請求文件流 --> <param name="contentDisposition">${contentDisposition}</param> <param name="contentType">${contentType}</param> <param name="inputName">inputStream</param> </result> <result name="login" type="redirect">/default</result> </action>
java(struts2):
public String token2(){ if(VerifyString.isEmpty(loginname) || eprjInfoId == null) { return "login"; } if (t<System.currentTimeMillis()/1000-60) { //1分鐘內(nèi)有效 return "login"; } UserObject userObject = null; try { userObject = userService.findByUsername(null, URLDecoder.decode(loginname, "utf-8")); } catch (Exception e) { } if (userObject==null) { throw new BaseException(this.getClass(),OrganException.USER_NOT_EXIST,""); } String hash =MD5.encode16((userObject.getUsername()+userObject.getSecretKey()+t).getBytes(Charset.forName("UTF-8"))); if (!hash.equalsIgnoreCase(h)) { throw new BaseException(this.getClass(),OrganException.USER_NOT_EXIST,""); } return this.download();//文件流 }
struts2的xml請求配置文件
<action name="download" method="download" class="cost-personnelsAction"> <result type="stroageStream"> <param name="contentDisposition">${contentDisposition}</param> <param name="contentType">${contentType}</param> <param name="inputName">inputStream</param> </result> </action>
文件流:
/** * 導出文件 * @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:// 建設項目 工程項目備份導出 { this.inputStream=cprjInfoService.backup(cprjInfoId, eprjInfoId); if(this.inputStream != null){ fileLength=this.inputStream.available(); int minLength=700000;//普通工程大小 //備份的建設項目或工程項目小于700000,代表備份文件有異常,報錯 if(fileLength<minLength) { throw new Excpetion(this.getClass(), Cost.COST_ERROR, "備份文件大小異常"); } } } break; default: break; } if(inputStream != null) fileLength=inputStream.available(); else fileLength=0; String method=request.getMethod(); String userAgent=request.getHeader("User-Agent"); Browser browser=Browser.fromUserAgent(userAgent); this.contentType=FileContentType.buildContentType(filename); if("POST".equals(method)) { filename=URLEncoder.encode(filename, "UTF-8"); } else { switch(browser.getType()) { case Browser.IE: filename=URLEncoder.encode(filename, "UTF-8"); break; default: filename=new String(filename.getBytes(),"ISO8859-1"); break; } } this.contentDisposition="attachment;"; this.contentDisposition+="filename=\""+filename+"\""; response.setHeader("accept-ranges", "bytes"); // response.setHeader("content-length", String.valueOf(fileLength)); }catch(Exception e) { isError=true; if(e instanceof BaseException) { this.returnCode=((BaseException)e).getCode(); this.message=languageException.toString((BaseException)e); Log.error(e.getMessage(),e); } else { Log.error(e.getMessage(),e); this.returnCode=BaseException.UNKNOWN; this.message=languageException.toString(e); } response.setContentType("text/html;charset=UTF-8"); PrintWriter out; try { out = new PrintWriter(response.getWriter()); out.println("<html>"); out.println("<body bgcolor=\"white\">"); out.println("<span class=\"bold\">"+this.message+"</span>"); out.println("</body>"); out.println("</html>"); out.flush(); } catch (Exception e1) { } return ERROR; } finally { if(progress != null && userObject != null) { progress.put("progressValue", 102); progress.put("progressText", ""); userService.notsupSetProgressInfo(userObject, progress); } } if(!isError) return SUCCESS; else return ERROR;
到此,相信大家對“HttpClient遠程請求struts2的文件流并實現(xiàn)下載的方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。