当前位置: 首页 > news >正文 重庆seo网站建设优化东莞产品展厅设计公司 news 2025/10/23 16:29:44 重庆seo网站建设优化,东莞产品展厅设计公司,网站建设项目进展情况汇报,网站数据库数据丢失个人简介#xff1a;Java领域新星创作者#xff1b;阿里云技术博主、星级博主、专家博主#xff1b;正在Java学习的路上摸爬滚打#xff0c;记录学习的过程~ 个人主页#xff1a;.29.的博客 学习社区#xff1a;进去逛一逛~ 资源操作#xff1a;Spring Resources一、Res… 个人简介Java领域新星创作者阿里云技术博主、星级博主、专家博主正在Java学习的路上摸爬滚打记录学习的过程~ 个人主页.29.的博客 学习社区进去逛一逛~ 资源操作Spring Resources一、Resource接口 Resource接口实现类⚪UrlResource⚪ClassPathResource⚪FileSystemResource⚪ServletContextResource⚪InputStreamResource⚪ByteArrayResource二、ResourceLoader 接口三、ResourceLoaderAware接口四、让Spring为Bean实例依赖注入资源建议一、Resource接口 Spring 的 Resource 接口位于 org.springframework.core.io 中。 旨在成为一个更强大的接口用于抽象对低级资源的访问。以下显示了Resource接口定义的方法 public interface Resource extends InputStreamSource {boolean exists();boolean isReadable();boolean isOpen();boolean isFile();URL getURL() throws IOException;URI getURI() throws IOException;File getFile() throws IOException;ReadableByteChannel readableChannel() throws IOException;long contentLength() throws IOException;long lastModified() throws IOException;Resource createRelative(String relativePath) throws IOException;String getFilename();String getDescription(); }其中一些重要的方法 getInputStream(): 找到并打开资源返回一个InputStream以从资源中读取。预计每次调用都会返回一个新的InputStream()调用者有责任关闭每个流exists(): 返回一个布尔值表明某个资源是否以物理形式存在isOpen: 返回一个布尔值指示此资源是否具有开放流的句柄。如果为trueInputStream就不能够多次读取只能够读取一次并且及时关闭以避免内存泄漏。对于所有常规资源实现返回false但是InputStreamResource除外。getDescription(): 返回资源的描述用来输出错误的日志。这通常是完全限定的文件名或资源的实际URL。 其他方法 isReadable(): 表明资源的目录读取是否通过getInputStream()进行读取。isFile(): 表明这个资源是否代表了一个文件系统的文件。getURL(): 返回一个URL句柄如果资源不能够被解析为URL将抛出IOExceptiongetURI(): 返回一个资源的URI句柄getFile(): 返回某个文件如果资源不能够被解析称为绝对路径将会抛出FileNotFoundExceptionlastModified(): 资源最后一次修改的时间戳createRelative(): 创建此资源的相关资源getFilename(): 资源的文件名是什么 例如最后一部分的文件名 myfile.txt Resource接口实现类 ⚪UrlResource Resource的一个实现类用来访问网络资源它支持URL的绝对路径。 http:------该前缀用于访问基于HTTP协议的网络资源。 ftp:------该前缀用于访问基于FTP协议的网络资源 file: ------该前缀用于从文件系统中读取资源 案例: import org.springframework.core.io.UrlResource; import java.io.IOException; import java.net.MalformedURLException;/*** author .29.* create 2023-03-01 9:11*/ public class urlResources {public static void loadAndReadUrlResources(String path){UrlResource url null;try {url new UrlResource(path);//获取资源名System.out.println(url.getURL());System.out.println(url.getFilename());//获取资源描述System.out.println(url.getDescription());//获取资源内容System.out.println(url.getInputStream().read());} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args){//访问网络资源测试UrlResource功能 // loadAndReadUrlResources(http://www.baidu.com);//方法二读取文件获取路径loadAndReadUrlResources(file:baidu.txt);} }注意file:前缀读取的是根路径下的类容http:前缀测试结果 file:前缀测试结果 ⚪ClassPathResource ClassPathResource 用来访问类加载路径下的资源相对于其他的 Resource 实现类其主要优势是方便访问类加载路径里的资源尤其对于 Web 应用ClassPathResource 可自动搜索位于 classes 下的资源文件无须使用绝对路径访问。 案例 import org.springframework.core.io.ClassPathResource; import java.io.IOException; import java.io.InputStream;/*** author .29.* create 2023-03-02 20:20*///测试ClassPathResource(); public class ClassPathResourceDemo {public static void main(String[] args){loadClassPathResource(baidu.txt);}public static void loadClassPathResource(String path){ClassPathResource resource new ClassPathResource(path);//获取文件信息System.out.println(resource.getFilename());System.out.println(resource.getDescription());//获取文件内容try {InputStream inputStream resource.getInputStream();byte[] bytes new byte[1024];if(inputStream.read(bytes) ! -1){System.out.println(new String(bytes));}} catch (IOException e) {e.printStackTrace();}} }⚪FileSystemResource Spring 提供的 FileSystemResource 类用于访问文件系统资源使用 FileSystemResource 来访问文件系统资源并没有太大的优势因为 Java 提供的 File 类也可用于访问文件系统资源。 案例 import org.springframework.core.io.FileSystemResource;import java.io.IOException; import java.io.InputStream;/*** author .29.* create 2023-03-02 20:32*/ public class FileSystemResourceDemo {public static void main(String[] args){loadFileSystemResource(d:\\haojin.txt); //path是绝对路径}public static void loadFileSystemResource(String path){FileSystemResource fileSystemResource new FileSystemResource(path);//获取系统资源的信息System.out.println(fileSystemResource.getFilename());System.out.println(fileSystemResource.getDescription());//获取资源内容try {InputStream inputStream fileSystemResource.getInputStream();byte[] bytes new byte[1024];if(inputStream.read(bytes) ! 1){System.out.println(new String(bytes));}} catch (IOException e) {e.printStackTrace();}} }⚪ServletContextResource 这是ServletContext资源的Resource实现它解释相关Web应用程序根目录中的相对路径。它始终支持流(stream)访问和URL访问但只有在扩展Web应用程序存档且资源实际位于文件系统上时才允许java.io.File访问。无论它是在文件系统上扩展还是直接从JAR或其他地方如数据库访问实际上都依赖于Servlet容器。 ⚪InputStreamResource InputStreamResource 是给定的输入流(InputStream)的Resource实现。它的使用场景在没有特定的资源实现的时候使用(感觉和Component 的适用场景很相似)。与其他Resource实现相比这是已打开资源的描述符。 因此它的isOpen()方法返回true。如果需要将资源描述符保留在某处或者需要多次读取流请不要使用它。 ⚪ByteArrayResource 字节数组的Resource实现类。通过给定的数组创建了一个ByteArrayInputStream。它对于从任何给定的字节数组加载内容非常有用而无需求助于单次使用的InputStreamResource。 二、ResourceLoader 接口 Spring将采用和ApplicationContext相同的策略来访问资源。也就是说如果ApplicationContext是FileSystemXmlApplicationContextres就是FileSystemResource实例如果ApplicationContext是ClassPathXmlApplicationContextres就是ClassPathResource实例 当Spring应用需要进行资源访问时实际上并不需要直接使用Resource实现类而是调用ResourceLoader实例的getResource()方法来获得资源ReosurceLoader将会负责选择Reosurce实现类也就是确定具体的资源访问策略从而将应用程序和具体的资源访问策略分离开来 案例 import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.Resource;/*** author .29.* create 2023-03-02 20:53*/ public class ResourceLoaderDemo {public static void main(String[] args){//示例一ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext();Resource resource context.getResource(baidu.txt);System.out.println(resource.getFilename());System.out.println(resource.getDescription());System.out.println(----------------------------------------------------------);//示例二FileSystemXmlApplicationContext context2 new FileSystemXmlApplicationContext();Resource resource2 context.getResource(baidu.txt);System.out.println(resource2.getFilename());System.out.println(resource2.getDescription());} } 三、ResourceLoaderAware接口 ResourceLoaderAware接口实现类的实例将获得一个ResourceLoader的引用ResourceLoaderAware接口也提供了一个setResourceLoader()方法该方法将由Spring容器负责调用Spring容器会将一个ResourceLoader对象作为该方法的参数传入。 如果把实现ResourceLoaderAware接口的Bean类部署在Spring容器中Spring容器会将自身当成ResourceLoader作为setResourceLoader()方法的参数传入。由于ApplicationContext的实现类都实现了ResourceLoader接口Spring容器自身完全可作为ResorceLoader使用。 案例 1.创建接口实现类 import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.ResourceLoader;/*** author .29.* create 2023-03-02 21:08*/ public class testBean implements ResourceLoaderAware {private ResourceLoader resourceLoader;//实现ResourceLoaderAware接口必须实现的方法//如果把该Bean部署在Spring容器中该方法将会有Spring容器负责调用。//SPring容器调用该方法时Spring会将自身作为参数传给该方法。Overridepublic void setResourceLoader(ResourceLoader resourceLoader) {this.resourceLoader resourceLoader;}//返回resourceLoader对象的方法public ResourceLoader getResourceLoader(){return this.resourceLoader;} }2.配置实现类的bean ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean id testBean classcom.haojin.spring.resources.ResourceLoaderAwareDemo.testBean/bean/beans3.测试 import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ResourceLoader;/*** author .29.* create 2023-03-02 21:12*/ public class Demo {public static void main(String[] args){ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(bean.xml);testBean testBean context.getBean(testBean, testBean.class);ResourceLoader resourceLoader testBean.getResourceLoader();System.out.println(Spring容器将自身注入到ResourceLoaderAware Bean 中: (context resourceLoader));} } 四、让Spring为Bean实例依赖注入资源建议 当程序获取 Resource 实例时总需要提供 Resource 所在的位置不管通过 FileSystemResource 创建实例还是通过 ClassPathResource 创建实例或者通过 ApplicationContext 的 getResource() 方法获取实例都需要提供资源位置。这意味着资源所在的物理位置将被耦合到代码中如果资源位置发生改变则必须改写程序。因此通常建议采用依赖注入让 Spring 为 Bean 实例依赖注入资源。 案例 1.创建依赖注入类定义属性和方法 import org.springframework.core.io.Resource;/*** author .29.* create 2023-03-02 21:36*/ public class ResourceBean {//Resource实现类对象private Resource resource;//对象的Getter() 和 Setter()public Resource getResource() {return resource;}public void setResource(Resource resource) {this.resource resource;}//输出资源信息的方法public void parse(){System.out.println(resource.getDescription());System.out.println(resource.getFilename());} } 2.创建spring配置文件配置依赖注入: 利用依赖注入以后只需要在配置文件中修改资源文件位置即可?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean id resourceBean classcom.haojin.spring.resources.DI.ResourceBeanproperty nameresource valuebaidu.txt//bean/beans3.测试 import org.springframework.context.support.ClassPathXmlApplicationContext;/*** author .29.* create 2023-03-02 21:38*/ public class testDI {public static void main(String[] args){ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(beans.xml);ResourceBean resourceBean context.getBean(resourceBean, ResourceBean.class);resourceBean.parse();} } 文章转载自: http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.bnbzd.cn.gov.cn.bnbzd.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.ngqty.cn.gov.cn.ngqty.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.clndl.cn.gov.cn.clndl.cn http://www.morning.smxrx.cn.gov.cn.smxrx.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.pdbgm.cn.gov.cn.pdbgm.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.yuanshenglan.com.gov.cn.yuanshenglan.com http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.txgjx.cn.gov.cn.txgjx.cn http://www.morning.ktmbr.cn.gov.cn.ktmbr.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.xgmf.cn.gov.cn.xgmf.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.lyhry.cn.gov.cn.lyhry.cn http://www.morning.ptxwg.cn.gov.cn.ptxwg.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.mzpd.cn.gov.cn.mzpd.cn http://www.morning.lwzgn.cn.gov.cn.lwzgn.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.qklff.cn.gov.cn.qklff.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.rfhwc.cn.gov.cn.rfhwc.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.pumali.com.gov.cn.pumali.com http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.npmcf.cn.gov.cn.npmcf.cn http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.rmrcc.cn.gov.cn.rmrcc.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.gwwtm.cn.gov.cn.gwwtm.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.cdygl.com.gov.cn.cdygl.com 查看全文 http://www.tj-hxxt.cn/news/242971.html 相关文章: 织梦中英文网站模板dede网站本地访问速度慢 家教网站建设广州网站建设推广服务 新郑做网站企业首次建设网站的策划流程 响应式网站开发的十大超级软件免费下载 腾讯云免费建站网页设计图片怎么居中对齐 网站 跳出率 多少汽车网站建设价格 注册一个个人网站wordpress手机版主题下载 昌吉做网站推广的公司外贸seo 以绿色为主色调的网站网站建设0doit 国外做装饰画的网站如何编辑网站源代码 h5 php mysql网站开发重庆设计网站建设 天天网站网站开发模板免费下载 河北省住房城乡建设局网站wordpress人工智能 展示照片的网站论坛定制 淄博网站建设哪家便宜茶叶网站建设的优势 做的网站错位怎么办一个完整的ppt作品 网站意义学校从色彩度讨论如何建设一个网站. 网站的企业风采怎么做做宣传图片用什么网站 江苏建设招标信息网站搜索引擎营销方法 卢氏县住房和城乡规划建设局网站百度一下网页版浏览器百度 无锡高端网站建设公司哪家好wordpress123页 模板建站服务器百度网盘会员 佛山优化网站做网站一般的尺寸 做百度竞价网站搜索不到外卖网站 模板 北京建设建网站深圳网站建设 制作元 设计可以在哪个网站接单厦门网络公司网站 合肥网站排名推广网站建设与制作的流程 长沙产品设计公司苏州seo推广公司 旅游微网站分销wordpress彩色tag 上海建设网站的公司响应式网站做seo怎么样