在显示上传的图片,IO流读取方式

完成页面显示图片功能,

     起初,用的是<img>标签,通过在src里配置文件绝对路径的方式显示图片,可能是因为服务器文件访问权限问题,导致图片不显示.

<img src="${attachment.filePath}"  alt="图片未上传或无图片" />

然后打算用 Action路径直接去用IO流读取图片.

 

jsp页面代码:

<img alt="图片未显示或无图片上传" src="showPhotoImage.action?id=${attachment.attId}">

java代码:

/**
	 * 证件图片的回显.
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "showPhotoImage.action", produces = "text/html;charset=UTF-8")
    public void showPhotoImage(HttpServletRequest request,HttpServletResponse response) {
        FileInputStream isr;
        try {
            String file = "";
           String attid = request.getParameter("id");
            Attachment attachment = (Attachment) attachmentService.get(attid);
            File f = null;
            file = attachment.getFilePath();
            f = new File(file);
            if (f.exists()) {
                isr = new java.io.FileInputStream(file);
                int c;
                byte[] b = new byte[1024];
                while ((c = isr.read(b)) != -1) {
                    response.getOutputStream().write(b, 0, c);
                }
                response.getOutputStream().flush();
                isr.close();
            }
        } catch (Exception exc) {
            logger.error(exc);
        }
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值