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;
	}
	
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM