当前位置: 代码迷 >> Web前端 >> 用源打开图片
  详细解决方案

用源打开图片

热度:111   发布时间:2012-10-24 14:15:58.0
用流打开图片
前端jsp:
application.setAttribute("url", arrPic[0]);
<script>
window.service.src = "GetImageFile.jsp";
</script>

GetImageFile.jsp:
<%@ page info="Random Image Show"
    pageEncoding="GBK" contentType="image/jpg"
    autoFlush="true" buffer="16kb" session="false" import="java.io.*" import="java.net.*"
%>
<%
try
{
System.out.println("ClientUrl=" + (String)application.getAttribute("url"));
String clientUrl = (String)application.getAttribute("url");         
URL url = new URL(clientUrl);  
  URLConnection conn = url.openConnection();  
  InputStream fis = conn.getInputStream(); 
ServletOutputStream sos = response.getOutputStream();
int i = fis.available(); // 得到文件大小
byte buf[] = new byte[i];  
int len = 0;
while ((len = fis.read(buf)) != -1) {
    sos.write(buf, 0, len);
}
sos.flush();
sos.close();
fis.close();
out.clear();
out = pageContext.pushBody();
}  
catch (Exception e)
{  
e.printStackTrace();  
}  
%>
  相关解决方案