Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在来自servlet的单个请求中向数据库插入多个附件_Hibernate_Jsp_Servlets - Fatal编程技术网

Hibernate 如何在来自servlet的单个请求中向数据库插入多个附件

Hibernate 如何在来自servlet的单个请求中向数据库插入多个附件,hibernate,jsp,servlets,Hibernate,Jsp,Servlets,我在数据库中插入了一个附件,但找不到如何将多个文件插入数据库。 我检查了很多网站,并以某种方式实现了如何使用servlet和jsp将单个附件插入数据库。但我无法对多个文件执行此操作。有人能帮我把多个文件插入数据库吗 这是插入单个附件的代码 package com.fileupload.attach; import java.io.IOException; import java.io.InputStream; import java.io.FileOutputStream; import ja

我在数据库中插入了一个附件,但找不到如何将多个文件插入数据库。 我检查了很多网站,并以某种方式实现了如何使用servlet和jsp将单个附件插入数据库。但我无法对多个文件执行此操作。有人能帮我把多个文件插入数据库吗

这是插入单个附件的代码

package com.fileupload.attach;

import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@MultipartConfig(maxFileSize = 16177215)
// upload file's size up to 16MB
public class FileUploadDBServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final int BUFFER_SIZE = 4096;
    // database connection settings
    private String dbURL = "jdbc:mysql://localhost:3306/mysql";
    private String dbUser = "root";
    private String dbPass = "password";

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // gets values of text fields
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");

        InputStream inputStream = null; // input stream of the upload file

        // obtains the upload file part in this multipart request
        Part filePart = request.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
            inputStream = filePart.getInputStream();
        }

        Connection conn = null; // connection to the database
        String message = null; // message will be sent back to client

        try {
            // connects to the database
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn = DriverManager.getConnection(dbURL, dbUser, dbPass);

            // constructs SQL statement
            String sql = "INSERT INTO contacts (first_name, last_name, photo) values (?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, firstName);
            statement.setString(2, lastName);

            if (inputStream != null) {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(3, inputStream);
            }

            // sends the statement to the database server
            int row = statement.executeUpdate();
            if (row > 0) {
                message = "File uploaded and saved into database";
            }

            String filepath = "D:/Dev/JavaWorkSpaceNew/FileUploadDatabase/WebContent/FromDb.jpg";
            String sql1 = "SELECT photo FROM contacts WHERE first_name=? AND last_name=?";
            PreparedStatement statement1 = conn.prepareStatement(sql1);
            statement1.setString(1, firstName);
            statement1.setString(2, lastName);

            ResultSet result = statement1.executeQuery();
            if (result.next()) {
                Blob blob = result.getBlob("photo");
                InputStream inputStream1 = blob.getBinaryStream();
                OutputStream outputStream = new FileOutputStream(filepath);

                int bytesRead = -1;
                byte[] buffer = new byte[BUFFER_SIZE];
                while ((bytesRead = inputStream1.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                inputStream1.close();
                outputStream.close();
                System.out.println("File saved");

            }
        } catch (SQLException ex) {
            message = "ERROR: " + ex.getMessage();
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            // sets the message in request scope
            request.setAttribute("Message", message);

            // forwards to the message page
            getServletContext().getRequestDispatcher("/Message.jsp").include(
                    request, response);
        }
    }
}

您必须将表单中的多部分请求作为不同文件的数组来获取,并将每个数组项作为每个文件进行处理,然后将其保存到您的数据库中..我使用servlet和jdbc实现了此功能。您是否介意为我发布一个有关此功能的示例。谢谢。