关于绝对路径查看图片的问题 如下
<img class="img " src="D://jzee/xx.jpg" style="height: 67px;">
这种直接写的绝对路径 项目在处理的时候会加上 前缀 导致图片预览失败
针对这种情况 将 路径改为 跳转后台地址 如下
filePath为图片磁盘路径 D:/JZEE/swjcasignimg/XX.PNG 由于直接传参会导致 400 所以后台传过来先加密转码的
<img class="img " src="/jzee-web-neikong/account/swj/accountWkFlow/showPicture.do?filePath=D%3A%5C%5Cjzee%5Cswjcasignimg%5C%E9%A9%AC%E6%99%8B%E6%AF%85.png" style="height: 67px;">
具体操作如下 后台获取到绝对 路径进行 加密 然后在加上后台得解析地址
//后台请求地址代码如下
@RequestMapping("/showPicture") @ResponseBody public void showPicture(String filePath, HttpServletRequest request, HttpServletResponse response) throws IOException { if (CommonUtil.isNotEmpty(filePath)) { try { filePath = java.net.URLDecoder.decode(filePath,"UTF-8"); } catch (Exception e) { e.printStackTrace(); } FileInputStream is = getImageBlob(filePath); if (is != null){ int i = is.available(); // 得到文件大小 byte data[] = new byte[i]; is.read(data); // 读数据 is.close(); OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象 toClient.write(data); // 输出数据 toClient.close(); } } } /** * 获取文件流 * @param file_path * @return * @throws IOException */ public FileInputStream getImageBlob(String file_path) throws IOException { FileInputStream is = null; File filePic = new File(file_path); try { is = new FileInputStream(filePic); } catch (Exception e) { e.printStackTrace(); } return is; }
然后前端就可以显示了