<%@ page import="java.io.FileInputStream" %> <%@ page import="java.io.OutputStream" %> <%@ page import="java.io.File" %> <% String fileUrl = request.getParameter("fileUrl");//图片真实地址 response.setContentType("text/html; charset=GBK"); response.setContentType("image/jpeg"); // 设置图片格式格式,这里可以忽略 FileInputStream fis = null; OutputStream os = null; String ob = ""; File photo = null; try{ if(fileUrl !=null && !"".equals(fileUrl)){ photo = new File(fileUrl); if(photo.exists()){ fis = new FileInputStream(fileUrl); os = response.getOutputStream(); int count = 0; byte[] buffer = new byte[1024]; while ((count = fis.read(buffer)) != -1) { os.write(buffer, 0, count); } } } }catch(Exception e){ e.printStackTrace(); }finally { if (os != null) os.close(); if (fis != null) fis.close(); } %>
其他页面调用:
<img id="photo" src="showPhoto.jsp?fileUrl=<%=fileUrl%>" width="140" height="150" title="点击重新上传头像" /><!--fileUrl为图片存放真实路径-->