首页 > 编程知识 正文

java百度网盘资源,java读取资源文件

时间:2023-05-06 20:12:38 阅读:114115 作者:2572

另一方面,java取得资源下的文件路径的项目结构

resources下的文件名最好是英文名称。 getPath ) )时,中文名称会乱码。

1.Thread方式Thread获取资源下的文件路径

string path1=thread.current thread (.getcontextclassloader ).getresource ) ' mapper ' ).getPath ); string path2=thread.current thread (.getcontextclassloader ).get resource (' mapper/student mapper.XML ' ).get pper 由于classes/mapper//d 3360/web test2/student/student/target/d 3360 *打包时间文件夹忽略它,因此在此示例中,它在target包中被忽略*//可以看到system.out.println (thread.current thread (.getcontextclassloader ).getresourer )2. ClassPathResource方法cler

string path3=newclasspathresource (word/title.txt ).getPath ); //inputstream 3360 Java.io.buffered inputstream @ 48eb 528 b,path:word/title.txt, URL :文件:/d :/web test2/student/student/target/classes/word/title.txt/* class path resource空文件夹3 .其他方式/** * ClassLoader方式* idea项目的启动即使是可以获取路径的jar包也无法获取路径,存在null异常。 */string mapper=class loader.getsystemclasloader ().GGG string mapper2=class loader.getsystemclassloader ().get ressoader/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */file file=resource utils.getfile (class path 3360 wwile system.out.println (' file : ' file.getname )、path:' file.getPath )和url:' file.toURI ) )/* * * * resources.class.getclass loader (.get resource (' mapper/student mapper.XML ' ).getPath ); 二、java获取资源下的文件信息idea生成jar包。 war包可以引用另一个springboot项目的父子多模块包

1 .用1.idea读取文件

在idea项目中,可以通过getPath ()获取的路径访问File文件。

string mapper=thread.current thread (.getcontextclassloader ).get resource (' mapper/student mapper.XML ' ).gettml 文件文件=new file (映射器; 2 .从jar包读取文件

即使项目在多模块mvn package中构建为jar包,也可以通过getResource“”获取资源下的文件路径,但通过路径getFile“”获取资源文件在IDEA中进行调试时,文件实际存在于磁盘所在的路径中,可以正常读取,但jar表示文件是存在于jar文件中的资源文件,磁盘没有真实路径,因此无法通过正确的做法是通过流的方式

//推荐的两种方式//Thread方式示例getresourceasstream (' word/title.txt ); publicfinalstaticinputstreamstream1=thread.current thread ().getContextClassLoader ).getresourceasstream (' resour clarent ClassPathResource方式publicstaticfinalclasspathresourcestream2=newclasspathresource (' resources下的文件路径'); 去读取资源下的txt文件中的信息。 使用getResourceAsStream ' '检索文件流,并从流中读取每行的数据。读取resources下txt文件中的数据

@getmapping(value='/2 ',name='读取txt文件中的信息。 idea、jar均成功')公共语音测试2 (throwsioexception )列表列表=new ArrayList ); 缓冲读取器读取器=null; inputstream inputstream=test constant.stream2. get inputstream (; try { String s=null; /** * jar包的读取文件中有中文乱码。 * newbufferedreader (newinputstreamreader (stream 2,' UTF-8 ' ) ); 直接转码*/reader=newbufferedreader (newinputstreamreader (inputstream,' UTF-8 ' ) ); wile((s=reader.readline ) )!=null () lists.add ) ) s; }catch(ioexceptione ) { e.printStackTrace ); }finally{try}if(reader!=null(reader.close ); }catch(ioexceptione ) { e.printStackTrace ); } lists.foreach (item-system.out.println ) item ); 下载resources下的txt文件,在getResourceAsStream (“”)中获取文件流,并获取数据读取resources下txt文件进行下载

publicfinalstaticinputstreamstream1=thread.current thread ().getContextClassLoader ).getresourceasstream ) ' word/word/classloader ).getresourceam @ get mapping (value='/1 ',name='以流格式读取并输出resources文件(下载)。 idea、jar方式均可成功' (public void test1 (httpservletresponseresponse ) throwsexception ) output stream out=response.getoutputpure byte[] bytes=new byte[1024]; while ((len=test constant.stream1. read ) bytes )!=-1 ) out.write(bytes,0,len ); } TestConstant.STREAM1.close (; out.flush (; out.close (; }3.从war包读取文件

项目将成为war包并在tomcat中启动。 读取文件路径、文件信息与jar打包方式一致。

注意事项:

war包必须位于ROOT之外,并为请求路径加前缀。 main.war,请求路径为localhost:8888/main/接口

war包启动后,启动文件(META-INF、WEB-INF、org )不需要为ROOT中的请求路径加前缀。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。