会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132433个问题

老师我还是没有发现问题在哪,报错信息又添加了一行和EncodingFilter第33行有关,我就很郁闷了,而且我发现把EncodingFilter的第33行注释掉之后就浏览器就没有任何响应信息呈现一个空白页面,实在没有头绪了,请老师指点

image.png

image.png

报错信息如下:

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)
	com.bjsxt.filter.EncodingFilter.doFilter(EncodingFilter.java:33)

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)
	com.bjsxt.filter.EncodingFilter.doFilter(EncodingFilter.java:33)

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


源码如下:

EncodingFilter:

package com.bjsxt.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * 处理请求编码Filter
 */
public class EncodingFilter implements Filter {
    //多线程下使用成员变量不安全,但只用来读取就不存在安全问题,就怕又读又写
    private String encoding;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //根据name取对应的valuie
        String code = filterConfig.getInitParameter("code");
        //赋值给成员变量
        this.encoding = code;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        if(encoding==null){//说明web.xml中没有设置编码格式
            //设置请求编码    默认的编码
            servletRequest.setCharacterEncoding("utf-8");
        }else{
            servletRequest.setCharacterEncoding(this.encoding);
        }

        System.out.println("Encoding    Filter........");

        //filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }
}



FileUpLoadServlet:

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类型对象
        Part part = req.getPart("file");//参数给form表单中文件上传的name
        /*//获取文件上传的字节流
        InputStream inputStream = part.getInputStream();*/
        //给文件改名     随机生成唯一的字符串+文件扩展名
        String fileName = UUID.randomUUID().toString() + part.getSubmittedFileName().substring(part.getSubmittedFileName().lastIndexOf("."));
        //路径转换
        ServletContext servletContext = this.getServletContext();
        //把相对于webapps目录下的相对路径转换成绝对路径
        String realPath = servletContext.getRealPath("images/" + fileName);
        //将上传的对象做IO流处理,并通过字符输出流写到路径中
        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> ITBZ </TITLE></HEAD>");
        out.println("<BODY>");
        out.println("文件上传成功!"+desc);
        out.println("</BODY>");
        out.println("</HTML>");

        out.flush();
        out.close();
    }
}


image.png

















JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 18556楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/浮动与定位 18558楼

老师我由于暂时没有视频中的测试环境,用postman做测试

http://127.0.0.1:8080/login/doLogin?username=13888001001&password=001001。提示如下错误:

{

"timestamp": "2020-08-27 14:40:36",

"status": 400,

"error": "Bad Request",

"message": "Required request body is missing: public com.bjsxt.vo.AjaxResult com.bjsxt.controller.system.LoginController.login(com.bjsxt.dto.LoginBodyDto,javax.servlet.http.HttpServletRequest)",

"path": "/login/doLogin"

}


但是如果放在body中的form-data中提交,又会显示如下错误:

image.png


所以如何在postman中测试呢?


JAVA 全系列/第二十一阶段:分布式医疗云平台/系统管理前后端开发(旧) 18560楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Ajax 18563楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 18565楼

老师你好!

麻烦你看看这是什么问题?

image.png


uwsgi.log文件:

*** Starting uWSGI 2.0.18 (64bit) on [Sun Oct 18 18:01:18 2020] ***

compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-39) on 18 October 2020 07:20:44

os: Linux-3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020

nodename: localhost.localdomain

machine: x86_64

clock source: unix

pcre jit disabled

detected number of CPU cores: 1

current working directory: /pythoncodes/script

writing pidfile to /pythoncodes/script/uwsgi.pid

detected binary path: /pythoncodes/py3env/bin/uwsgi

*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 

chdir() to /pythoncodes/netshop/

your processes number limit is 3795

your memory page size is 4096 bytes

detected max file descriptor number: 1024

!!! no /etc/mime.types file found !!!

lock engine: pthread robust mutexes

thunder lock: enabled

uWSGI http bound on 0.0.0.0:8000 fd 3

uwsgi socket 0 bound to UNIX address /pythoncodes/script/uwsgi.sock fd 6

*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 

Python version: 3.8.2 (default, Oct 18 2020, 12:37:31)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

PEP 405 virtualenv detected: /pythoncodes/py3env/

Set PythonHome to /pythoncodes/py3env/

Python main interpreter initialized at 0x1d5aca0

*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 

python threads support enabled

your server socket listen backlog is limited to 100 connections

your mercy for graceful operations on workers is 60 seconds

mapped 486672 bytes (475 KB) for 5 cores

*** Operational MODE: preforking ***

