首页 > 解决方案 > 尝试将图像从 jsp 发送到 servlet,然后再发送到 DB2 数据库

问题描述

我正在使用连接池将图像从 jsp 发送到 servlet,然后再发送到 DB2。我的表单 enctype 是 multipart/form-data。

Servlet 中的代码:protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("**IN SaveNoteContent POST**");
    
    int id = Integer.parseInt(request.getParameter("ID"));
    System.out.println("Note ID:  " + id);
    
    String noteText = request.getParameter("noteText");
    System.out.println(noteText);
    
    
    Part filePart = request.getPart("file");
    String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
    System.out.println(filePart);
    System.out.println("FileName:  " + fileName);
    
    
    InputStream fileContent = filePart.getInputStream();
    
    ConnectionPool pool = ConnectionPool.getInstance("jdbc/cmmill26");
    connection = pool.getConnection();
    
    cn = new CreateNote(connection);
    
    cn.add_content(noteText,fileContent,id);
    
   
    
}

单独的java类。

public void add_content(String body, InputStream image, int id) throws IOException {
    
    
    System.out.println("Beginning of adding note content...");
    
    String query = "INSERT INTO note_content(body, image, audio, note_id_content) VALUES (?,?,?,?)";
    
    
    try
    {
        System.out.println(image);

        PreparedStatement ps;
        ps = dbConnection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        ps.setString(1,body);
        ps.setBinaryStream(2,image);
        // I was going to attempt to also send audio file after successfully uploading the image to blob.
        ps.setBinaryStream(3, image);
        ps.setInt(4,id);
        ps.executeUpdate();

    }
    catch (SQLException e)
    {
        System.out.println(e.getMessage());
    } // end try catch
    
    
}

我试过 setBlob 和 setBinaryStream 收到这个错误:java.lang.AbstractMethodError: Method com/ibm/db2/jcc/am/zo.setBinaryStream(ILjava/io/InputStream;)V is abstract at com.ibm.db2.jcc .am.zo.setBinaryStream(未知来源)

标签: javajspservletsdb2

解决方案


推荐阅读