「javaweb读取文件」javaweb 下载文件

博主:adminadmin 2022-11-28 11:51:06 52

今天给各位分享javaweb读取文件的知识,其中也会对javaweb 下载文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎么获取web资源文件

1、一般工程中使用I/O类指定文件的绝对路径读取

FileInputStream

fis

=

new

FileInputStream("src/main/resources/zsm.properties");

ppt.load(fis);

String

memAddr1

=

ppt.getProperty("memAddr1");

2、Web工程中可以使用ServletContext或ClassLoader来读取

2.1、通过ServletContext来读取资源文件,文件路径是相对于web项目(如/JspServletFeature)根路径而言的。

2.2、通过ClassLoader来读取,文件路径是相对于类目录而言的(maven工程中一般为/target/classes)

示例如下

(1)文件位置

放在src目录(或其子目录)下是相对于项目根目录如JspServletFeature的路径

放在JavaResources下是相对于类目录即classes的目录

(2)代码

//

使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature)

out.println("\n使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature):");

readFileByServletContext(response,

"FileReadFile1.properties");

readFileByServletContext(response,

"/FileReadFile1.properties");

readFileByServletContext(response,

"WEB-INF/classes/FileReadFile2.properties");

readFileByServletContext(response,

"/WEB-INF/classes/FileReadFile2.properties");

java web工程中读取properties文件,路径一直不知道怎么写

1. 使用java.lang.Class类的getResourceAsStream(String name)方法

InputStream in = getClass().getResourceAsStream("/config.properties");

在静态方法中,由于不能使用getClass()方法,必须写出类的名字。区别不大。

MyClass.class.getResourceAsStream("/config.properties");

使用这个方法,路径前面可以加斜杠也可以不加。根据Class类getResourceAsStream()方法的JavaDoc:

Finds a resource with a given name.  The rules for searching resources associated with a given class are implemented by the defining  class loader of the class.  This method delegates to this object's class loader.  If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form:

modified_package_name/name

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

就是说,这个path假如以斜杠开头,则斜杠后面的部分是文件相对classpath的路径;假如不是,Java会把这个path看作是“包名/文件名”的结构,会尝试在这个类的包里面去找,而不是从classpath开始找;在这种情况下,除非你把properties文件放到MyClass.class所属的包里面,不然都会是null的。

2. 使用java.lang.ClassLoader类的getResourceAsStream(String name)方法

路径是不能加斜杠的!非常重要。

MyClass.class.getClassLoader().getResourceAsStream("config.properties");

这是因为使用classloader进行读取,所输入的参数必须是一个相对classpath的绝对路径,在格式上,一个绝对路径是不能以'/'开头的。

注意这两个方法是同名的,但路径参数的格式截然不同。

3. 在Maven中的运用

现在几乎所有的web project都是maven project,Maven的默认设置是把

src/main/resources/

加入到classpath里面的。那么,最好的做法是把你的properties文件放进src/main/resources里面,然后用上面代码读取。用Class类的,一般要加斜杠;用ClassLoader类的,绝不能加斜杠!

假如是Eclipse里面,需要把这个src/main/resources加到classpath里面。具体操作是右击工程,选择“Configure buildpath”,根据Maven的要求,把src/main/java和src/main/resources都加进去,并且保证Exclude是none,Include是all,或者至少要包括你需要读取的文件。

java web中读取文件,相对路径怎么写

相对路径的话,可以先获取到当前文件的编译路径,之后在找到想找文件的路径的思路来实现。

举例:

XMLS.class.getClass().getResourceAsStream("/test/test.txt");

解释:XMLS.class.getClass()是获取当前的类编译路径,之后通过getResourceAsStream的形式即可找到要读取的文件的路径。

备注:这个方法中后面的路径也可以通过截取的形式来进行路径获取,实现原理都是找到当前类路径,之后通过相对位置找到另外文件路径。

javaweb读取文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于javaweb 下载文件、javaweb读取文件的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。