当前位置: 首页 > 图文教程 > 网络编程 > JSP > 在JSP中如何从数据流中取得图片数据并按随意位置显示

JSP
JSP连接各类数据库大全(6)
小窗口大学问--玩转弹出窗口(2)
Jsp中的session使用
Jsp中数据bean的直接赋值
Jsp/bean Mysql数据库 新增 修改 删除的通用方法
随机数字 浮点数 字符串产生
利用HttpSessionListener统计在线人数
servlet规范定义的Servlet 生命周期
serlvet为什么只需要实现doGet和doPost
servlet实例的个数及因此引发的问题
jsp计数器制作手册(2)
JSP连接各类数据库大全(1)
JSP编程进度条设计实例(3)
JSP编程进度条设计实例(5)
jsp计数器制作手册(1)
JSP编程进度条设计实例(1)
小窗口大学问--玩转弹出窗口(1)
小窗口大学问--玩转弹出窗口(4)
ASP与JSP的比较(1)
JSP连接各类数据库大全(5)

在JSP中如何从数据流中取得图片数据并按随意位置显示


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 124 ::
收藏到网摘: n/a

在JSP中如何从数据流中取得图片数据并按随意位置显示
    在Servlet只能显示一个图片,并且用图层定位时写代码很是麻烦,并且如果是从流中读数据,就只能显示一个图片,同时,还不能解决图片和文字信息的同时输出和位置叠加
    在JSP中,我们可以用以下办法来解决
    首先,写一个Servlet负责从数据文件或数据库等数据来源中得到数据流并输出
    然后在JSP文件中多次调用此Servlet来请求并取的图片数据流并显示
我的Servlet代码入下
InitializeMap.java
/*
 * 创建日期 2005-5-30
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package zy.ser;


import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator
 * 
 * TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class InitializeMap extends HttpServlet {
    String imgName;
    public InitializeMap() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doDelete method of the servlet. <br>
     * 
     * This method is called when a HTTP delete request is received.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doDelete(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.initURI(request);
        File   file=new File(this.getServletContext().getRealPath("img\\map1\\"+this.imgName+".jpg"));
        System.out.println(file);
        FileInputStream  fis=new FileInputStream(file);
        System.out.println(fis);
        int length=fis.available();
        System.out.println(length);
        byte[]  data=new byte[length];
        //read  the  stream  data into  the  stream 
        fis.read(data);
        fis.close();
        OutputStream  sos=response.getOutputStream();
        //read  the  stream  data  into  the output stream
        response.setContentType("image/*");
        sos.write(data);
        sos.flush();
        sos.close();
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01     Transitional//EN\">");
                out.println("<HTML>");
                out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
                out.println(" <BODY>");
                out.print(" This is ");
                out.print(this.getClass());
                out.println(", using the POST method");
                
                out.println(" </BODY>");
                out.println("</HTML>");
                out.flush();
                out.close();

    }

    /**
     * The doPut method of the servlet. <br>
     * 
     * This method is called when a HTTP put request is received.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Put your code here
    }

    /**
     * Returns information about the servlet, such as author, version, and
     * copyright.
     * 
     * @return String information about this servlet
     */
    public String getServletInfo() {
        return "This is my default servlet created by Eclipse";
    }

    /**
     * Initialization of the servlet. <br>
     * 
     * @throws ServletException
     *             if an error occure
     */
    public void init() throws ServletException {
        // Put your code here
    }
    public void initURI(HttpServletRequest  request){
        this.imgName=request.getParameter("imgName");
    }
}