<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    迷失北京

    BlogJava 聯(lián)系 聚合 管理
      60 Posts :: 0 Stories :: 13 Comments :: 0 Trackbacks

          廢話少說代碼伺候:

    封裝好的ImageUtil類:目的讀取本地的圖片文件并存入數(shù)據(jù)庫,然后讀出數(shù)據(jù)庫中以Blob形式存儲的圖片保存到指定目錄。

     

    1 package org.blog.util;
    2 import java.io.File;
    3 import java.io.FileInputStream;
    4 import java.io.FileNotFoundException;
    5 import java.io.FileOutputStream;
    6 import java.io.IOException;
    7  public class ImageUtil {
    8 private static File file = null;
    9 /**
    10 * 讀取圖像的二進(jìn)制流
    11 *
    12 * @param infile
    13 * @return
    14 */
    15 public static FileInputStream getByteImage(String infile) {
    16 FileInputStream inputImage = null;
    17 file = new File(infile);
    18 try {
    19 inputImage = new FileInputStream(file);
    20 } catch (FileNotFoundException e) {
    21 e.printStackTrace();
    22 }
    23 return inputImage;
    24 }
    25 /**
    26 * 輸出圖片
    27 * @param inputStream
    28 * @param path
    29 */
    30 public static void readBlob(FileInputStream inputStream, String path) {
    31 try {
    32 FileOutputStream fileOutputStream = new FileOutputStream(path);
    33 byte[] buf = new byte[1024];
    34 int len = 0;
    35 while ((len = inputStream.read(buf)) != -1) {
    36 fileOutputStream.write(buf, 0, len);//
    37   }
    38 inputStream.close();
    39 fileOutputStream.close();
    40 } catch (FileNotFoundException e) {
    41 e.printStackTrace();
    42 } catch (IOException e) {
    43 e.printStackTrace();
    44 }
    45 }
    46 }

    從數(shù)據(jù)庫中讀出二進(jìn)制流顯示到j(luò)sp頁面:

    servlet源碼:

     

    1 package servlet;
    2 import java.io.ByteArrayInputStream;
    3 import java.io.FileInputStream;
    4 import java.io.IOException;
    5 import java.io.InputStream;
    6 import java.io.OutputStream;
    7 import java.io.PrintWriter;
    8 import java.sql.Blob;
    9 import javax.servlet.ServletException;
    10 import javax.servlet.ServletOutputStream;
    11 import javax.servlet.http.HttpServlet;
    12 import javax.servlet.http.HttpServletRequest;
    13 import javax.servlet.http.HttpServletResponse;
    14 import org.blog.util.ImageUtil;
    15 import org.hibernate.Hibernate;
    16  public class Image extends HttpServlet {
    17 private static final long serialVersionUID = 1L;
    18 @Override
    19 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    20 throws ServletException, IOException {
    21 this.doPost(req, resp);
    22 }
    23 @Override
    24 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    25 throws ServletException, IOException {
    26 try {
    27 FileInputStream in = ImageUtil.getByteImage("D:\\me.jpg");
    28 Blob blob = Hibernate.createBlob(in);
    29 InputStream inputStream = blob.getBinaryStream();// IO流
    30 int length = (int) blob.length();
    31 byte[] b = new byte[length];
    32 inputStream.read(b, 0, length);
    33 PrintWriter out = resp.getWriter();
    34 InputStream is = new ByteArrayInputStream(b);
    35 int a = is.read();
    36 while (a != -1) {
    37 out.print((char) a);
    38 a = is.read();
    39 }
    40 out.flush();
    41 out.close();
    42 /*OutputStream outputStream = resp.getOutputStream();// 從response中獲取getOutputStream
    43 outputStream.write(b);// 寫
    44 inputStream.close();
    45 outputStream.close();*/
    46 } catch (Exception e) {
    47 System.out.println("error");
    48 }
    49 }
    50 }

    jsp源碼:

    1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    2 <%
    3 String path = request.getContextPath();
    4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5 %>
    6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7 <html>
    8 <head>
    9 <base href="<%=basePath%>">
    10
    11 <title>My JSP 'image.jsp' starting page</title>
    12
    13 <meta http-equiv="pragma" content="no-cache">
    14 <meta http-equiv="cache-control" content="no-cache">
    15 <meta http-equiv="expires" content="0">
    16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    17 <meta http-equiv="description" content="This is my page">
    18 <!--
    19 <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
    20 -->
    21 </head>
    22
    23 <body>
    24 <div style="border: solid red ;" mce_style="border: solid red ;"> <img src="image.do" mce_src="image.do"></div>
    25 </body>
    26 </html>


    posted on 2011-04-30 20:33 王康 閱讀(15679) 評論(1)  編輯  收藏

    Feedback

    # re: java以Blob形式存儲,讀取圖片并在jsp頁面顯示圖片流 2014-06-16 09:47 4w5t6
    gd th  回復(fù)  更多評論
      


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 日本免费人成视频在线观看| 久久午夜夜伦鲁鲁片无码免费| 免费A级毛片无码A∨| 亚洲色欲久久久久综合网| 一级毛片aaaaaa视频免费看| 日本中文一区二区三区亚洲| 污污污视频在线免费观看| 亚洲欧洲日产国码二区首页| 四虎在线免费视频| 一本色道久久综合亚洲精品蜜桃冫| 永久免费AV无码网站在线观看| 免费视频成人国产精品网站| 亚洲人成色77777| 美女扒开屁股让男人桶爽免费| 青青操免费在线观看| 在线看片韩国免费人成视频| 亚洲avav天堂av在线网爱情| 全免费a级毛片免费看不卡| 九九免费精品视频在这里| 久久精品国产亚洲沈樵| 和日本免费不卡在线v| 精品久久亚洲一级α| 久久国产精品亚洲一区二区| 亚洲三级在线免费观看| 免费观看国产精品| 国产偷伦视频免费观看| 亚洲精品视频在线看| 男女啪啪免费体验区| 亚洲精品无码专区久久久 | 国产卡二卡三卡四卡免费网址| 免费无码午夜福利片 | 亚洲啪啪免费视频| 亚洲情综合五月天| 国产精品无码一二区免费| 四虎在线成人免费网站| 日本视频在线观看永久免费| 青青久久精品国产免费看| 亚洲欧美成人av在线观看| 亚洲成aⅴ人片在线影院八| 亚洲精品成人片在线播放| 亚洲精品成人久久久|