May 16

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

在Servlet只能显示一个图片,并且用图层定位时写代码很是麻烦,并且如果是从流中读数据,就只能显示一个图片,同时,还不能解决图片和文字信息的同时输出和位置叠加
 在JSP中,我们可以用以下办法来解决
 首先,写一个Servlet负责从数据文件或数据库等数据来源中得到数据流并输出
 然后在JSP文件中多次调用此Servlet来请求并取的图片数据流并显示
 我的Servlet代码入下

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;[separator]

 import javax.servlet.http.HttpServletRequest;

 import javax.servlet.http.HttpServletResponse;

 /**

 *@authorAdministrator

 *

 *TODO要更改此生成的类型注释的模板,请转至窗口-首选项-Java-代码样式-代码模板

 */

 public class InitializeMap extends HttpServlet
 {

   String imgName;

   public InitializeMap(){

   super();

 }

 /**

 *Destructionoftheservlet.


 */

 public void destroy(){

 super.destroy();//Justputs"destroy"stringinlog

 //Putyourcodehere

 }

 /**

 *ThedoDeletemethodoftheservlet.


 *

 *ThismethodiscalledwhenaHTTPdeleterequestisreceived.

 *

 *@paramrequest

 *therequestsendbytheclienttotheserver

 *@paramresponse

 *theresponsesendbytheservertotheclient

 *@throwsServletException

 *ifanerroroccurred

 *@throwsIOException

 *ifanerroroccurred

 */

 public void doDelete(HttpServletRequest request,

 HttpServletResponse response) throws ServletException,IOException
 {

 //Putyourcodehere

 }

 /**

 *ThedoGetmethodoftheservlet.


 *

 *Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.

 *

 *@paramrequest

 *therequestsendbytheclienttotheserver

 *@paramresponse

 *theresponsesendbytheservertotheclient

 *@throwsServletException

 *ifanerroroccurred

 *@throwsIOException

 *ifanerroroccurred

 */

 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];

 //readthestreamdataintothestream

 fis.read(data);

 fis.close();

 OutputStream sos=response.getOutputStream();

 //readthestreamdataintotheoutputstream

 response.setContentType("image/*");

 sos.write(data);

 sos.flush();

 sos.close();

 }

 /**

 *ThedoPostmethodoftheservlet.


 *

 *Thismethodiscalledwhenaformhasitstagvaluemethodequalsto

 *post.

 *

 *@paramrequest

 *therequestsendbytheclienttotheserver

 *@paramresponse

 *theresponsesendbytheservertotheclient

 *@throwsServletException

 *ifanerroroccurred

 *@throwsIOException

 *ifanerroroccurred

 */

 public void doPost(HttpServletRequest request,HttpServletResponse response)

 throws ServletException,IOException{

 response.setContentType("text/html");

 PrintWriter out=response.getWriter();

 out.println("");

 out.println("");

 out.println("");

 out.println("");

 out.print("Thisis");

 out.print(this.getClass());

 out.println(",usingthePOSTmethod");

 out.println("");

 out.println("");

 out.flush();

 out.close();

 }

 /**

 *ThedoPutmethodoftheservlet.


 *

 *ThismethodiscalledwhenaHTTPputrequestisreceived.

 *

 *@paramrequest

 *therequestsendbytheclienttotheserver

 *@paramresponse

 *theresponsesendbytheservertotheclient

 *@throwsServletException

 *ifanerroroccurred

 *@throwsIOException

 *ifanerroroccurred

 */

 public void doPut(HttpServletRequest request,HttpServletResponse response)

 throws ServletException,IOException{

 //Putyourcodehere

 }

 /**

 *Returnsinformationabouttheservlet,suchasauthor,version,and

 *copyright.

 *

 *@returnStringinformationaboutthisservlet

 */

 public String getServletInfo(){

 return "ThisismydefaultservletcreatedbyEclipse";

 }

 /**

 *Initializationoftheservlet.


 *

 *@throwsServletException

 *ifanerroroccure

 */

 public void init() throws ServletException{

 //Putyourcodehere

 }

 public void initURI(HttpServletRequest request){

 this.imgName=request.getParameter("imgName");

 }

 }
Tags: , ,
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]