Traceback (most recent call last):

  File "/pythoncodes/py3env/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module>

    from . import _mysql

ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "./netshop/wsgi.py", line 16, in <module>

    application = get_wsgi_application()

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application

    django.setup(set_prefix=False)

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/__init__.py", line 24, in setup

    apps.populate(settings.INSTALLED_APPS)

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate

    app_config.import_models()

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models

    self.models_module = import_module(models_module_name)

  File "/usr/local/python38/lib/python3.8/importlib/__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>

    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 47, in <module>

    class AbstractBaseUser(models.Model):

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/db/models/base.py", line 117, in __new__

    new_class.add_to_class('_meta', Options(meta, app_label))

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/db/models/base.py", line 321, in add_to_class

    value.contribute_to_class(cls, name)

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/db/models/options.py", line 204, in contribute_to_class

    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/db/__init__.py", line 28, in __getattr__

    return getattr(connections[DEFAULT_DB_ALIAS], item)

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/db/utils.py", line 201, in __getitem__

    backend = load_backend(db['ENGINE'])

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/db/utils.py", line 110, in load_backend

    return import_module('%s.base' % backend_name)

  File "/usr/local/python38/lib/python3.8/importlib/__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "/pythoncodes/py3env/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 15, in <module>

    import MySQLdb as Database

  File "/pythoncodes/py3env/lib/python3.8/site-packages/MySQLdb/__init__.py", line 24, in <module>

    version_info, _mysql.version_info, _mysql.__file__

NameError: name '_mysql' is not defined

unable to load app 0 (mountpoint='') (callable not found or import error)

*** no app loaded. going in full dynamic mode ***

*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 

*** uWSGI is running in multiple interpreter mode ***

spawned uWSGI master process (pid: 4079)

spawned uWSGI worker 1 (pid: 4081, cores: 1)

spawned uWSGI worker 2 (pid: 4082, cores: 1)

spawned uWSGI worker 3 (pid: 4083, cores: 1)

spawned uWSGI worker 4 (pid: 4084, cores: 1)

spawned uWSGI worker 5 (pid: 4085, cores: 1)

spawned uWSGI http 1 (pid: 4086)

--- no python application found, check your startup logs for errors ---

[pid: 4082|app: -1|req: -1/1] 192.168.126.1 () {38 vars in 737 bytes} [Sun Oct 18 10:10:11 2020] GET /admin => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)

--- no python application found, check your startup logs for errors ---

[pid: 4083|app: -1|req: -1/2] 192.168.126.1 () {40 vars in 700 bytes} [Sun Oct 18 10:10:11 2020] GET /favicon.ico => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)

--- no python application found, check your startup logs for errors ---

[pid: 4084|app: -1|req: -1/3] 192.168.126.1 () {38 vars in 737 bytes} [Sun Oct 18 10:10:11 2020] GET /admin => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)

--- no python application found, check your startup logs for errors ---

[pid: 4081|app: -1|req: -1/4] 192.168.126.1 () {40 vars in 700 bytes} [Sun Oct 18 10:10:11 2020] GET /favicon.ico => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)


Python 全系列/下架-第十二阶段:Python_大型电商项目(5天后下架)/Django项目阶段-电商项目(旧) 18566楼

先启动服务器端、再启动客户端后 服务器端没有收到信息 但是客户端收到了反馈

package com.bjsxt.net.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器端  要先启动服务器端
 */
public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("---------------服务器端已启动--------------------");
        //(1)创建ServerSocket对象
        ServerSocket server = new ServerSocket(9999);
        //(2)监听是否有客户端来请求连接
        Socket client = server.accept();
        //(3)获取信息  输入流
        InputStream is = client.getInputStream();
        //(4)反馈已收到  输出流
        OutputStream os = client.getOutputStream();
        os.write("收到了".getBytes());
        //(5)关闭流、关闭Socket
        if(os!=null){
            os.close();
        }
        if(is!=null){
            is.close();
        }
        if(client!=null){
            client.close();
        }
    }
}
package com.bjsxt.net.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 客户端    要先启动服务器端
 */
public class Test {
    public static void main(String[] args) throws IOException {
        //(1)创建Socket对象
        Socket client = new Socket("127.0.0.1",9999);
        //(2)获取输出流 向服务器端发送信息
        OutputStream os = client.getOutputStream();
        os.write('a');
        //(3)获取输入流  获取服务器端发来的反馈
        InputStream is = client.getInputStream();
        byte[] buf = new byte[1024];  //中转站
        int len=0;  //读到的字节个数
        while((len=is.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }
        //(4)关闭流
        if(is!=null){
            is.close();
        }
        if(os!=null){
            os.close();
        }
        if(client!=null){
            client.close();
        }
    }
}

第一次运行的时候 好像跳出了什么拦截信息 没注意看 顺手关了 然后发现发现运行结果如下

image.png


image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 18568楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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