隨筆 - 25, 文章 - 1, 評論 - 0, 引用 - 0
          數據加載中……

          Ajax從服務端讀取圖片數據

          服務端代碼:
          @Action(value = "/imageload")
              
          public String imageLoad ()throws Exception {
                  
                  File f 
          = new File ("d:/down.jpg");
                  FileInputStream in 
          = new FileInputStream (f);
                  
          byte[] b = new byte[in.available()];
                  in.read(b);
                  HttpServletResponse response 
          = ServletActionContext.getResponse();
          //        response.setCharacterEncoding("UTF-8");
                  response.setContentType("image");
                  OutputStream pwt 
          = response.getOutputStream();
                  pwt.write(b);
                  pwt.flush();
                  pwt.close();
                  
          return null;
              }
          客戶端代碼:
          <%@ page language="java" contentType="text/html; charset=utf-8"
              pageEncoding
          ="utf-8"%>
          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
          <title>Insert title here</title>
          <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
          </head>
          <body>
          <img id="img1" src=""></img>
          <script type="text/javascript">
              
          function load() {
                  $('#img1').attr('src', 'subject
          /imageload.action');
              }
          </script>

          <button onclick=load();>點擊獲取</button>
          </body>
          </html>


          posted on 2010-05-19 17:20 至尊貝貝 閱讀(1241) 評論(0)  編輯  收藏 所屬分類: 代碼:JavaScript

          主站蜘蛛池模板: 师宗县| 邵武市| 五家渠市| 安义县| 西峡县| 安阳县| 保山市| 迁西县| 邹城市| 油尖旺区| 彭山县| 汉沽区| 隆昌县| 万山特区| 洪湖市| 龙南县| 晋江市| 崇明县| 昆山市| 南皮县| 西吉县| 疏勒县| 手机| 南木林县| 民丰县| 陆河县| 太谷县| 屯昌县| 虎林市| 广南县| 临沭县| 东城区| 项城市| 大宁县| 定陶县| 澄城县| 石柱| 原阳县| 宝丰县| 大英县| 西畴县|