【问题标题】:how to insert and fetch image,audio and video in database server using servlet如何使用 servlet 在数据库服务器中插入和获取图像、音频和视频
【发布时间】:2020-03-06 06:36:28
【问题描述】:

如果我使用连接 MySQL 并获取用户上传的音频、图像和视频的 java servlet,如何使用 servlet 将没有 blob、音频和视频的图像从 HTML 插入数据库。我还想从数据库中检索并在用户可以播放的浏览器中显示它。请帮助我找到正确的解决方案。

下面是使用 servlet 将图像插入数据库的代码。我以 blob 的形式存储了图像,这是不可取的。我需要另一种方式来存储图像以及一些音频和视频文件。

ImageServlet.java

package com.image.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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("/image")
@MultipartConfig(maxFileSize = 16177215)
public class ImageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         final int BUFFER_SIZE = 4096;

        InputStream inputStream = null; 

        Part filePart = req.getPart("photo");
        if (filePart != null) {
            // prints out some information for debugging
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());

            //obtains input stream of the upload file
            //the InputStream will point to a stream that contains
            //the contents of the file
            inputStream = filePart.getInputStream();
        }

        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

try {

            Class.forName("com.mysql.jdbc.Driver");

            String dburl="jdbc:mysql://localhost:3306/employee_db";
            con=DriverManager.getConnection(dburl,"root","root");

            String sql = "INSERT INTO image_tutor values(?)";
            pstmt=con.prepareStatement(sql);
            if (inputStream != null) {
                //files are treated as BLOB objects in database
                //here we're letting the JDBC driver
                //create a blob object based on the
                //input stream that contains the data of the file
                pstmt.setBlob(1, inputStream);
            }
            //sends the statement to the database server
            int row = pstmt.executeUpdate();
            if (row > 0) {
                System.out.println( "File uploaded and saved into database");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(pstmt!=null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }//if block
        }//finally block
    }//doPost block
}//class block

image.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="./image" method="post" enctype="multipart/form-data">
    <input type="file" name="photo" size="50" placeholder="Upload Your Image" required/><br><br>
            <input type="submit" value="Save">
</form>
</body>
</html>

【问题讨论】:

    标签: java mysql servlets jdbc


    【解决方案1】:

    最常用的想法是将文件路径保存在数据库中,并将内容保存在服务器可以读取的其他位置。我会为你说的所有文件类型这样做。

    【讨论】:

      猜你喜欢
      • 2015-01-26
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多