会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 181楼
JAVA 全系列/第五阶段:JavaWeb开发/Ajax技术详解(旧) 182楼

老师我这个代码第一遍跟着老师敲是没有问题的,我在另一个项目里面又试了一遍老是说我的post方法的enctype有问题,可是我设置了呀,老师我该怎么解决


Type 异常报告

消息 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 null

描述 服务器遇到一个意外的情况,阻止它完成请求。

Exception

javax.servlet.ServletException: 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 null
	org.apache.catalina.connector.Request.parseParts(Request.java:2913)
	org.apache.catalina.connector.Request.getParts(Request.java:2775)
	org.apache.catalina.connector.Request.getPart(Request.java:2944)
	org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1105)
	com.bjsxt.servlet.FileUpLoadServlet.doPost(FileUpLoadServlet.java:32)
	com.bjsxt.servlet.FileUpLoadServlet.doGet(FileUpLoadServlet.java:20)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

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 null
	org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:140)
	org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.getMultiPartStream(FileItemIteratorImpl.java:194)
	org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.findNextItem(FileItemIteratorImpl.java:213)
	org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.<init>(FileItemIteratorImpl.java:131)
	org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:255)
	org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:279)
	org.apache.catalina.connector.Request.parseParts(Request.java:2873)
	org.apache.catalina.connector.Request.getParts(Request.java:2775)
	org.apache.catalina.connector.Request.getPart(Request.java:2944)
	org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1105)
	com.bjsxt.servlet.FileUpLoadServlet.doPost(FileUpLoadServlet.java:32)
	com.bjsxt.servlet.FileUpLoadServlet.doGet(FileUpLoadServlet.java:20)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

):注意 主要问题的全部 stack 信息可以在 server logs 里查看


JAVA:

package com.bjsxt.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

@WebServlet("/fileupload.do")
@MultipartConfig//基于注解开发的文件上传
public class FileUpLoadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码
        req.setCharacterEncoding("utf-8");
        //设置响应编码
        resp.setContentType("text/html;charset=utf-8");
        //获取文件描述
        String desc = req.getParameter("desc");
        //获取part对象  参数给form表单中上传文件的name
        Part part = req.getPart("file");
        /*//设置唯一的文件名,避免因为文件名重复而造成文件覆盖,
        * 先截取文件的后缀(扩展名),然后与随机生成唯一的字符串拼接
        * UUID.randowUUID()随机生成一个唯一的字符串,返回UUID类型,需要调用toString()返回字符串类型*/
        String name = UUID.randomUUID().toString() + part.getSubmittedFileName().substring(part.getSubmittedFileName().lastIndexOf("."));
        //将相对路径转换为绝对路径
        ServletContext servletContext = this.getServletContext();
        String realPath = servletContext.getRealPath("images/" + name);
        System.out.println(realPath);
        //将上传的对象做IO流chuli,并通过字符输出流写入到路径中
        part.write(realPath);

        PrintWriter out = resp.getWriter();
        /*拼接HTML页面*/
        out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");
        out.println("<HTML>");
        out.println("<HEAD><TITLE>临江仙.文件上传</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("文件上传成功!"+desc);
        out.println("<BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
}


HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body><!--
        get:以字符型方式提交,
        post:既可以以字节方式提交也可以字符方式提交
        -->
    <form   action="fileupload.do"  method="post"   enctype="multipart/form-data">
        文件描述:<input type="text" name="desc"/>
        文件上传:<input type="file" name="file"/>
        <input type="submit"   value="OK"/>
    </form>
</body>
</html>











JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 183楼
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 184楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解 187楼

老师,只要是设计 HTML页面的,我都无法执行


页面报错截图

image.png

网页控制台报错截图

image.png

html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form ACTION="/reset.do" method="post">
        搜索:<input type="text" name="search"/><br/>
        <input type="submit" value="Search"/>
    </form>
</body>
</html>


xml配置文件代码

<servlet>
    <servlet-name>Demo6_RedirectServlet</servlet-name>
    <servlet-class>com.bjsxt.servlet.ServletDemo.Demo6_RedirectServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Demo6_RedirectServlet</servlet-name>
    <url-pattern>/reset.do</url-pattern>
</servlet-mapping>


java代码

package com.bjsxt.servlet.ServletDemo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/*
重定向响应
一般有两次请求:
一 、请求自己的服务器,发现在头文件中的location中有url
二、请求url地址
 */
public class Demo6_RedirectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String search = req.getParameter("search");
        resp.sendRedirect("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd="+ URLEncoder.encode(search,"utf-8"));
    }
}

