当前位置:首页 » 编程语言 » 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);

热点内容
aspphp环境 发布:2025-03-07 02:40:38 浏览:382
c语言汉字转拼音 发布:2025-03-07 02:26:05 浏览:557
磁盘与数据库 发布:2025-03-07 02:19:54 浏览:561
微信的缓存是什么 发布:2025-03-07 02:15:17 浏览:995
sql添加表数据 发布:2025-03-07 02:15:16 浏览:593
其他台式电脑怎么登录服务器 发布:2025-03-07 02:09:45 浏览:106
数控车床g76编程实例 发布:2025-03-07 02:07:43 浏览:662
魔兽世界新服务器是什么意思 发布:2025-03-07 02:07:41 浏览:619
ftp传输二进制 发布:2025-03-07 01:57:26 浏览:286
php网上商城源码 发布:2025-03-07 01:57:20 浏览:707