***.file cannot be resolved to a

2020-07-16  本文已影响0人  chanyi

在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);
            }
        }
上一篇下一篇

猜你喜欢

热点阅读