MySQL 如何在mysql中存储图片并通过jsp检索显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22320957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:11:10  来源:igfitidea点击:

How to store images in mysql and retrieve and display them through jsp

mysqljspjdbc

提问by user3400652

i am working with jdbc,mysql,html,jsp.i want to store an image into database through jdbc code and retrive and display them using jsp.please help urgently.I'm creating an online shopping website as a project.

我正在使用 jdbc、mysql、html、jsp。我想通过 jdbc 代码将图像存储到数据库中,并使用 jsp 检索和显示它们。请紧急帮助。我正在创建一个在线购物网站作为一个项目。

回答by jmail

This is very simple to store an image in MySQL database using JSP :

使用 JSP 在 MySQL 数据库中存储图像非常简单:

Go Step By Step:

一步一步来:

Step 1- Create this table in database

步骤 1- 在数据库中创建此表

create table upload_image
(
    iImageID int AUTO_INCREMENT primary key,
    bImage longblob
);

Step2- Save This code as uploadimage.jsp

Step2- 将此代码另存为uploadimage.jsp

<%@ page language="java"  errorPage="" %>
<html>
<head>
<title>Image insert into database</title>
</head>

<body>
<form name="frm" action="saveImage.jsp" enctype="multipart/form-data" method="post">
 <input type="file" name="uProperty" /> <br>
 <input type="submit" name="goUpload" value="Upload" />
</form>
</body>
</html>

Step3- Save This code as saveImage.jsp

Step3- 将此代码另存为 saveImage.jsp

<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.io.output.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%
   Connection conn=null;
   Class.forName("com.mysql.jdbc.Driver").newInstance();
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/gimtech","root", "harit");

   PreparedStatement psImageInsertDatabase=null;

   byte[] b=null;
   try{
      String sqlImageInsertDatabase="insert into upload_image (bImage) values(?)";
      psImageInsertDatabase=conn.prepareStatement(sqlImageInsertDatabase);

      DiskFileItemFactory factory = new DiskFileItemFactory();

      ServletFileUpload sfu = new ServletFileUpload(factory);
      List items = sfu.parseRequest(request);

      Iterator iter = items.iterator();

      while (iter.hasNext()) {
         FileItem item = (FileItem) iter.next();
         if (!item.isFormField()) {
              b = item.get();
          }
      }

      psImageInsertDatabase.setBytes(1,b);
      psImageInsertDatabase.executeUpdate();
   }
   catch(Exception e)
   {
     e.printStackTrace();
     response.sendRedirect("addimage.jsp");
   }

%>

And you should must add the jar files:

您必须添加 jar 文件:

  1. commons-fileupload-1.2.1.jar
  2. commons-io-1.4.jar
  3. mysql-connector-java-5.1.5-bin.jar
  1. commons-fileupload-1.2.1.jar
  2. commons-io-1.4.jar
  3. mysql-connector-java-5.1.5-bin.jar