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

JSP
无需调优的内存优化
JAVA EXCEL API
新手指南之如何搜索你的问题的答案
Java 101:如果我不懂Java该怎么办?
jsp由浅入深
把本页内容导出成word文件或excel文件(原创)
在BEA WebLogic中使用Java消息服务
Form Your Own Design Pattern Study Group
从Coding Fan到真正的技术专家
我的Mysql5.0中文乱码解决方案
结合JAVASCRIPT将HTML导入Excel形成简单Web报表
Struts 的汉字显示问题终结解决方案
j2me网络实战指南
Java Page Flow开发:从JDBC数据源中检索并显示数据
人生、梦想、Java,又一个梦想者上路!
用Struts开发基于MVC的Web应用
Apache plug-in配置代理请求实战
一个简单的Timer Service
Tomcat 服务器下 JSP 页面中文问题及解决方法总结
Java Servlet 编程及应用(一)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 111 ::
收藏到网摘: 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");
    }
}