最近作項目有一個需求:一個網盤系統,文件存放在分佈式文件系統中,以前的文件下載統一走的文件下載服務,如今須要在單文件下載的時候不須要走文件下載服務,而是直接訪問文件系統上的路徑,響應的時候修改響應頭,使之變爲文件下載(減小文件下載服務的壓力)。java
分析:該需求有兩點;①在網關中路由的時候修改路由地址②響應的時候修改響應頭,使之變爲文件下載。spring
直接看網關中的過濾器代碼實現:app
修改請求路徑:分佈式
package com.example.demo; import java.net.URI; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; import org.springframework.stereotype.Component; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; /** * 需求:若是請求路徑內包含list請求,則從新路由到指定的接口,路由結束後,修改響應頭 * * @author mxf * */ @Component public class RequestFilter extends ZuulFilter { @Override public boolean shouldFilter() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String uri = request.getRequestURI(); /** * 根據條件去判斷是否須要路由,是否須要執行該過濾器 */ if (uri.contains("list")) { return true; } return false; } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletResponse response = ctx.getResponse(); try { // 下面是資源地址 // http://192.168.16.72:8077/group1/M00/00/03/wKgQSVzHshWAdSpoAAAFsn6csJI682.png // URI uri2 = new URI(requestURL.substring(0,requestURL.indexOf(requestURI) + 1)); /** * 重點:由於根據條件去路由新的連接,因此這裏要從新設置 RouteHost 和 URI */ URI uri2 = new URI("http://192.168.16.72:8077/"); // 設置新的RouteHost ctx.setRouteHost(uri2.toURL()); /** * 請求間攜帶的信息 放在請求頭中,方便響應的時候修改響應頭 */ response.addHeader("sign", "target"); response.addHeader("info", "bb.png"); response.addHeader("info_size", "1458"); } catch (Exception e1) { e1.printStackTrace(); } // 設置新的URI ctx.put(FilterConstants.REQUEST_URI_KEY, "group1/M00/00/03/wKgQSVzHshWAdSpoAAAFsn6csJI682.png"); return null; } @Override public String filterType() { return FilterConstants.ROUTE_TYPE; } @Override public int filterOrder() { return 1; } }
修改響應頭:ide
package com.example.demo; import javax.servlet.http.HttpServletResponse; import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; /** * 根據您條件修改響應頭 * @author mxf * */ @Component public class ResponseFilter extends ZuulFilter{ /** * 是否執行該過濾器 */ @Override public boolean shouldFilter() { RequestContext currentContext = RequestContext.getCurrentContext(); HttpServletResponse response = currentContext.getResponse(); String sign = response.getHeader("sign"); String info = response.getHeader("info"); String info_size = response.getHeader("info_size"); if (!StringUtils.isEmpty(sign) && !StringUtils.isEmpty(info) && !StringUtils.isEmpty(info_size)) { return true; } return false; } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletResponse response = ctx.getResponse(); String info = response.getHeader("info"); String info_size = response.getHeader("info_size"); /** * 設置響應頭,使請求變爲文件下載 */ ctx.addZuulResponseHeader("Content-Type", "application/octet-stream"); ctx.addZuulResponseHeader("Content-Disposition", "attachment;fileName=" + info); ctx.addZuulResponseHeader("Content-Length", ""+info_size); return null; } @Override public String filterType() { return FilterConstants.POST_TYPE; } @Override public int filterOrder() { return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 2; } }
從網上找了不少資料,沒有相關的信息。因此想記錄一下。.net