完成页面显示图片功能,
起初,用的是<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);
}
}