为了提高美工的工作效率,提高网站运行效率,将静态资源和代码进行了分离。结合组件化,资源就可能存在于JAR包中、主工程和资源工程三处。为了确保各处能够正确识别资源,做了一个静态资源过滤器:
public class StaticResourceFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
String strServletPath = request.getServletPath();
if(!canSolve(request, strServletPath)) {
byte[] bBuf = getBuffer(request, strServletPath);
if(bBuf!=null && bBuf.length>0) {
res.getOutputStream().write(bBuf);
res.flushBuffer();
} else {
HttpServletResponse response = (HttpServletResponse)res;
response.sendRedirect(ApplicationParameter.RESOURCE_PATH + strServletPath);
}
} else {
chain.doFilter(req, res);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
}
public void destroy() {
}
protected boolean canSolve(HttpServletRequest request, String strServletPath) {
if(strArrIgnoreServletPath != null) {
for(String strIgnoreServletPath : strArrIgnoreServletPath) {
if(strIgnoreServletPath.equalsIgnoreCase(strServletPath)) {
return true;
}
}
}
@SuppressWarnings("deprecation")
String strRealPath = request.getRealPath(strServletPath);
return new File(strRealPath).exists();
}
protected byte[] getBuffer(HttpServletRequest request, String strServletPath) throws IOException {
byte[] bArrRet = (byte[])mapCache.get(strServletPath);
if(bArrRet == null) {
File file = getFileFromOverridePath(request, strServletPath);
if(file == null) {
file = getFileFromClassPath(request, strServletPath);
}
if(file != null) {
bArrRet = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bArrRet);
fis.close();
} else {
InputStream fis = getFileFromJar(strServletPath);
if(fis != null) {
bArrRet = new byte[10240];
int iPos = 0, iCount = 0;
while ((iCount = fis.read(bArrRet, iPos, bArrRet.length-iPos)) != -1) {
iPos += iCount;
if(iPos >= bArrRet.length) {
bArrRet = Arrays.copyOf(bArrRet, bArrRet.length+10240);
}
}
fis.close();
bArrRet = Arrays.copyOf(bArrRet, iPos);
} else {
bArrRet = new byte[0];
}
}
mapCache.put(strServletPath, bArrRet);
}
return bArrRet!=null&&bArrRet.length>0 ? bArrRet : null;
}
protected File getFileFromOverridePath(HttpServletRequest request, String strServletPath) throws IOException {
String strRelativePath = strServletPath.toLowerCase().startsWith(strBasePathLocation.toLowerCase())
? strServletPath.substring(strBasePathLocation.length()) : strServletPath;
String strOverridePath = strBasePathLocation + strOverridePathPrefix
+ (strRelativePath.charAt(0)=='/' ? strRelativePath.substring(1) : strRelativePath);
File file = new File(request.getRealPath(strOverridePath));
return file.exists() ? file : null;
}
protected File getFileFromClassPath(HttpServletRequest request, String strServletPath) throws IOException {
String strClassPath = strBasePathLocation + strClassPathPrefix
+ (strServletPath.charAt(0)=='/' ? strServletPath.substring(1) : strServletPath);
File file = new File(request.getRealPath(strClassPath));
return file.exists() ? file : null;
}
protected InputStream getFileFromJar(String strServletPath) throws IOException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + strServletPath;
Resource[] resources = resourcePatternResolver.getResources(pattern);
if(resources!=null && resources.length>0) {
return resources[0].getInputStream();
}
return null;
}
private static final Map<String, Object> mapCache = new Hashtable<String, Object>();
private static final String[] strArrIgnoreServletPath = new String[] {
"/struts/utils.js",
};
private static final String strOverridePathPrefix = "static-override/";
private static final String strBasePathLocation = "/WEB-INF/";
private static final String strClassPathPrefix = "classes/";
}
比较简单,细节就不多说了。web.xml中的相关配置:
<!-- 静态资源处理 -->
<filter>
<filter-name>staticResFilter</filter-name>
<filter-class>com.ekingstar.framework.util.StaticResourceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>staticResFilter</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>staticResFilter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>staticResFilter</filter-name>
<url-pattern>*.json</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>staticResFilter</filter-name>
<url-pattern>*.jpg</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>staticResFilter</filter-name>
<url-pattern>*.png</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>staticResFilter</filter-name>
<url-pattern>*.gif</url-pattern>
</filter-mapping>