获取本地图片,前端img中的SRC直接调用地址,显示文件

时间:2025-03-20 21:40:52
@RequestMapping(value = "/getCodeFile",method = )
public void getFile(HttpServletRequest request , HttpServletResponse response) throws IOException {

    OutputStream outputStream= null;
    InputStream in = null;
    try{
        //读取路径下面的文件
        File file = new File("C:/Users/AdministratorPC-20190507PKLY/Desktop/");
        //获取文件后缀名格式
        String ext = ().substring((().indexOf(".")));
        //判断图片格式,设置相应的输出文件格式
        if(("jpg")){
            ("image/jpeg");
        }else if(("JPG")){
            ("image/jpeg");
        }else if(("png")){
            ("image/png");
        }else if(("PNG")){
            ("image/png");
        }
        //读取指定路径下面的文件
        in = new FileInputStream(file);
        outputStream = new BufferedOutputStream(());
        //创建存放文件内容的数组
        byte[] buff =new byte[1024];
        //所读取的内容使用n来接收
        int n;
        //当没有读取完时,继续读取,循环
        while((n=(buff))!=-1){
            //将字节数组的数据全部写入到输出流中
            (buff,0,n);
        }
        //强制将缓存区的数据进行输出
        ();
        //关流
        ();
        ();
    }catch (Exception e){
        ();
    }finally {
        if(outputStream!=null){
            ();
        }
        if(in!=null){
            ();
        }
    }
}