码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-06-10 11:28:23      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:load   trace   public   img   position   找不到   osi   大小   type   

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

标签:load   trace   public   img   position   找不到   osi   大小   type   

原文地址:https://www.cnblogs.com/irobotzz/p/13083535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!