org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn‘t contain

问题

Servlet+JSP开发javaWeb项目,发送请求控制台报错

详细问题

form表单核心代码

<form action="${pageContext.request.contextPath}/seller/addgoods" method="post">
</form>

处理类核心代码

@MultipartConfig
@WebServlet(name = "SellerAddGoodsServlet", value = "/seller/addgoods")
public class SellerAddGoodsServlet extends HttpServlet {
    
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
    }
}

解决方案

对于form表单增加enctype="multipart/form-data"属性
对于笔者即

<form action="${pageContext.request.contextPath}/seller/addgoods" method="post" enctype="multipart/form-data">
</form>

解决原因

根据错误日志,报错信息为:

org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded

这个错误表示请求的内容类型不是 multipart/form-data 或 multipart/mixed,而是 application/x-www-form-urlencoded,因此无法使用文件上传的方式解析请求。

要解决这个问题,需要确保在发送请求时使用正确的 enctype 属性来指定表单的编码类型为 multipart/form-data。例如,在 HTML 表单中添加 enctype=“multipart/form-data”:

确保在发送请求时,表单使用正确的 enctype 属性,并且确保服务器端的请求处理代码能够正确解析 multipart/form-data 类型的请求。

参考文献

解决原因参考chatgpt

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/T_Y_F_/article/details/131160091
今日推荐