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只是一个映射的路径,可以映射到系统中任何路径.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 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 ; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步