`

servlet显示图片

    博客分类:
  • java
 
阅读更多

 

/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType(CONTENT_TYPE);
		request.setCharacterEncoding("UTF-8");
        InputStream in = null;
        OutputStream out = null;
		String fjzj = request.getParameter("fjzj");
		XtglFj xtglFj = getFjByZj(fjzj);
		String realpath = getXtPath(); // 获取附件服务器保存的路径
		String path = realpath + xtglFj.getFjpath(); //文件路径
		if(path != null && !"".equals(path)){
			File outputFile = new File(path);
			response.setContentType("text/html; charset=UTF-8");
			response.setContentType("image/jpeg");
			if (outputFile.exists() && outputFile.length() > 0) {
				in = new FileInputStream(outputFile);
				out = response.getOutputStream();
				IOUtils.copy(in, out);
				out.close();
            } else {
                System.out.println("找不到文件:" + outputFile.getName());
            }
		}
	}

 如何显示在jsp页面的话,

 

去掉

response.setContentType("text/html;charset=UTF-8");
response.setContentType("image/jpeg");

 添加标签 <img src='${pageContext.request.contextPath}/zpservlet?n="+ran+"&fjzj=11 />

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics