gpt4 book ai didi

java - 如何使用servlet在数据库服务器中插入和获取图像、音频和视频

转载 作者:行者123 更新时间:2023-11-29 15:30:15 24 4
gpt4 key购买 nike

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

下面是使用 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

图像.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 - 如何使用servlet在数据库服务器中插入和获取图像、音频和视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58786702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com