当前位置:首页 » 编程语言 » java获取服务器路径

java获取服务器路径

发布时间: 2023-09-24 14:32:34

1. java 怎么获取服务器webroot的路径

使用JAVA后台代码取得WEBROOT物理路径,可以有如下两种方式:
1、使用JSP Servlet取得WEB根路径可以用request.getContextPath(),相对路径request.getSession().getServletContext().getRealPath("/"),它们可以使用我们很容易取得根路径。

2、如果使用了spring, 在WEB-INF/web.xml中,创建一个webAppRootKey的param,指定一个值(默认为webapp.root)作为键值,然后通过Listener,或者Filter,或者Servlet执行String webAppRootKey = getServletContext().getRealPath("/"); 并将webAppRootKey对应的webapp.root分别作为Key,Value写到System Properties系统属性中。之后在程序中通过System.getProperty("webapp.root")来获得WebRoot的物理路径。
具体示例代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>csc2.root</param-value>
</context-param>
<listener>
<listener-class>test.ApplicationListener</listener-class>
</listener>
</web-app>

ApplicationListener.java
package test;
import javax.servlet.ServletContextEvent;
import org.springframework.web.context.ContextLoaderListener;
public class ApplicationListener extends ContextLoaderListener {
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
String webAppRootKey = sce.getServletContext().getRealPath("/");
System.setProperty("csc2.root" , webAppRootKey);
String path =System.getProperty("csc2.root");
System.out.println("sssss:::"+path);
}
}

test.java
public class test {
public void remve(){
String path =System.getProperty("csc2.root");
System.out.println("result::::::::"+path);
}

}

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="test.test" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
test t = new test();
t.remve();
%>
<html>
</html>
部署程序发布 启动TOMCAT 运行index.jsp 就可以调用JAVA中全局设置的物理路径了(说明这里的JSP 只是调用了TEST.JAVA 的remove方法,不做其他使用。原理解释,TOMCAT启动和读取WEB.XML 监听方式加载SPRING ApplicationListener继承SPRING ContextLoaderListener加载SPRING顺便吧全局路径赋值给csc2.root 描述,这样之后JAVA 代码中就可以使用System.getProperty("csc2.root")调用全路路径了。

2. java web怎么访问服务器指定路径

可以把文件目录配置在web.xml文件的初始化参数中, 通过ServletAPI读取文件目录

比如
定义一个Properties文件保存相关配置
#可以上传文件的后缀名
extensions=pptx,docx.doc,txt,jpg,jar
#单个文件的大小1M
fileMaxSize=1048576
#总共上传文件大小5M
totalFileMaxSize=5242880
#文件保存路径
filePath=z:/temp
#临时文件路径
tempDir=z:/temp/temp

使用Listener在服务器启动时加载配置信息

1
2
3
4
5
6
7
8
9
10
11

ServletContext context = event.getServletContext();
InputStream inputStream = context
.getResourceAsStream("/WEB-INF/classes/file/upload/commons/uploadConfig.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
context.setAttribute("fileConfig", properties);
System.out.println("properties = " + properties.size());
} catch (IOException e) {
e.printStackTrace();
}

在你上传文件时通过配置文件读取路径保存
String filePath = ((Properties) this.getServletContext().getAttribute("fileConfig"))
.getProperty(FileUploadConstants.FILE_PATH);

热点内容
脚本微信取关 发布:2025-02-01 19:35:01 浏览:150
如何用云服务器部署svn 发布:2025-02-01 19:33:20 浏览:988
缓存迅雷 发布:2025-02-01 19:31:53 浏览:974
linux与unixshell编程指南 发布:2025-02-01 19:25:03 浏览:938
护肤品数据库 发布:2025-02-01 19:25:02 浏览:647
python接受json数据 发布:2025-02-01 19:24:24 浏览:942
修改网站数据库 发布:2025-02-01 19:02:16 浏览:423
果粉不换安卓怎么办 发布:2025-02-01 18:57:21 浏览:796
网页卡需要什么配置 发布:2025-02-01 18:50:30 浏览:136
编程玩家 发布:2025-02-01 18:22:36 浏览:823