java.lang.IllegalStateException:在没有多部分配置的情况下调用Request.getPart。将@MultipartConfig添加到servlet,或将multipart-config元素添加到web.xml
当我尝试将图像添加到项目中时,我收到此错误
我的HTML是
<form action="../include/crud.jsp" method="POST">
<input type='file' onchange="readURL(this);" style="display:none;" name="photo" />
</form>
我的crud.jsp是
<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.PreparedStatement"%>
<%@ include file="dbconfig.jsp" %>
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="javax.swing.*" %>
<%@page
import="org.apache.commons.fileupload.FileItem"
import="org.apache.commons.fileupload.FileUploadException"
import="org.apache.commons.fileupload.disk.DiskFileItemFactory"
import="org.apache.commons.fileupload.servlet.ServletFileUpload"
%>
<%
Part filePart = request.getPart("photo"); // Retrieves <input type="file" name="file">
String fileName = filePart.getSubmittedFileName();
InputStream fileContent = filePart.getInputStream();
try {
conn.setAutoCommit(false);
PreparedStatement preparedStatement = null;
String act = request.getParameter("btncrud");
if (act == null) {
//no button has been selected
} else if (act.equals("Add")) {
//Add button was pressed
String add_disbursement = "INSERT INTO photo_table "
+ "(ri_photo,ri_photo_name)"
+ " VALUES (?,?)";
preparedStatement.setBlob(1,fileContent);
preparedStatement.setString(2,fileName);
preparedStatement.executeUpdate();
} else if (act.equals("Update")) {
//update button was pressed
} else {
//someone has altered the HTML and sent a different value!
}
%>
FYI
列ri_photo
的类型为mediumblob
UPDATE
已添加cos-multipart.jar
和import=" com.oreilly.servlet.MultipartRequest"
仍为同一错误
如何在JSP页面中添加@MultipartConfig
答案 0 :(得分:0)
只要请求正文大小大于默认值,就会发生此异常。
如果您想要自定义大小或大于默认大小,则需要覆盖web.xml中的multipart-config设置 你可以参考以下内容:
<multipart-config>
<max-file-size>*size in bytes*</max-file-size>
<max-request-size>*size in bytes*</max-request-size>
<file-size-threshold>*size in bytes*</file-size-threshold>
</multipart-config>