我正在制作一个学校项目,需要使用jsp将考试答案以图像的形式上传到数据库中。为此,我要求用户从浏览器输入图像,然后将其作为 servlet jsp 中的参数获取。但是,当我尝试运行程序并将图像提交到数据库时,出现错误“无法处理部件,因为未提供多部件配置”。我已经尝试过之前在类似问题中提出的解决方案,但错误仍然存在。我做错了什么?
<form method="POST" action="submitExam.jsp" enctype="multipart/form-data">
<input type="file" name="answer" class="form-control"required>
<input type="submit" value="submit">
</form>
<%@page import="javax.servlet.annotation.MultipartConfig"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.sql.*" %>
<%Class.forName("com.mysql.jdbc.Driver");%>
<%
Connection conn;
PreparedStatement pst;
Part part = request.getPart("answer");
InputStream in = null;
if(part != null)
{
in = part.getInputStream();
}
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/student_information_management_system","root","");
pst = conn.prepareStatement("INSERT into exam_answers(answer) VALUES (?)");
pst.setBlob(1, in);
pst.executeUpdate();
%>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>submitExam</servlet-name>
<jsp-file>/submitExam.jsp</jsp-file>
<multipart-config>
<location>/temp</location>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>submitExam</servlet-name>
<url-pattern>/submitExam.jsp</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
我尝试使用建议的解决方案中的方法这里但仍然不起作用
您的错误: `“无法处理零件,因为未提供多零件配置”。
您的 JSP 必须添加
<%@ page import="javax.servlet.http.Part" %>