一直没有接触过对图片的处理,最近电子档案项目中要求在页面在浏览图片。
要解决的问题:用户进入图片信息列表,对感兴趣的图片,点击“查看图片”弹出一个页面,浏览图片
Action的方法:
/**
* 根据ID显示图片
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward checkimg(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String jh = request.getParameter("jh");
ImgService imgser = (ImgService) getBean("imgservice");
Imginfo img = (Imginfo) imgser.findImgByJH(jh);
try {
BufferedOutputStream buffout = new BufferedOutputStream(response
.getOutputStream());
response.setContentType("image/jpeg");
Clob clob = img.getImg();
String imgstr=clob.getSubString(1, (int) clob.length());//这段代码是将clob字段转换成字符串
byte[] buff = ImgUtil.getBase64(imgstr);//项目中图片是转换为base64码存储数据库的,取出来需要对其解码
buffout.write(buff);
buffout.flush();
buffout.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
JSP页面代码:
这段代码效果,点击查看图片,把参数传到弹出的页面
<a href="#" οnclick="javascript:window.open('/dzdan/imgupload/checkimg.jsp?jh=<%=info.getJh() %>','','height=500, width=800, top=100,left=280, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')">查看图片</a>
弹出页面代码:图片路径直接指向 Action的checkinmg方法,jh是参数
<body>
<%String jh=request.getParameter("jh"); %>
<%if(!jh.equals("")){
jh.trim();
%>
<img src="upload.do?method=checkimg&jh=<%=jh %>">
<%} %>
</body>
struts.config配置文件;
<action path="/imgupload/upload" type="com.gzepro.imgupload.struts.action.UploadAction"
name="uploadForm" parameter="method">
<forward name="success" path="/imgupload/view.jsp"></forward>
<forward name="fail" path="/imgupload/fail.jsp"></forward>
<forward name="imglist" path="/imgupload/imginfo_list.jsp"></forward>
<forward name="index" path="/imgupload/index.jsp"></forward>
<forward name="pki" path="/imgupload/result.jsp"></forward>
</action>
其实很简单,但第一次接触,花的时间多了点,代码效率也不高,找时间重写一下