浏览器直接访问file:///D://data/upload/banner.png是可以的,但在页面里就是不显示
解决:不配置绝对路径,src里访问后台,参数为图片名称,通过应用服务器获取图片输出
jsp:
后台:
@RequestMapping("/image/{filename}") public void showImageByType(@PathVariable String filename, HttpServletRequest request, HttpServletResponse response) throws Exception { InputStream inputStream = null; OutputStream writer = null; try { String root = ReadeGlobePa.getValueByProper("fileSaveDir"); filename = filename.replace("/", ""); filename = filename.replace("..", ""); inputStream = new FileInputStream(new File(root + "/upload/" + filename)); writer = response.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = inputStream.read(buf)) != -1) { writer.write(buf, 0, len); //写 } inputStream.close(); } catch (FileNotFoundException e) { response.setStatus(404); } catch (Exception e) { // logger.error(e.getMessage(), e); } finally { try { if (inputStream != null) { inputStream.close(); } if (writer != null) { writer.close(); } } catch (IOException e) { logger.error(e.getMessage(), e); } } }