當前位置: 首頁>>代碼示例>>Java>>正文


Java ServletContext.getMimeType方法代碼示例

本文整理匯總了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);
}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:43,代碼來源:SSIServlet.java

示例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");  	   
	    }	    
}
 
開發者ID:stppy,項目名稱:spr,代碼行數:57,代碼來源:DownloadServlet.java

示例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);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:43,代碼來源:SSIServlet.java

示例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);
}
 
開發者ID:how2j,項目名稱:lazycat,代碼行數:40,代碼來源:SSIServlet.java

示例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");  	   
	    }	    
}
 
開發者ID:stppy,項目名稱:tcp,代碼行數:57,代碼來源:DownloadServlet.java


注:本文中的javax.servlet.ServletContext.getMimeType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。