Spring 演示UrlResource访问网络资源

Posted on 2024-01-18 00:57  弯弓射雕的男人  阅读(40)  评论(0编辑  收藏  举报
package com.atguigu.spring6.Resource;

import org.springframework.core.io.UrlResource;

import java.io.IOException;
import java.net.MalformedURLException;

    //演示UrlResource访问网络资源
public class UrlResourceDemo {
    public static void main(String[] args) {
        //http前缀开头
        loadUrlResour("http://www.baidu.com");
        //file前缀开头  要放到项目根路径下
        loadUrlResour("file:atguigu.txt");
    }

    //访问http
    public static void loadUrlResour(String path){

        try {
            //1.创建resource解救实现类UrlResour
            UrlResource url=new UrlResource(path);
            //2 获取资源信息
            System.out.println(url.getFilename());
            System.out.println(url.getURL());
            System.out.println(url.getDescription());
            System.out.println(url.getInputStream().read());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}