当前位置: 代码迷 >> Java Web开发 >> [转帖]在JSP中如何从数据流中取得图片数据并按随意位置显示
  详细解决方案

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

热度:295   发布时间:2005-09-28 10:52:00.0
[转帖]在JSP中如何从数据流中取得图片数据并按随意位置显示
在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");
    }
}
搜索更多相关主题的帖子: JSP  转帖  数据  位置  

----------------解决方案--------------------------------------------------------
  相关解决方案