Tomcat Dynamic Mapping Path

Source: Internet
Author: User

Write a tool class, the upload file function to save the file directory to the outside of the WebApps directory, through the dynamic generation of XML mapping files to the Tomcat\conf\catalina\localhost directory to achieve directory mapping. Files that can be directly accessed by HTTP are mapped directly and cannot be read directly by the input/output stream.

Files.xml file contents (the Ps:xml filename needs to be the same as the directory name of the path configuration):

<?xml version= "1.0" encoding= "Utf-8"?>
<context docbase= "/e:/apache-tomcat-7.0.61/data/files" path= "/files" reloadable= "true"/>

This allows you to access the image directly from Http://files/xxx.png. And/files is just a mapped path that can be mapped to any path in the system.

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 {/** * Gets the URL of its own directory through class */private static URL url = Fileutils.class . GetResource ("/");/** * Gets the project absolute path */public static string GetProjectPath () {String path = Url.getpath (). Split ("/web-inf") [ 0];return path;} /** * Get Tomcat absolute path * @return */public static string Gettomcatpath () {String path = GetProjectPath ();p ath = path.substring ( 0, Path.lastindexof ("/"));p ath = path.substring (0, Path.lastindexof ("/")); return path;} /** * Get WebApps absolute path * @return */public static string Getwebappspath () {String path = GetProjectPath ();p ath = Path.substrin G (0, Path.lastindexof ("/")); return path;} /** * Get Data absolute path * @return */public static String Getdatapath () {return Gettomcatpath () + "/data";} /** * methods for creating virtual directories-direct mapping path and mapping path * @param docBase True Path mappedPaths * @param path map * @throws IOException * @throws jdomexception */public static void Seturlpattern (String docBase, Str ing path) throws IOException {String Filespath = docBase + path; File File = new file (Filespath), if (!file.exists ()) {file.mkdirs ();} Element root = Documenthelper.createelement ("Context");D ocument Document = documenthelper.createdocument (root); Root.addattribute ("DocBase", Filespath). AddAttribute ("path", Path). AddAttribute ("Reloadable", "true");// Storing the generated XML document on the hard disk true indicates whether the newline 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 ();} /** * Method of creating a virtual directory--directly set the path to the map, the default mapping is files * @param docBase The true path mapped, the mapped path defaults to files * @throws IOException * @throws jdomexceptio n */public static void Seturlpattern (String path) throws IOException {SeturLpattern (Getdatapath (), path);} /** * method of creating virtual directory--Direct mapping path, default map to files * @param docBase mapped true path default <tomcat directory >/data/files directory, mapped path by default is 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;}}

Tomcat Dynamic Mapping Path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.