首页 > 网站 > WEB开发 > 正文

面上直接显示图片

2024-04-27 15:17:22
字体:
来源:转载
供稿:网友

1,后台java 

1.1 Control 层

 /**     * 访问此链接 可以在页面上直接显示图片     *     * @param request     * @param response     * @URL <img src="${ctx}/upload/showImg?downloadUrl=/member/attachment/member/2016/01/1452838617345176.png">     */    @RequestMapping(value = "/showImg")    public void showImageByUrl(HttpServletRequest request, HttpServletResponse response) {        // downloadUrl =/member/attachment/member/1281351564138117.PNG        String downloadUrl = request.getParameter("downloadUrl");        logger.info("#showImage#下载链接downloadUrl= " + downloadUrl);        // 从Local下载        service.downloadAttachFromLocal(downloadUrl, response);    }1.2 server 层

@Override    public void downloadAttachFromLocal(String downloadUrl, HttpServletResponse response) {        // 文件路径前缀        String downloadLocalPathFront = PRopertyUtil.getValue("attach.localPath.front");        OutputStream os = null;        InputStream is = null;        try {            is = FileUtils.downloadFile(downloadLocalPathFront + downloadUrl);            os = response.getOutputStream();            response.reset();            response.setContentType("image/jpeg");            response.setHeader("Pragma", "No-cache");            response.setHeader("Cache-Control", "no-cache");            response.setDateHeader("Expires", 0);            if (is != null) {                byte[] bytes = new byte[4096];                int c;                while ((c = is.read(bytes)) != -1) {                    os.write(bytes, 0, c);                }            }            os.flush();        } catch (IOException e) {            ControllerUtil.printStackTrace(e);        } finally {            try {                if (is != null)                    is.close();            } catch (IOException e) {                ControllerUtil.printStackTrace(e);            }            try {                if (os != null)                    os.close();            } catch (IOException e) {                ControllerUtil.printStackTrace(e);            }        }    }

2, 页面 jsp

<div class="dow display_length" title="${attachDetail.actiNumAttachIdModel.originalFileName}">												<a href="${ctx}/upload/showImg?downloadUrl=<c:out value="${attachDetail.actiNumAttachIdModel.downloadUrl}"/><c:out value="${attachDetail.actiNumAttachIdModel.downloadFileName}"/>">													<img src="${ctx}/upload/showImg?downloadUrl=<c:out value="${attachDetail.actiNumAttachIdModel.downloadUrl}"/><c:out value="${attachDetail.actiNumAttachIdModel.downloadFileName}"/>" alt="" id="img-mwsf-14" class="photo"  height="150" style="border:1px solid black;"/>												</a>												</div>图片显示例子:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表