servlet使用dispatcher转jsp页面,原本图片无法显示

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/guoanddong/article/details/51959835

在servlet处理完之后使用dispatcher转到jsp页面,原本的图片无法显示, 因为servlet转jsp的路径目录与原本的路径目录不同。所以会有无法显示的问题;

解决方案;就是在jsp页面添加

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

在head中添加 base路径

<head>
  <base href="<%=basePath%>">
    <title>My JSP 'demo.jsp' starting page</title>
  </head>

展开阅读全文

Servlet + Extjs图片无法显示

05-28

1、servlet 部分[code=java]public class PrintImage extends HttpServlet rn private static final long serialVersionUID = 1L;rn rn /**rn * @see HttpServlet#HttpServlet()rn */rn public PrintImage() rn super();rn // TODO Auto-generated constructor stubrn rnrn /**rn * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)rn */rn protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException rn doPost(request, response);rn rnrn /**rn * @throws IOException rn * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)rn */rn protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException rn BufferedOutputStream outputStream =null;rn InputStream fileInputStream = null;rn tryrn //这里没有设置charset=utf-8 是否有关?rn response.setContentType("image/jpeg");rn response.setHeader("Pragma", "No-cache");rn response.setHeader("Cache-Control", "no-cache");rn response.setHeader("Expires", "0");rn response.setDateHeader("Expires", 0);rn rn System.out.println("PrintImage invoked");rn //文件名称是页面传入的 路径是绝对路径 如/home/image/rn File file = new File(SysParamUtil.getParam(SysParamConstants.FILE_UPLOAD_DISK)+ request.getParameter("fileName")); rn rn if(!file.exists()) rn fileInputStream = request.getSession().getServletContext().getResourceAsStream("/ext/resources/images/nopic.gif");rn else rn fileInputStream = new FileInputStream(file);rn rn rn BufferedImage bufferedImage = ImageIO.read(fileInputStream);rn outputStream = new BufferedOutputStream(response.getOutputStream());rn JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder(outputStream);rn JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(bufferedImage);rn encodeParam.setQuality(1f, true);rn imageEncoder.encode(bufferedImage, encodeParam);rn rn byte[] data = new byte[8192];rn rn int len = -1;rn rn while((len = fileInputStream.read(data, 0, 8192)) != -1) rn outputStream.write(data, 0, len);rn rn catch(Exception ex)rn ex.printStackTrace();rn finallyrn if(outputStream !=null)rn outputStream.flush();rn outputStream.close();rn rn if(fileInputStream != null)rn fileInputStream.close();rn rn rn rnrn[/code]rnrn2、servlet配置部分rn[code=text]rnrn PrintImagern PrintImagern PrintImagern com.huateng.system.util.PrintImagern rnrn PrintImagern /PrintImagern rn[/code]rn3、下面是extjs部分rn[code=javascript]var dataview = new Ext.DataView(rn frame: true,rn store: storeImg,rn tpl : new Ext.XTemplate(rn ' ',rn '',rn ' ',rn ' ',rn '',rn ' ',rn '',rn '',rn '',rn ''rn ),rn id: 'phones',rn itemSelector: 'li.phone',rn overClass : 'phone-hover',rn singleSelect: true,rn multiSelect : true,rn autoScroll : truern );[/code]rn程序发布到A主机上可以正常显示图片,到B主机就不行了,文件路径都能读写,程序无异常! 论坛

没有更多推荐了,返回首页