首页 技术 正文
技术 2022年11月14日
0 收藏 709 点赞 2,926 浏览 2907 个字

写了一个工具类,将上传文件功能保存文件的目录移到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”/>

这样就可以直接通过https://img.zhankr.net/41mnahdcpps141557.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;
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,429
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,245
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,689
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726