📄 webresourceservlet.java
字号:
package com.esri.solutions.jitk.web.util;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
/**
* Reads web resources (images, javascript, and stylesheets) from the
* classpath and writes them to the Output Stream to be sent to the client.
* This servlet looks to the PathInfo property on the request to get the resource
* to retrieve from the classpath. The format will be <package>/<package>.../<resource>.
*
* <p>
* This servlet only allows for certain types of web resources to be
* retrieved from the classpath. The types can be configured as init-params
* within the Web Deployment Descriptor. The param-name is the file-extension
* in uppercase and the param-value is the MIME type. Example:
*
* <init-param>
* <param-name>JPG</param-name>
* <param-value>image/jpg</param-value>
* </init-param>
* </p>
*
* <p>
* If a request is received for a resource type that is not included in
* the init-params then an SC_NOT_ACCEPTABLE is returned.
* </p>
*
* <p>
* If a request is received for a resource that cannot be located, then
* an SC_NOT_FOUND is returned.
* </p>
*
* <p>
* If the request is not formed correctly, then an SC_BAD_REQUEST is
* returned.
* </p>
*
*/
public class WebResourceServlet extends javax.servlet.http.HttpServlet {
private static final long serialVersionUID = 1L;
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String resPath = null;
String mimeType = null;
resPath = request.getPathInfo();
if (resPath != null) {
InputStream in = this.getClass().getResourceAsStream(resPath);
if (in == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String ext = FilenameUtils.getExtension(resPath);
mimeType = this.getInitParameter(ext.toUpperCase());
if (mimeType == null) {
response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
return;
}
response.setContentType(mimeType);
IOUtils.copy(in, response.getOutputStream());
}
else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -