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

JSP
GET 方式提交的含有特殊字符的参数
java big5到gb2312的编码转换
java Lucene 中自定义排序的实现
hibernate中的增删改查实现代码
jsp 定制标签(Custom Tag)
jsp基础速成精华讲解
IE cache缓存 所带来的问题收藏
关于JSP的一点疑问小结
JSP 多条SQL语句同时执行的方法
jsp include文件时的一个乱码解决方法
在JSTL EL中处理java.util.Map,及嵌套List的情况
jsp 页面显示的一些用法
根据Hibernte的cfg文件生成sql文件
五种 JSP页面跳转方法详解
JSP 防范SQL注入攻击分析
JSP 连接MySQL配置与使用
java eclipse 启动参数
jsp 页面上图片分行输出小技巧
解决jsp开发中不支持EL问题
JSP 页面中使用FCKeditor控件(js用法)

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


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