JSP 绝对路径查看图片的问题 (A标签 src属性)

 关于绝对路径查看图片的问题  如下

  <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;

}

然后前端就可以显示了

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值