美文网首页
***.file cannot be resolved to a

***.file cannot be resolved to a

作者: chanyi | 来源:发表于2020-07-16 14:11 被阅读0次

    在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);
                }
            }
    

    相关文章

      网友评论

          本文标题:***.file cannot be resolved to a

          本文链接:https://www.haomeiwen.com/subject/sykphktx.html