zoukankan      html  css  js  c++  java
  • tomcat动态映射路径

    写了一个工具类,将上传文件功能保存文件的目录移到webapps目录外面,通过动态生成xml映射文件到tomcat\conf\Catalina\localhost目录下从而实现目录映射。可以被http直接访问的文件直接映射,不能被直接访问的通过输入输出流读取。

    files.xml文件内容(ps:xml文件名需和path配置的目录名称一样):

    <?xml version="1.0" encoding="utf-8"?>
    <Context docBase="/E:/apache-tomcat-7.0.61/data/files" path="/files" reloadable="true"/>

    这样就可以直接通过http://files/xxx.png访问图片。而/files只是一个映射的路径,可以映射到系统中任何路径.

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URL;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    
    public class FileUtils {
    
    	/**
    	 * 通过class获取本身所在目录的URL
    	 */
    	private static URL url = FileUtils.class.getResource("/");
    	/**
    	 * 获取项目绝对路径
    	 */
    	public static String getProjectPath() {
    		String path = url.getPath().split("/WEB-INF")[0];
    		return path;
    	}
    	
    	/**
    	 * 获取tomcat绝对路径
    	 * @return
    	 */
    	public static String getTomcatPath() {
    		String path = getProjectPath();
    		path = path.substring(0, path.lastIndexOf("/"));
    		path = path.substring(0, path.lastIndexOf("/"));
    		return path;
    	}
    	
    	/**
    	 * 获取Webapps绝对路径
    	 * @return
    	 */
    	public static String getWebappsPath() {
    		String path = getProjectPath();
    		path = path.substring(0, path.lastIndexOf("/"));
    		return path;
    	}
    	
    	/**
    	 * 获取data绝对路径
    	 * @return
    	 */
    	public static String getDataPath() {
    		return getTomcatPath() + "/data";
    	}
    	
    	/**
    	 * 创建虚拟目录的方法--直接设置映射的路径和映射路径
    	 * @param docBase 被映射的真实路径
    	 * @param path 映射的路径
    	 * @throws IOException
    	 * @throws JDOMException
    	 */
    	public static void setUrlPattern(String docBase, String path) throws IOException {
    		String filesPath = docBase + path;
    		File file = new File(filesPath);
    		if (!file.exists()) {
    			file.mkdirs();
    		}
    		Element root = DocumentHelper.createElement("Context");
    		Document document = DocumentHelper.createDocument(root);
    		root.addAttribute("docBase", filesPath)
    		.addAttribute("path", path)
    		.addAttribute("reloadable", "true");
    		// 把生成的xml文档存放在硬盘上 true代表是否换行
    		OutputFormat format = new OutputFormat();
    		format.setEncoding("utf-8");
    		StringBuilder savePath = new StringBuilder(getTomcatPath() + "/conf/Catalina/localhost/" + path + ".xml");
    		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(savePath.toString()), format);
    		xmlWriter.write(document);
    		xmlWriter.close();
    	}
    	
    	/**
    	 * 创建虚拟目录的方法--直接设置映射的路径,默认映射为files
    	 * @param docBase 被映射的真实路径,映射的路径默认是files
    	 * @throws IOException
    	 * @throws JDOMException
    	 */
    	public static void setUrlPattern(String path) throws IOException {
    		setUrlPattern(getDataPath(), path);
    	}
    	/**
    	 * 创建虚拟目录的方法--直接设置映射的路径,默认映射为files
    	 * @param docBase 被映射的真实路径默认<tomcat目录>/data/files目录,映射的路径默认是files
    	 * @throws IOException
    	 * @throws JDOMException
    	 */
    	public static void setUrlPattern() throws IOException {
    		setUrlPattern(getDataPath(), "files");
    	}
    	
    	public static boolean isUrlPatternExist(String path) {
    		StringBuilder savePath = new StringBuilder(getTomcatPath() + "/conf/Catalina/localhost/" + path + ".xml");
    		File file = new File(savePath.toString());
    		if (file.exists()) {
    			return true;
    		}
    		return false;
    	}
    	
    	public static boolean isUrlPatternExist() {
    		StringBuilder savePath = new StringBuilder(getTomcatPath() + "/conf/Catalina/localhost/files.xml");
    		File file = new File(savePath.toString());
    		if (file.exists()) {
    			return true;
    		}
    		return false;
    	}
    	
    }
    
  • 相关阅读:
    Flink Table环境配置、读取外部数据(File、Kafka)以及查询转换
    Flink之Table初探
    Flink之Watermarks
    Flink之ProcessFunction侧输出流
    Flink之ProcessFunction案例
    Flink之Mysql数据CDC
    Express ejs 模板做的 app.js 文件
    金额转换文章(100=>零佰)
    将 音频流(MP3流)并进行播放
    浅谈MySQL(一):存储引擎与索引
  • 原文地址:https://www.cnblogs.com/guxingzhe/p/5063695.html
Copyright © 2011-2022 走看看