当前位置 : 主页 > 编程语言 > c++ >

打成jar包获取jar包内的静态资源

来源:互联网 收集:自由互联 发布时间:2021-07-03
项目打成jar包获取jar包内的静态资源 public DataResult picRead(HttpServletRequest request, HttpServletResponse response,@RequestParam("path") String path) throws ServletException, IOException { //读取本地图片输入流 Inpu
项目打成jar包获取jar包内的静态资源
public DataResult picRead(HttpServletRequest request, HttpServletResponse response,@RequestParam("path") String path)
            throws ServletException, IOException {
        //读取本地图片输入流
        InputStream inputStream = null;
        try{
           inputStream = new FileInputStream(resultUnZipPath+path);
        }catch (IOException e){
            try {
                //打成jar包可获取jar包内的静态资源
                inputStream = this.getClass().getResourceAsStream("/static/assets/images/imageNotFound.jpg");
            }catch (Exception e1){
                return new DataResult(RestConst.ErrorCode.EMPTY_PARAM,"未找到图片");
            }

        }
        int i = 0;
        while (i == 0){
            i = inputStream.available();
        }
        //byte数组用于存放图片字节数据
        byte[] buff = new byte[i];
        inputStream.read(buff);
        inputStream.close();
        response.setContentType("image/*");
        OutputStream out = response.getOutputStream();
        out.write(buff);
        out.close();
        return new DataResult("");
    }
网友评论