还有就是上面java代码中,

String search = req.getParameter("search");

括号中的search代表的是什么意思?

JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 188楼
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 192楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 193楼

image.png

<%--
  Created by IntelliJ IDEA.
  User: CYF
  Date: 2022/8/13
  Time: 7:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
    <script type="text/javascript" src="js/jquery-3.6.0.js"></script>
    <script>
      $(function() {
        //初始化用户数据
        getData();
        //添加用户
        $("#add").click(function () {
          addOrUpdateUser("addUser");

        });
        //更新用户
        $("#update").click(function () {
          addOrUpdateUser("updateUser");

        })


        function getData() {
          $.getJSON("user.do", {flag: "getData"}, function (result) {
            listUser(result);

          });
        }

        //获取页面用户输出数据
        function addOrUpdateUser(flag) {
          var userid = $("#id").val();
          var username = $("#username").val();
          var password = $("#password").val();
          var salary = $("#salary").val();
          var birthday = $("#birthday").val();

          var data = {
            userid: userid,
            username: username,
            password: password,
            salary: salary,
            birthday: birthday,
            flag: flag,

          };
          $.get("user.do", data, function (result) {
            alert(result)
            location.reload();
          })
        }

        function preUpdateUser(userid) {
          var arr = new Array();
          //遍历选择行中的用户数据
          $("#" + userid).closest("tr").children().each(function (index, ele) {
            if (index <= 4) {
              arr[index] = ele.innerText;
            }

          })
          //设置编辑区域的用户数据
          $("#id").val(arr[0])
          $("#id").val("readonly", true)
          $("#username").val(arr[1])
          $("#password").val(arr[2])
          $("#salary").val(arr[3])
          $("#birthday").val(arr[4])


        }


        //遍历用户数据,拼接成html
        function listUser(obj) {

          var str = ""
          $.each(obj, function () {
            str += "<tr align='center'>" +
                    "<td id='" + this.id + "'>" + this.id + "</td>" +
                    "<td id='" + this.username + "'>" + this.username + "</td>" +
                    "<td id='" + this.password + "'>" + this.password + "</td>" +
                    "<td id='" + this.salary + "'>" + this.salary + "</td>" +
                    "<td id='" + this.birthday + "'>" + this.birthday + "</td>" +
                    "<td> <a href='#' onclick='preUpdateUser(" + this.id + ")' c>更新</a>&nbsp;&nbsp;<a href='#'>删除</a></td></tr>";
          })
          $("#tBody").prepend(str)

        }
      }


    </script>
  </head>
  <body>
  <div>
    <table align="center" width="60%" border="1">
      <tr>
        <td>ID:</td>
        <td><input type="text" name="id" id="id"/></td>
        <td>姓名:</td>
        <td><input type="text" name="username" id="username"/></td>
        <td>密码:</td>
        <td><input type="text" name="password" id="password"/></td>
      </tr>
      <tr>
        <td>收入:</td>
        <td><input type="text" name="salary" id="salary"/></td>
        <td>出生日期:</td>
        <td><input type="text" name="birthday" id="birthday"/></td>
        <td colspan="2"></td>
      </tr>
      <tr align="center">
        <td colspan="6">
          <input type="button" value="添加用户" id="add" />
          <input type="button" value="更新用户" id="update"/>
        </td>
      </tr>
    </table> <hr/>
    <table align="center" width="60%" bgcolor="" border="1" id="myTable">
      <thead>
      <tr align="center">
        <td>ID</td>
        <td>姓名</td>
        <td>密码</td>
        <td>收入</td>
        <td>生日</td>
        <td>操作</td>
      </tr>
      </thead>
      <tbody id="tBody"></tbody>
    </table>
  </div>
  </body>
</html>

这是什么问题呀

JAVA 全系列/第五阶段:JavaWeb开发/Ajax技术详解(旧) 194楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637