getResourceAsStream 输出url地址
时间: 2024-04-28 12:24:47 浏览: 76
您可以使用以下代码将 `getResourceAsStream` 方法返回的 `InputStream` 对象转换为 URL 地址字符串:
```java
InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("pdf/accountExpenseNotice.pdf");
URL url = Thread.currentThread().getContextClassLoader().getResource("pdf/accountExpenseNotice.pdf");
String urlString = url != null ? url.toString() : null;
System.out.println("URL地址: " + urlString);
```
这样,您就可以将 URL 地址打印输出或进行其他处理了。请注意,如果资源不存在或无法访问,`urlString` 可能为 `null`。
相关问题
(6)新建一个类名为DemoServletContext02的Servlet,该Servlet路径映射为/context02 (7)分别在这两个Servlet中获取web应用上下文对象ServletContext并打印出来看它们是否是同一个对象 (8)给web应用配置初始化参数:{"loginid":"tom","loginpwd":"123"},并分别在上面两个Servlet中获取它们 (9)在DemoServletContext01中将键值对{"msg":"登录成功!"}存入ServletContext域对象中,在DemoServletContext02去获取上面这个键值对并输出 (10)分别在src、web及WEB-INF目录下新建三个配置文件jdbc.properties,文件内容分别为: jdbc01.driver=com.mysql.jdbc.Driver jdbc01.url=jdbc:mysql:///localhost/jdbc jdbc01.username=root jdbc01.password=123 jdbc02.driver=com.mysql.jdbc.Driver jdbc02.url=jdbc:mysql:///localhost/jdbc jdbc02.username=root jdbc02.password=123 jdbc03.driver=com.mysql.jdbc.Driver jdbc03.url=jdbc:mysql:///localhost/jdbc jdbc03.username=root jdbc03.password=123 (11)分别获取上面三个文件夹中的文件并输出其内容
6. 新建一个类名为 DemoServletContext02 的 Servlet,该 Servlet 路径映射为 /context02。
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("DemoServletContext02 context: " + context);
}
}
```
在 web.xml 中设置 Servlet 路径映射:
```xml
<servlet>
<servlet-name>DemoServletContext02</servlet-name>
<servlet-class>DemoServletContext02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServletContext02</servlet-name>
<url-pattern>/context02</url-pattern>
</servlet-mapping>
```
7. 分别在这两个 Servlet 中获取 web 应用上下文对象 ServletContext 并打印出来看它们是否是同一个对象
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("DemoServletContext01 context: " + context);
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("DemoServletContext02 context: " + context);
}
}
```
在控制台中输出的结果应该是一样的,因为它们获取的是同一个 web 应用上下文对象。
8. 给 web 应用配置初始化参数:{"loginid":"tom","loginpwd":"123"},并分别在上面两个 Servlet 中获取它们。
在 web.xml 中添加以下配置:
```xml
<context-param>
<param-name>loginid</param-name>
<param-value>tom</param-value>
</context-param>
<context-param>
<param-name>loginpwd</param-name>
<param-value>123</param-value>
</context-param>
```
在 DemoServletContext01 和 DemoServletContext02 中获取这些初始化参数:
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String loginid = context.getInitParameter("loginid");
String loginpwd = context.getInitParameter("loginpwd");
System.out.println("DemoServletContext01 loginid: " + loginid);
System.out.println("DemoServletContext01 loginpwd: " + loginpwd);
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String loginid = context.getInitParameter("loginid");
String loginpwd = context.getInitParameter("loginpwd");
System.out.println("DemoServletContext02 loginid: " + loginid);
System.out.println("DemoServletContext02 loginpwd: " + loginpwd);
}
}
```
9. 在 DemoServletContext01 中将键值对 {"msg":"登录成功!"} 存入 ServletContext 域对象中,在 DemoServletContext02 去获取上面这个键值对并输出。
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
context.setAttribute("msg", "登录成功!");
System.out.println("DemoServletContext01: 键值对 {"msg":"登录成功!"} 已存入 ServletContext 域对象中。");
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String msg = (String) context.getAttribute("msg");
System.out.println("DemoServletContext02: " + msg);
}
}
```
10. 分别在 src、web 及 WEB-INF 目录下新建三个配置文件 jdbc.properties,文件内容分别为:
jdbc01.driver=com.mysql.jdbc.Driver
jdbc01.url=jdbc:mysql:///localhost/jdbc
jdbc01.username=root
jdbc01.password=123
jdbc02.driver=com.mysql.jdbc.Driver
jdbc02.url=jdbc:mysql:///localhost/jdbc
jdbc02.username=root
jdbc02.password=123
jdbc03.driver=com.mysql.jdbc.Driver
jdbc03.url=jdbc:mysql:///localhost/jdbc
jdbc03.username=root
jdbc03.password=123
11. 分别获取上面三个文件夹中的文件并输出其内容。
```java
import java.io.InputStream;
import java.util.Properties;
public class DemoProperties {
public static void main(String[] args) {
// 读取 src 目录下的 jdbc.properties 文件
InputStream in1 = DemoProperties.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties prop1 = new Properties();
try {
prop1.load(in1);
System.out.println("jdbc01.driver: " + prop1.getProperty("jdbc01.driver"));
System.out.println("jdbc01.url: " + prop1.getProperty("jdbc01.url"));
System.out.println("jdbc01.username: " + prop1.getProperty("jdbc01.username"));
System.out.println("jdbc01.password: " + prop1.getProperty("jdbc01.password"));
} catch (IOException e) {
e.printStackTrace();
}
// 读取 web 目录下的 jdbc.properties 文件
InputStream in2 = DemoProperties.class.getClassLoader().getResourceAsStream("../jdbc.properties");
Properties prop2 = new Properties();
try {
prop2.load(in2);
System.out.println("jdbc02.driver: " + prop2.getProperty("jdbc02.driver"));
System.out.println("jdbc02.url: " + prop2.getProperty("jdbc02.url"));
System.out.println("jdbc02.username: " + prop2.getProperty("jdbc02.username"));
System.out.println("jdbc02.password: " + prop2.getProperty("jdbc02.password"));
} catch (IOException e) {
e.printStackTrace();
}
// 读取 WEB-INF 目录下的 jdbc.properties 文件
InputStream in3 = DemoProperties.class.getClassLoader().getResourceAsStream("../../jdbc.properties");
Properties prop3 = new Properties();
try {
prop3.load(in3);
System.out.println("jdbc03.driver: " + prop3.getProperty("jdbc03.driver"));
System.out.println("jdbc03.url: " + prop3.getProperty("jdbc03.url"));
System.out.println("jdbc03.username: " + prop3.getProperty("jdbc03.username"));
System.out.println("jdbc03.password: " + prop3.getProperty("jdbc03.password"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
有如下的配置文件:confifig.properties @Info(name="卡卡西" ,tags={"复制忍者" , "六代火影"}) @Organization(name="木叶村" ,size=556) public class Ex1{ } version=1.0 appName=注解的应用案例 id=123456789 title=java从入门到入土 size=120 druid.driverClassName=com.mysql.cj.jdbc.Driver druid.url=jdbc:mysql://localhost:3306/mybatis druid.username=root druid.password=root123 有如下的类:public class BaseDAO{ @Value(name="druid.driverClassName") private static className; @Value(name="druid.url") private static url; @Value(name="druid.username") private static username; @Value(name="druid.password") private static password; static{ // 在静态块中参考课堂的案例,将配置文件中对应 的属性值绑定到上面的四个静态变量中。 } public static void showInfo(){ // 输出上面4个静态成员变量的值 } 根据题目意思定义一个Value注解。 完成BaseDAO中的静态块中的代码和showInfo方法中的代 码编写。
定义Value注解如下:
```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Value {
String name() default "";
}
```
完整的BaseDAO代码如下:
```java
public class BaseDAO {
@Value(name="druid.driverClassName")
private static String className;
@Value(name="druid.url")
private static String url;
@Value(name="druid.username")
private static String username;
@Value(name="druid.password")
private static String password;
static{
try(InputStream input = BaseDAO.class.getClassLoader().getResourceAsStream("config.properties")){
Properties prop = new Properties();
prop.load(input);
className = prop.getProperty("druid.driverClassName");
url = prop.getProperty("druid.url");
username = prop.getProperty("druid.username");
password = prop.getProperty("druid.password");
} catch(IOException e){
e.printStackTrace();
}
}
public static void showInfo(){
System.out.println("driverClassName: " + className);
System.out.println("url: " + url);
System.out.println("username: " + username);
System.out.println("password: " + password);
}
}
```
静态块中的代码使用了Properties类来读取配置文件中的信息,然后将其绑定到静态变量中。showInfo方法可以输出这些静态变量的值。
阅读全文