本文整理汇总了Java中javax.servlet.ServletContext.getMimeType方法的典型用法代码示例。如果您正苦于以下问题:Java ServletContext.getMimeType方法的具体用法?Java ServletContext.getMimeType怎么用?Java ServletContext.getMimeType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletContext
的用法示例。
在下文中一共展示了ServletContext.getMimeType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requestHandler
import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
* Process our request and locate right SSI command.
*
* @param req
* a value of type 'HttpServletRequest'
* @param res
* a value of type 'HttpServletResponse'
*/
protected void requestHandler(HttpServletRequest req,
HttpServletResponse res) throws IOException {
ServletContext servletContext = getServletContext();
String path = SSIServletRequestUtil.getRelativePath(req);
if (debug > 0)
log("SSIServlet.requestHandler()\n" + "Serving "
+ (buffered?"buffered ":"unbuffered ") + "resource '"
+ path + "'");
// Exclude any resource in the /WEB-INF and /META-INF subdirectories
// (the "toUpperCase()" avoids problems on Windows systems)
if (path == null || path.toUpperCase(Locale.ENGLISH).startsWith("/WEB-INF")
|| path.toUpperCase(Locale.ENGLISH).startsWith("/META-INF")) {
res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
log("Can't serve file: " + path);
return;
}
URL resource = servletContext.getResource(path);
if (resource == null) {
res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
log("Can't find file: " + path);
return;
}
String resourceMimeType = servletContext.getMimeType(path);
if (resourceMimeType == null) {
resourceMimeType = "text/html";
}
res.setContentType(resourceMimeType + ";charset=" + outputEncoding);
if (expires != null) {
res.setDateHeader("Expires", (new java.util.Date()).getTime()
+ expires.longValue() * 1000);
}
req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
processSSI(req, res, resource);
}
示例2: doGet
import javax.servlet.ServletContext; //导入方法依赖的package包/类
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String fileName = getFilename(request.getParameter("urlDocumento").toString());
//extrae la extension y el nombre de archivo por separado
int dot = fileName.lastIndexOf(".");
String fileNameExt = fileName.substring(dot);
fileName = fileName.substring(0, dot);
//remplaza cualquier caracter del tipo espacio, puntos y otros por "".
fileName = fileName.replaceAll("\\W","");
// lee el archivo del path absoluto
String filePath = getServletContext().getInitParameter("file-upload") + File.separator + fileName + fileNameExt;
try{
File downloadFile = new File(filePath);
FileInputStream inStream = new FileInputStream(downloadFile);
// obtiene ServletContext
ServletContext context = getServletContext();
// obtiene el tipo de MIME
String mimeType = context.getMimeType(filePath);
if (mimeType == null) {
// setea en tipo binario el MIME cuando el tomcat no identifica el tipo de aplicación.
mimeType = "application/octet-stream";
}
System.out.println("MIME type: " + mimeType);
// modifica la respuesta
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());
// fuerza la descarga
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
response.setHeader(headerKey, headerValue);
// obtiene la respuesta en output stream
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[15360];// 15 MB MaxFileSize del UploadServlet
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inStream.close();
outStream.close();
} catch (FileNotFoundException fnfe){
response.sendRedirect("/archivo_no_encontrado.jsp");
}
}
示例3: requestHandler
import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
* Process our request and locate right SSI command.
*
* @param req
* a value of type 'HttpServletRequest'
* @param res
* a value of type 'HttpServletResponse'
*/
protected void requestHandler(HttpServletRequest req,
HttpServletResponse res) throws IOException, ServletException {
ServletContext servletContext = getServletContext();
String path = SSIServletRequestUtil.getRelativePath(req);
if (debug > 0)
log("SSIServlet.requestHandler()\n" + "Serving "
+ (buffered?"buffered ":"unbuffered ") + "resource '"
+ path + "'");
// Exclude any resource in the /WEB-INF and /META-INF subdirectories
// (the "toUpperCase()" avoids problems on Windows systems)
if (path == null || path.toUpperCase().startsWith("/WEB-INF")
|| path.toUpperCase().startsWith("/META-INF")) {
res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
log("Can't serve file: " + path);
return;
}
URL resource = servletContext.getResource(path);
if (resource == null) {
res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
log("Can't find file: " + path);
return;
}
String resourceMimeType = servletContext.getMimeType(path);
if (resourceMimeType == null) {
resourceMimeType = "text/html";
}
res.setContentType(resourceMimeType + ";charset=" + outputEncoding);
if (expires != null) {
res.setDateHeader("Expires", (new java.util.Date()).getTime()
+ expires.longValue() * 1000);
}
req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
processSSI(req, res, resource);
}
示例4: requestHandler
import javax.servlet.ServletContext; //导入方法依赖的package包/类
/**
* Process our request and locate right SSI command.
*
* @param req
* a value of type 'HttpServletRequest'
* @param res
* a value of type 'HttpServletResponse'
*/
protected void requestHandler(HttpServletRequest req, HttpServletResponse res) throws IOException {
ServletContext servletContext = getServletContext();
String path = SSIServletRequestUtil.getRelativePath(req);
if (debug > 0)
log("SSIServlet.requestHandler()\n" + "Serving " + (buffered ? "buffered " : "unbuffered ") + "resource '"
+ path + "'");
// Exclude any resource in the /WEB-INF and /META-INF subdirectories
// (the "toUpperCase()" avoids problems on Windows systems)
if (path == null || path.toUpperCase(Locale.ENGLISH).startsWith("/WEB-INF")
|| path.toUpperCase(Locale.ENGLISH).startsWith("/META-INF")) {
res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
log("Can't serve file: " + path);
return;
}
URL resource = servletContext.getResource(path);
if (resource == null) {
res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
log("Can't find file: " + path);
return;
}
String resourceMimeType = servletContext.getMimeType(path);
if (resourceMimeType == null) {
resourceMimeType = "text/html";
}
res.setContentType(resourceMimeType + ";charset=" + outputEncoding);
if (expires != null) {
res.setDateHeader("Expires", (new java.util.Date()).getTime() + expires.longValue() * 1000);
}
req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
processSSI(req, res, resource);
}
示例5: doGet
import javax.servlet.ServletContext; //导入方法依赖的package包/类
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String fileName = getFilename(request.getParameter("urlDocumento").toString());
//extrae la extension y el nombre de archivo por separado
int dot = fileName.lastIndexOf(".");
String fileNameExt = fileName.substring(dot);
fileName = fileName.substring(0, dot);
//remplaza cualquier caracter del tipo espacio, puntos y otros por "".
fileName = fileName.replaceAll("\\W","");
// lee el archivo del path absoluto
String filePath = getServletContext().getInitParameter("file-upload") + File.separator + fileName + fileNameExt;
try{
File downloadFile = new File(filePath);
FileInputStream inStream = new FileInputStream(downloadFile);
// obtiene ServletContext
ServletContext context = getServletContext();
// obtiene el tipo de MIME
String mimeType = context.getMimeType(filePath);
if (mimeType == null) {
// setea en tipo binario el MIME cuando el tomcat no identifica el tipo de aplicación.
mimeType = "application/octet-stream";
}
System.out.println("MIME type: " + mimeType);
// modifica la respuesta
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());
// fuerza la descarga
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
response.setHeader(headerKey, headerValue);
// obtiene la respuesta en output stream
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[15360];// 15 MB MaxFileSize del UploadServlet
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inStream.close();
outStream.close();
} catch (FileNotFoundException fnfe){
response.sendRedirect("/tablero/archivo_no_encontrado.jsp");
}
}