`

jsp中img引用本地文件

img 
阅读更多
在web应用中是不能访问本地文件De,可以把要引用的文件移到项目中,通过相对路径获取。有时候我们就是需要访问本地文件,那么我们可以通过流的形式传入。例如:
在showPic.jsp中:<body>
<img align="top" src="<%=request.getContextPath()%>/viewListener/viewWareCamera/picCreate.jsp?ppath=<bean:write name='cameraOperateForm' property='picture_postion'/>" width="500" height="400" id="imgg"/>
</body>
在这里,我们的src通过调用另一个jsp来实现。并且也可以根据需要向另一个jsp传值。
那么,在picCreate.jsp中:
<%@ page import="java.io.*" %>

<%
String ppath=request.getParameter("ppath");
    String file = "Z:/"+ppath;
FileInputStream in = null;
OutputStream o=null;
try{
in=new FileInputStream(new File(file));
o=response.getOutputStream();
o.flush();
  int l = 0;
  byte[] buffer = new byte[4096];
while((l = in.read(buffer)) != -1){
o.write(buffer,0,l);
}
                   //下面这两句是清理jsp自动的带有out流。如果不做操作的话,会报流已经使用的异常
out.clear();
out = pageContext.pushBody();

}catch(Exception e){
e.printStackTrace();
System.out.println("already called");
}finally{
o.flush();
in.close();
o.close();
}%>

这样就可以实现显示本地的文件或图片。
分享到:
评论
1 楼 hellodajun 2012-06-25  
如果我是远程访问,然后还想预览图片,如何整。。。

相关推荐

Global site tag (gtag.js) - Google Analytics