未加星标

Save and Retrieve Image from MySQL Database Using Servlet and JSP

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
Save and Retrieve Image from mysql Database Using Servlet and JSP Database

Create a table in mysql database with following schema.

Database Name:test

Table Name:data


Save and Retrieve Image from MySQL Database Using Servlet and JSP

Here I have used MEDIUMBLOB datatype to store image in database. It supports maximum 16 MB file. You can also use other types like TINYBLOB (255 bytes), BLOB (64 KB), LONGBLOB (4 GB).

Project

Create a dynamic web project with name ImageUploadDownload . The project have following files.

index.jsp

It contains a form to choose image to upload. When user chooses an image and submit form, the request is sent to UploadImage.java .

<html> <head> <title>UploadImage</title> </head> <body> <b>Upload | <a href="view.jsp">View</a></b><br/><br/> <formaction="uploadImage" method="post" enctype="multipart/form-data"> <inputtype="file" name="image" required="required"/><br/><br/> <inputtype="submit"/> </form> </body> </html> UploadImage.java

It is a servlet that is responsible for storing image in database.

package com; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet("/uploadImage") @MultipartConfig(maxFileSize = 16177216) public class UploadImage extends HttpServlet{ @Override protected void doPost(HttpServletRequestreq, HttpServletResponseresp) throws ServletException, IOException { String connectionURL = "jdbc:mysql://localhost:3306/test"; String user = "root"; String pass = "root"; int result = 0; Connectioncon = null; Partpart = req.getPart("image"); if(part != null){ try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(connectionURL, user, pass); PreparedStatementps = con.prepareStatement("insert into data(image) values(?)"); InputStreamis = part.getInputStream(); ps.setBlob(1, is); result = ps.executeUpdate(); } catch(Exception e){ e.printStackTrace(); } finally{ if(con != null){ try{ con.close(); } catch(Exception e){ e.printStackTrace(); } } } } if(result > 0){ resp.sendRedirect("result.jsp?message=Image+Uploaded"); } else{ resp.sendRedirect("result.jsp?message=Some+Error+Occurred"); } } } result.jsp

This page shows success if image is successfully saved in database otherwise shows error.

<% String message = request.getParameter("message"); if(message != null){ out.print(message); } %> view.jsp

It contains a form with a text field. User have to enter unique image id and submit the form. The request is sent to viewImage.jsp page.

<html> <head> <title>ViewImage</title> </head> <body> <b>View | <a href="index.jsp">Upload</a></b><br/><br/> <formaction="viewImage.jsp" method="post"> <inputtype="text" name="id" required="required" placeholder="enter image id.."/><br/><br/> <inputtype="submit"/> </form> </body> </html> viewImage.jsp

It contains an image tag to display the image.

<html> <head> <title>ViewImage</title> </head> <body> <b>View | <a href="index.jsp">Upload</a></b><br/><br/> <% String id = request.getParameter("id"); %> <imgsrc="getImage.jsp?id=<%=id%>" width="400px"/> </body> </html> getImage.jsp

It is responsible for fetching image form database with given image id and display it.

<[email protected] import="java.sql.Blob"%> <[email protected] import="java.io.OutputStream"%> <[email protected] import="java.sql.ResultSet"%> <[email protected] import="java.sql.PreparedStatement"%> <[email protected] import="java.sql.DriverManager"%> <[email protected] import="java.sql.Connection"%> <% String id = request.getParameter("id"); String connectionURL = "jdbc:mysql://localhost:3306/test"; String user = "root"; String pass = "root"; Connection con = null; try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(connectionURL, user, pass); PreparedStatement ps = con.prepareStatement("select * from data where id=?"); ps.setString(1, id); ResultSet rs = ps.executeQuery(); if(rs.next()){ Blob blob = rs.getBlob("image"); byte byteArray[] = blob.getBytes(1, (int)blob.length()); response.setContentType("image/gif"); OutputStream os = response.getOutputStream(); os.write(byteArray); os.flush(); os.close(); } } catch(Exception e){ e.printStackTrace(); } finally{ if(con != null){ try{ con.close(); } catch(Exception e){ e.printStackTrace(); } } } %> Screenshots
Save and Retrieve Image from MySQL Database Using Servlet and JSP
Save and Retrieve Image from MySQL Database Using Servlet and JSP

Feel free to ask your queries in comment section.

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: SQLMySQLJSPTIEDI
分页:12
转载请注明
本文标题:Save and Retrieve Image from MySQL Database Using Servlet and JSP
本站链接:http://www.codesec.net/view/480549.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(29)