class path resource [api.pdf] cannot be opened because it does not exist

Posted 滚动的蛋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了class path resource [api.pdf] cannot be opened because it does not exist相关的知识,希望对你有一定的参考价值。

1、代码

  

public void downloadApiDoc(HttpServletResponse response) {
        ClassPathResource classPathResource = new ClassPathResource("api.pdf");
        try (InputStream inputStream = classPathResource.getInputStream()){
            //得到文件大小
            int size =inputStream.available();
            byte[] data =new byte[size];
            //读数据
            inputStream.read(data);
            inputStream.close();
            response.reset();
            //设置返回的文件类型
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "inline; filename=Api_Doc.pdf");
            response.setContentLength(size);
            OutputStream os = response.getOutputStream();
            os.write(data);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("download api doc");
        }
    }

  

2、原因:使用的classpath,虽然resources下有文件大门时traget的class下没有文件所有找不到

    

 

 

3、解决:重新build一下

   

以上是关于class path resource [api.pdf] cannot be opened because it does not exist的主要内容,如果未能解决你的问题,请参考以下文章

IllegalArgumentException: Could not resolve resource location pattern [classpath .xml]: class path

单点登录-----遇到问题-----FileNotFoundException: class path resource-UsernamePasswordWrapperAuthenticatio(代码

异常IOException parsing XML document from class path resource [xxx.xml]

Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource...

Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource...

SpringBoot Error creating bean with name 'dataSource' defined in class path resource。。。