如何从jar包中提取并获取其中的静态资源文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计160个文字,预计阅读时间需要1分钟。
项目成功打包成jar包后,可获取jar包内的静态资源。示例代码如下:
javapublic class DataResult { public void picRead(HttpServletRequest request, HttpServletResponse response, @RequestParam(path) String path) throws ServletException, IOException { // 读取本地图片输入流 InputStream input=new FileInputStream(path); }}
项目打成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(""); }
本文共计160个文字,预计阅读时间需要1分钟。
项目成功打包成jar包后,可获取jar包内的静态资源。示例代码如下:
javapublic class DataResult { public void picRead(HttpServletRequest request, HttpServletResponse response, @RequestParam(path) String path) throws ServletException, IOException { // 读取本地图片输入流 InputStream input=new FileInputStream(path); }}
项目打成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(""); }

