***.file cannot be resolved to absolute file path because it does not reside in the file system

在java中使用Resoure.getFile()方法时,在本地可以正确找到,但是当代码打成jar包之后,执行此方法会报错

***.file cannot be resolved to absolute file path because it does not reside in the file system

原因在于打成jar包之后Resource.getFile()只会获取jar包不会读取jar包中的文件。可以改为:

        InputStream inputStream = resource.getInputStream();
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-type", "application/octet-stream;charset=UTF-8");
        response.setContentType("application/octet-stream;charset=UTF-8");
        try (OutputStream out = response.getOutputStream();
             BufferedInputStream bis = new BufferedInputStream(inputStream)) {
            byte[] buff = new byte[1024];
            int i = bis.read(buff);
            while (i != -1) {
                out.write(buff, 0, buff.length);
                out.flush();
                i = bis.read(buff);
            }
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。