| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 692 人关注过本帖
标题:[转帖]在JSP中如何从数据流中取得图片数据并按随意位置显示
只看楼主 加入收藏
bestlife
Rank: 1
等 级:新手上路
帖 子:484
专家分:0
注 册:2005-7-8
收藏
 问题点数:0 回复次数:0 
[转帖]在JSP中如何从数据流中取得图片数据并按随意位置显示
在JSP中如何从数据流中取得图片数据并按随意位置显示
    在Servlet只能显示一个图片,并且用图层定位时写代码很是麻烦,并且如果是从流中读数据,就只能显示一个图片,同时,还不能解决图片和文字信息的同时输出和位置叠加
    在JSP中,我们可以用以下办法来解决
    首先,写一个Servlet负责从数据文件或数据库等数据来源中得到数据流并输出
    然后在JSP文件中多次调用此Servlet来请求并取的图片数据流并显示
我的Servlet代码入下
InitializeMap.java
/*
 * 创建日期 2005-5-30
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package zy.ser;


import
import
import

import
import

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 转帖 数据 位置 
2005-09-28 10:52
快速回复:[转帖]在JSP中如何从数据流中取得图片数据并按随意位置显示
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023030 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved