java存到数据库中_Java中将图片保存到数据库中(转载)

本文通过一个JSP实例详细介绍了如何在Java中将图片数据保存到数据库中。首先创建了一个录入界面,用户选择图片文件后,Servlet获取数据并通过JavaBean保存到数据库。之后,展示了如何检索已保存的图片数据并在页面上显示出来,验证了图片数据的正确存储。
摘要由CSDN通过智能技术生成

在实际的开发中,我们可能需要将图片、影音等文件直接保存到数据库中,然后通过编程方式将数据读出进行使用。例如将读出的图片数据显示出来,将读出的电影文件播放出来。

二进制数据直接保存到文件和从文件中读出非常的简单。和普通的数据库操作差别不大。只是用到部分流操作。例如各种输入输出流操作。所以深刻理解流操是非常重要的。

在此我借助于一个JSP的简单实例进行讲解。此实例保存职员数据,其中职员数据包含一个图片列。此列保存每名员工的照片。在此将照片直接保存到数据库中。首先建立职员信息表EmployeeInfo,表列非常的简单

employeeId:职员编号(自动增长);employeeName:职员姓名;age:职员年龄;pic:职员图片(image类型)

首先讲解信息的保存。先建立一个录入界面index.jsp,其中包含一个元素,用于让用户选择图片文件。

页面代码如下(不做过多讲解):

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2

3 Stringpath=request.getContextPath();4 StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";5 %>

6

7

8

9

10 ">

11

12

My JSP 'index.jsp' starting page

13

14

15

16

17

18

21

22

23

24

25

26

27 EmployeeName:

28

29

30

31 Age:

32

33

34

35 Image:

36

37

38

39

40

41

42

43

44

index.jsp

页面请求addServlet,获取录入信息,并调用JavaBean实现数据保存。Servlet代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 importjava.io.IOException;2 importjava.io.PrintWriter;3

4 importjavax.servlet.ServletException;5 importjavax.servlet.http.HttpServlet;6 importjavax.servlet.http.HttpServletRequest;7 importjavax.servlet.http.HttpServletResponse;8 import com.frank.rule.*;9 public class AddServlet extendsHttpServlet {10

11 /**

12 * Constructor of the object.13 */

14 publicAddServlet() {15 super();16 }17

18 /**

19 * Destruction of the servlet.
20 */

21 public voiddestroy() {22 super.destroy(); //Just puts "destroy" string in log23 //Put your code here

24 }25

26 /**

27 * The doGet method of the servlet.
28 *29 * This method is called when a form has its tag value method equals to get.30 *31 *@paramrequest the request send by the client to the server32 *@paramresponse the response send by the server to the client33 *@throwsServletException if an error occurred34 *@throwsIOException if an error occurred35 */

36 public voiddoGet(HttpServletRequest request, HttpServletResponse response)37 throwsServletException, IOException {38

39 String employeeName=request.getParameter("employeeName");40 int age=Integer.parseInt(request.getParameter("age"));41 String pic=request.getParameter("pic");42 EmployeeDAO employeeDAO=newEmployeeDAO();43 if(employeeDAO.employeeAdd(employeeName, age, pic))44 response.sendRedirect("success.jsp");45 else

46 response.sendRedirect("index.jsp");47

48 }49

50 /**

51 * The doPost method of the servlet.
52 *53 * This method is called when a form has its tag value method equals to post.54 *55 *@paramrequest the request send by the client to the server56 *@paramresponse the response send by the server to the client57 *@throwsServletException if an error occurred58 *@throwsIOException if an error occurred59 */

60 public voiddoPost(HttpServletRequest request, HttpServletResponse response)61 throwsServletException, IOException {62

63 doGet(request,response);64 }65

66 /**

67 * Initialization of the servlet.
68 *69 *@throwsServletException if an error occurs70 */

71 public void init() throwsServletException {72 //Put your code here

73 }74

75 }

AddServlet

此Servlet只是简单的获取页面输入的数据,其中获取的Pic为用户选择的图片路径。严格讲,获取文件需要经过验证,防止非法图片的选择,这个可以根据自己的需要改善。然后Servlet调用业务类,将获取的数据通过参数传入,进行数据的增加。其中employeeAdd方法实现数据的保存,代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public boolean employeeAdd(String employeeName,intage,String pic){2 Connection conn=null;3 PreparedStatement pstmt=null;4 FileInputStream fis=null;5 try{6 Class.forName("net.sourceforge.jtds.jdbc.Driver");7 conn=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/SampleDB","sa","");8 fis=newFileInputStream(pic);9

10 String strSQL="INSERT INTO employeeInfo VALUES(?,?,?)";11 pstmt=conn.prepareStatement(strSQL);12 pstmt.setString(1, employeeName);13 pstmt.setInt(2, age);14 pstmt.setBinaryStream(3, fis, fis.available());15 if(pstmt.executeUpdate()>0)16 return true;17 else

18 return false;19 }catch(ClassNotFoundException ex){20 ex.printStackTrace();21 return false;22 }catch(SQLException ex){23 ex.printStackTrace();24 return false;25 }catch(IOException ex){26 ex.printStackTrace();27 return false;28 }finally{29 try{30 fis.close();31 pstmt.close();32 conn.close();33 }catch(Exception ex){34

35 }36 }37 }

employeeAdd

注意粗体部分,用获取的pic信息(文件路径)创建文件输入流对象,增加pic字段的内容通过流对象增加。

pstmt.setBinaryStream(3, fis, fis.available());

三个参数分别为:参数索引,流对象,流对象大小

当增加成功时,重定向到success.jsp

运行结果如下:

4f083d948fff4ea78d0e03046fb202ce.png

增加后,显示如下代表成功:

babf1af5062910ab8272913ab4c41e5a.png

那么如何验证图片数据是否保存到了数据库中呢?我们通过再次检索增加的数据,读出增加的图片数据并在页面中显示出来进行验证。

首先建立一个非常简单的页面search.jsp,此页面通过文本框使用户输入要检索的人员的姓名,检索人员的基本信息(不检索图片数据),将检索的人员数据形成JOPO对象,保存到session中,以便页面使用。

POJO类(EmployeeObj)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public classEmployeeObj {2 private intemployeeId;3 privateString employeeName;4 private intage;5 public intgetEmployeeId() {6 returnemployeeId;7 }8 public void setEmployeeId(intemployeeId) {9 this.employeeId =employeeId;10 }11 publicString getEmployeeName() {12 returnemployeeName;13 }14 public voidsetEmployeeName(String employeeName) {15 this.employeeName =employeeName;16 }17 public intgetAge() {18 returnage;19 }20 public void setAge(intage) {21 this.age =age;22 }23

24 }

EmployeeObj

search.jsp页面代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2

3 Stringpath=request.getContextPath();4 StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";5 %>

6

7

8

9

10 ">

11

12

My JSP 'search.jsp' starting page

13

14

15

16

17

18

19

22

23

24

25

26

27 employeeName:

28

29

30

31

search.jsp

通过searchServlet实现基本信息的检索

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 importjava.io.IOException;2 importjava.io.PrintWriter;3

4 importjavax.servlet.ServletException;5 importjavax.servlet.http.HttpServlet;6 importjavax.servlet.http.HttpServletRequest;7 importjavax.servlet.http.HttpServletResponse;8 importjavax.servlet.http.HttpSession;9

10 public class SearchServlet extendsHttpServlet {11

12 /**

13 * Constructor of the object.14 */

15 publicSearchServlet() {16 super();17 }18

19 /**

20 * Destruction of the servlet.
21 */

22 public voiddestroy() {23 super.destroy(); //Just puts "destroy" string in log24 //Put your code here

25 }26

27 /**

28 * The doGet method of the servlet.
29 *30 * This method is called when a form has its tag value method equals to get.31 *32 *@paramrequest the request send by the client to the server33 *@paramresponse the response send by the server to the client34 *@throwsServletException if an error occurred35 *@throwsIOException if an error occurred36 */

37 public voiddoGet(HttpServletRequest request, HttpServletResponse response)38 throwsServletException, IOException {39

40 String employeeName=request.getParameter("employeeName");41 EmployeeDAO employeeDAO=newEmployeeDAO();42 EmployeeObj employeeObj=employeeDAO.getEmployeeByName(employeeName);43 HttpSession session=request.getSession();44 session.setAttribute("employee", employeeObj);45 response.sendRedirect("display.jsp");46 }47

48 /**

49 * The doPost method of the servlet.
50 *51 * This method is called when a form has its tag value method equals to post.52 *53 *@paramrequest the request send by the client to the server54 *@paramresponse the response send by the server to the client55 *@throwsServletException if an error occurred56 *@throwsIOException if an error occurred57 */

58 public voiddoPost(HttpServletRequest request, HttpServletResponse response)59 throwsServletException, IOException {60

61 doGet(request,response);62 }63

64 /**

65 * Initialization of the servlet.
66 *67 *@throwsServletException if an error occurs68 */

69 public void init() throwsServletException {70 //Put your code here

71 }72

73 }

SearchServlet

此Servlet调用业务类的getEmployeeByName方法检索人员基本信息,返回EmployeeObj对象,并放入session中,当然也可以放入request中,根据自己的需要改进。页面检索成功后重定向到display.jsp进行信息的显示(稍后讲解)

getEmployeeByName方法代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 publicEmployeeObj getEmployeeByName(String employeeName){2 Connection conn=null;3 PreparedStatement pstmt=null;4 ResultSet rst=null;5 EmployeeObj employeeObj=newEmployeeObj();6 try{7 Class.forName("net.sourceforge.jtds.jdbc.Driver");8 conn=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/SampleDB","sa","");9

10 String strSQL="SELECT employeeId,employeeName,age FROM EmployeeInfo WHERE employeeName=?";11 pstmt=conn.prepareStatement(strSQL);12 pstmt.setString(1, employeeName);13 rst=pstmt.executeQuery();14 if(rst.next()){15 employeeObj.setEmployeeId(rst.getInt("employeeId"));16 employeeObj.setEmployeeName(rst.getString("employeeName"));17 employeeObj.setAge(rst.getInt("age"));18 returnemployeeObj;19 }20 return null;21 }catch(ClassNotFoundException ex){22 ex.printStackTrace();23 return null;24 }catch(SQLException ex){25 ex.printStackTrace();26 return null;27 }finally{28 try{29 pstmt.close();30 conn.close();31 }catch(Exception ex){32

33 }34 }35 }

getEmployeeByName

display.jsp负责显示查询出的结果信息,代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2

3 Stringpath=request.getContextPath();4 StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";5 %>

6

7

8

9

10 ">

11

12

My JSP 'display.jsp' starting page

13

14

15

16

17

18

19

22

23 EmployeeObj employeeObj=(EmployeeObj)session.getAttribute("employee");24 %>

25

26

27

28

29

30 EmployeeId

31

32

33

34 EmployeeName

35

36

37

38 Age

39

40

41

42 Pic

43 "/>

44

45

46

47

display.jsp

代码非常的简单,只是简单的从session中获取保存的EmployeeObj对象,然后利用jsp表达式将数据成员信息显示到页面上。但是注意粗体部分,因为我们需要将保存的图片数据读出,然后显示成图片。所以在此我们利用Img元素显示图片,而Src利用displayServlet的运行结果输出到客户端,作为图片的显示源。displayServlet包含参数employeeId,用来决定具体显示哪一员工的图片。

displayServlet通过Servlet输出流,将读取的图片数据发送到客户端,代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 importjava.awt.image.BufferedImage;2 importjava.io.IOException;3 importjava.io.InputStream;4 importjava.io.PrintWriter;5

6 importjavax.imageio.ImageIO;7 importjavax.servlet.ServletException;8 importjavax.servlet.ServletOutputStream;9 importjavax.servlet.http.HttpServlet;10 importjavax.servlet.http.HttpServletRequest;11 importjavax.servlet.http.HttpServletResponse;12

13 importcom.frank.rule.EmployeeDAO;14

15 public class DisplayServlet extendsHttpServlet {16

17 /**

18 * Constructor of the object.19 */

20 publicDisplayServlet() {21 super();22 }23

24 /**

25 * Destruction of the servlet.
26 */

27 public voiddestroy() {28 super.destroy(); //Just puts "destroy" string in log29 //Put your code here

30 }31

32 /**

33 * The doGet method of the servlet.
34 *35 * This method is called when a form has its tag value method equals to get.36 *37 *@paramrequest the request send by the client to the server38 *@paramresponse the response send by the server to the client39 *@throwsServletException if an error occurred40 *@throwsIOException if an error occurred41 */

42 public voiddoGet(HttpServletRequest request, HttpServletResponse response)43 throwsServletException, IOException {44

45 response.setContentType("image/gif");46 int employeeId=Integer.parseInt(request.getParameter("employeeId"));47 EmployeeDAO employeeDAO=newEmployeeDAO();48 InputStream is=employeeDAO.getPicById(employeeId);49 int size=is.available();50 byte[] image=new byte[size];51 is.read(image);52 ServletOutputStream out=response.getOutputStream();53 out.write(image);54

55 }56

57 /**

58 * Initialization of the servlet.
59 *60 *@throwsServletException if an error occurs61 */

62 public void init() throwsServletException {63 //Put your code here

64 }65

66 }

DisplayServlet

在此,调用业务类的getPicById方法得到图片数据的输入流。然后获取的输入流写到ServletOutputStream,这样可以在Servlet的输出端使用,即img的src中使用

getPicById代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public InputStream getPicById(intemployeeId){2 Connection conn=null;3 PreparedStatement pstmt=null;4 ResultSet rst=null;5 InputStream is=null;6 try{7 Class.forName("net.sourceforge.jtds.jdbc.Driver");8 conn=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/SampleDB","sa","");9

10 String strSQL="SELECT pic FROM EmployeeInfo WHERE employeeId=?";11 pstmt=conn.prepareStatement(strSQL);12 pstmt.setInt(1, employeeId);13 rst=pstmt.executeQuery();14 if(rst.next()){15 is=rst.getBinaryStream("pic");16 returnis;17 }18 return null;19 }catch(ClassNotFoundException ex){20 ex.printStackTrace();21 return null;22 }catch(SQLException ex){23 ex.printStackTrace();24 return null;25 }finally{26 try{27 pstmt.close();28 conn.close();29 }catch(Exception ex){30

31 }32 }33 }

getPicById

注意粗体部分

好了,我们可以通过运行结果来进行验证,假设我们搜索名称为Mike的人员数据,那么这样:

f6240f0871eb732b544ca22e1cc69c69.png

点search按钮,运行结果如下:

2e94b7072ae59cbacff880969d553730.png

好了,显示出了检索结果,不但包括人员的基本数据,保存到数据库中的图片数据也被读出并显示成图片。

代码只是利用jsp简单的进行体现,大家可以根据自己的需要进行改进。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值