java中将图片保存到数据库中

在实际的开发中,咱们可能须要将图片、影音等文件直接保存到数据库中,而后经过编程方式将数据读出进行使用。例如将读出的图片数据显示出来,将读出的电影文件播放出来。 二进制数据直接保存到文件和从文件中读出很是的简单。和普通的数据库操做差异不大。只是用到部分流操做。例如各类输入输出流操做。因此深入理解流操是很是重要的。 在此我借助于一个JSP的简单实例进行讲解。此实例保存职员数据,其中职员数据包含一个图片列。此列保存每名员工的照片。在此将照片直接保存到数据库中。首先创建职员信息表EmployeeInfo,表列很是的简单 employeeId:职员编号(自动增加);employeeName:职员姓名;age:职员年龄;pic:职员图片(image类型) 首先讲解信息的保存。先创建一个录入界面index.jsp,其中包含一个<input type="file"/>元素,用于让用户选择图片文件。 页面代码以下(不作过多讲解): < %@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> < % String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> < !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html> < head> < base href="<%=basePath%>"> < title>My JSP 'index.jsp' starting page</title> < meta http-equiv="pragma" content="no-cache"> < meta http-equiv="cache-control" content="no-cache"> < meta http-equiv="expires" content="0"> < meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> < meta http-equiv="description" content="This is my page"> < !-- <link rel="stylesheet" type="text/css" href="styles.css"> --> < /head> < body> < form action="addServlet" method="POST"> < table border="15" width="60%" align="center"> < tr> < td width="30%" align="right">EmployeeName:</td> < td width="70%" align="left"><input type="text" name="employeeName"/></td> < /tr> < tr> < td width="30%" align="right">Age:</td> < td width="70%" align="left"><input type="text" name="age"/></td> < /tr> < tr> < td width="30%" align="right">Image:</td> < td width="70%" align="left"><input type="file" name="pic"/></td> < /tr> < tr> < td colspan="2" align="center"><input type="submit" value="add"/></td> < /tr> < /table> < /form> < /body> < /html> 页面请求addServlet,获取录入信息,并调用JavaBean实现数据保存。Servlet代码以下: package com.frank.action; 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; import com.frank.rule.*; public class AddServlet extends HttpServlet { /** * Constructor of the object. */ public AddServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // 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 { String employeeName=request.getParameter("employeeName"); int age=Integer.parseInt(request.getParameter("age")); String pic=request.getParameter("pic"); EmployeeDAO employeeDAO=new EmployeeDAO(); if(employeeDAO.employeeAdd(employeeName, age, pic)) response.sendRedirect("success.jsp"); else response.sendRedirect("index.jsp"); } /** * 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 { doGet(request,response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } } 此Servlet只是简单的获取页面输入的数据,其中获取的Pic为用户选择的图片路径。严格讲,获取文件须要通过验证,防止非法图片的选择,这个能够根据本身的须要改善。而后Servlet调用业务类,将获取的数据经过参数传入,进行数据的增长。其中employeeAdd方法实现数据的保存,代码以下: public boolean employeeAdd(String employeeName,int age,String pic){ Connection conn=null; PreparedStatement pstmt=null; FileInputStream fis=null; try{ Class.forName("net.sourceforge.jtds.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/SampleDB","sa",""); fis=new FileInputStream(pic); String strSQL="INSERT INTO employeeInfo VALUES(?,?,?)"; pstmt=conn.prepareStatement(strSQL); pstmt.setString(1, employeeName); pstmt.setInt(2, age); pstmt.setBinaryStream(3, fis, fis.available()); if(pstmt.executeUpdate()>0) return true; else return false; }catch(ClassNotFoundException ex){ ex.printStackTrace(); return false; }catch(SQLException ex){ ex.printStackTrace(); return false; }catch(IOException ex){ ex.printStackTrace(); return false; }finally{ try{ fis.close(); pstmt.close(); conn.close(); }catch(Exception ex){ } } } 注意粗体部分,用获取的pic信息(文件路径)建立文件输入流对象,增长pic字段的内容经过流对象增长。 pstmt.setBinaryStream(3, fis, fis.available()); 三个参数分别为:参数索引,流对象,流对象大小 当增长成功时,重定向到success.jsp 那么如何验证图片数据是否保存到了数据库中呢?咱们经过再次检索增长的数据,读出增长的图片数据并在页面中显示出来进行验证。 首先创建一个很是简单的页面search.jsp,此页面经过文本框使用户输入要检索的人员的姓名,检索人员的基本信息(不检索图片数据),将检索的人员数据造成JOPO对象,保存到session中,以便页面使用。 JOPO类(EmployeeObj) package com.frank.obj; public class EmployeeObj { private int employeeId; private String employeeName; private int age; public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } search.jsp页面代码以下: < %@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> < % String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> < !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html> < head> < base href="<%=basePath%>"> < title>My JSP 'search.jsp' starting page</title> < meta http-equiv="pragma" content="no-cache"> < meta http-equiv="cache-control" content="no-cache"> < meta http-equiv="expires" content="0"> < meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> < meta http-equiv="description" content="This is my page"> < !-- <link rel="stylesheet" type="text/css" href="styles.css"> --> < /head> < body> < form action="searchServlet" method="POST"> employeeName:<input type="text" name="employeeName"/> < input type="submit" value="search"/> < /form> < /body> < /html> 经过searchServlet实现基本信息的检索 package com.frank.action; 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; import javax.servlet.http.HttpSession; import com.frank.obj.EmployeeObj; import com.frank.rule.EmployeeDAO; public class SearchServlet extends HttpServlet { /** * Constructor of the object. */ public SearchServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // 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 { String employeeName=request.getParameter("employeeName"); EmployeeDAO employeeDAO=new EmployeeDAO(); EmployeeObj employeeObj=employeeDAO.getEmployeeByName(employeeName); HttpSession session=request.getSession(); session.setAttribute("employee", employeeObj); response.sendRedirect("display.jsp"); } /** * 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 { doGet(request,response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } } 此Servlet调用业务类的getEmployeeByName方法检索人员基本信息,返回EmployeeObj对象,并放入session中,固然也能够放入request中,根据本身的须要改进。页面检索成功后重定向到display.jsp进行信息的显示(稍后讲解) getEmployeeByName方法代码以下: public EmployeeObj getEmployeeByName(String employeeName){ Connection conn=null; PreparedStatement pstmt=null; ResultSet rst=null; EmployeeObj employeeObj=new EmployeeObj(); try{ Class.forName("net.sourceforge.jtds.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/SampleDB","sa",""); String strSQL="SELECT employeeId,employeeName,age FROM EmployeeInfo WHERE employeeName=?"; pstmt=conn.prepareStatement(strSQL); pstmt.setString(1, employeeName); rst=pstmt.executeQuery(); if(rst.next()){ employeeObj.setEmployeeId(rst.getInt("employeeId")); employeeObj.setEmployeeName(rst.getString("employeeName")); employeeObj.setAge(rst.getInt("age")); return employeeObj; } return null; }catch(ClassNotFoundException ex){ ex.printStackTrace(); return null; }catch(SQLException ex){ ex.printStackTrace(); return null; }finally{ try{ pstmt.close(); conn.close(); }catch(Exception ex){ } } } display.jsp负责显示查询出的结果信息,代码以下: < %@ page language="java" import="java.util.*,com.frank.obj.*" pageEncoding="ISO-8859-1"%> < % String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> < !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html> < head> < base href="<%=basePath%>"> < title>My JSP 'display.jsp' starting page</title> < meta http-equiv="pragma" content="no-cache"> < meta http-equiv="cache-control" content="no-cache"> < meta http-equiv="expires" content="0"> < meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> < meta http-equiv="description" content="This is my page"> < !-- <link rel="stylesheet" type="text/css" href="styles.css"> --> < % EmployeeObj employeeObj=(EmployeeObj)session.getAttribute("employee"); %> < /head> < body> < table width="80" border="15"> < tr> < td width="30%" align="right">EmployeeId</td> < td width="70%" align="left"><%=employeeObj.getEmployeeId() %></td> < /tr> < tr> < td width="30%" align="right">EmployeeName</td> < td width="70%" align="left"><%=employeeObj.getEmployeeName() %></td> < /tr> < tr> < td width="30%" align="right">Age</td> < td width="70%" align="left"><%=employeeObj.getAge() %></td> < /tr> < tr> < td width="30%" align="right">Pic</td> < td width="70%" align="left"><img src="displayServlet?employeeId=<%=employeeObj.getEmployeeId() %>"/></td> < /tr> < /table> < /body> < /html> 代码很是的简单,只是简单的从session中获取保存的EmployeeObj对象,而后利用jsp表达式将数据成员信息显示到页面上。可是注意粗体部分,由于咱们须要将保存的图片数据读出,而后显示成图片。因此在此咱们利用Img元素显示图片,而Src利用displayServlet的运行结果输出到客户端,做为图片的显示源。displayServlet包含参数employeeId,用来决定具体显示哪一员工的图片。 displayServlet经过Servlet输出流,将读取的图片数据发送到客户端,代码以下: package com.frank.action; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.frank.rule.EmployeeDAO; public class DisplayServlet extends HttpServlet { /** * Constructor of the object. */ public DisplayServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // 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 { response.setContentType("image/gif"); int employeeId=Integer.parseInt(request.getParameter("employeeId")); EmployeeDAO employeeDAO=new EmployeeDAO(); InputStream is=employeeDAO.getPicById(employeeId); int size=is.available(); byte[] image=new byte[size]; is.read(image); ServletOutputStream out=response.getOutputStream(); out.write(image); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } } 在此,调用业务类的getPicById方法获得图片数据的输入流。而后获取的输入流写到ServletOutputStream,这样能够在Servlet的输出端使用,即img的src中使用 getPicById代码以下: public InputStream getPicById(int employeeId){ Connection conn=null; PreparedStatement pstmt=null; ResultSet rst=null; InputStream is=null; try{ Class.forName("net.sourceforge.jtds.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/SampleDB","sa",""); String strSQL="SELECT pic FROM EmployeeInfo WHERE employeeId=?"; pstmt=conn.prepareStatement(strSQL); pstmt.setInt(1, employeeId); rst=pstmt.executeQuery(); if(rst.next()){ is=rst.getBinaryStream("pic"); return is; } return null; }catch(ClassNotFoundException ex){ ex.printStackTrace(); return null; }catch(SQLException ex){ ex.printStackTrace(); return null; }finally{ try{ pstmt.close(); conn.close(); }catch(Exception ex){ } } } 注意粗体部分 好了,咱们能够经过运行结果来进行验证,假设咱们搜索名称为Mike的人员数据,那么这样: 点search按钮,运行结果以下: 好了,显示出了检索结果,不但包括人员的基本数据,保存到数据库中的图片数据也被读出并显示成图片。 代码只是利用jsp简单的进行体现,你们能够根据本身的须要进行改进 ---很抱歉截图不能够在这个上面显示 ----
相关文章
相关标签/搜索