使用 img标签引用项目外的 绝对路径的 图片

这里有2中方式
1.使用tomcat文件夹下的conf里面的server.xml里面的host标签下 添加一个

        <Context path="/file" docBase="E:\test" debug="0" reloadable="true"/>

然后在 img 的src= “ /file/red.png” 就是引用了 E:\test\red.png图片

2.使用io流的方式 显示图片

先使用action写一个输出文件流的action
例如

public void show(){
         HttpServletRequest request = ServletActionContext.getRequest();
         HttpServletResponse response = ServletActionContext.getResponse();

         String url = request.getParameter("url");
         String path = url.substring(0,url.lastIndexOf("/"));
         String realFileName = url.substring(url.lastIndexOf("/")+1,url.length());
         String fileName = request.getParameter("fileName");
         String suffix = request.getParameter("suffix");
//       path = ServletActionContext.getServletContext().getRealPath(path);

        try {
            fileName = java.net.URLEncoder.encode(fileName+"."+suffix, "utf-8"); //ie 中文不兼容问题
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;filename="
                + fileName);

        OutputStream os = null;
        FileInputStream fis = null;
        byte[] buffer = new byte[1024];
        path = path+"\\"+realFileName+"."+suffix;
        int len = 0;
        try {

            fis = new FileInputStream(new File(path));
            os = response.getOutputStream();
            while ((len = fis.read(buffer)) > 0) {
                os.write(buffer, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                os.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return ;
    }

然后在然后在 img 的src= “ ImgPathActionDownLoad.action?url=E:\test\red.png &fileName=red&suffix=png” 就是引用了 E:\test\red.png图片 这里面的除了参数 url其他你自己可以在action里面改

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值