Content-Type是什么

目录

Content-Type是什么

获取方式

设置方式

常见类型

application/x-www-form-urlencoded

multipart/form-data

application/json

text/xml

text/html

text/plain


Content-Type是什么

Content-Type出现在请求标头和响应标头中,意思是内容类型,用于指定请求数据和响应数据的类型。客户端和服务端对不同数据类型的处理方式不同。

获取方式

可以通过HttpServletRequest对象来获取

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class DemoController {
    
    private final HttpServletRequest request;
    
    @RequestMapping("/test")
    public String demo() {
        String contentType = request.getContentType();
        System.out.println(contentType);
        return contentType;
    }
}

测试结果:

设置方式

可以通过HttpServletResponse对象来设置

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequiredArgsConstructor
public class DemoController {

    private final HttpServletResponse response;

    @RequestMapping("/test")
    public void demo() throws IOException {
        System.out.println("test success!");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/x-www-form-urlencoded;charset=utf-8");
        String value = new ObjectMapper().writeValueAsString("test success!");
        response.getWriter().write(value);
    }
}

测试结果:

常见类型

application/x-www-form-urlencoded

用于表单提交,请求方式为POST,数据以键值对的形式携带在请求体中。如果包含中文或者特殊字符,会进行URL转码。

URL转码:URL转码是将URL中的非法字符替换为特殊字符序列的过程。URL中只允许出现英文字母、数字以及部分特殊字符,而其他字符(例如中文和一些特殊字符)是不允许直接出现的。如果URL中包含了这些非法字符,就需要进行转码,将其转换为URL允许的形式。中文一般使用utf-8编码进行转码。

例如:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form method="POST" action="http://localhost:8080/test" enctype="application/x-www-form-urlencoded">
        用户名:<input type="text" name="username" />
        密码:<input type="password" name="password" />
        <input type="submit" value="登录" />
    </form>
</body>

</html>

接收方式:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @PostMapping("/test")
    public String demo(String username, String password) {
        return "username=" + username + "password=" + password;
    }
}

multipart/form-data

用于表单提交,请求方式为POST,上传文件。

例如:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form method="POST" action="http://localhost:8080/test" enctype="multipart/form-data">
        上传文件:<input type="file" name="file" />
        <input type="submit" value="提交" />
    </form>
</body>

</html>

接收方式:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class DemoController {

    @PostMapping("/test")
    public String demo(MultipartFile file) {
        return file.getOriginalFilename();
    }
}

application/json

指定数据的格式为json格式。

例如:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/axios.js"></script>
</head>

<body>
    <input type="button" value="获取数据POST" onclick="post()">
</body>

<script>
    function post() {
        axios({
            method: "post",
            url: "http://localhost:8080/test",
            data: {
                "username": "艾伦",
                "password": "123abc"
            }
        }).then(result => {
            console.log(result.data)
        })
    }
</script>

</html>

接收方式:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@Data
@NoArgsConstructor
public class UserLogin {
    private String username;
    private String password;
}
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @PostMapping("/test")
    public String demo(@RequestBody UserLogin userLogin) {
        return JSONObject.toJSONString(userLogin);
    }
}

text/xml

xml格式的数据,现在已经不用了,被json格式的数据代替。

text/html

html格式的数据

text/plain

Content-Type默认的值就是text/plain,纯文本,不做任何数据处理。

例如:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form method="POST" action="http://localhost:8080/test" enctype="text/plain">
        用户名:<input type="text" name="username" />
        密码:<input type="password" name="password" />
        <input type="submit" value="登录" />
    </form>
</body>

</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/254991.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Ubuntu如何安装KVM

环境&#xff1a; 联想E14笔记本 Ubuntu20.04 问题描述&#xff1a; Ubuntu如何安装KVM 解决方案&#xff1a; 1.验证CPU是否支持硬件虚拟化 rootst-ThinkPad-E14:~# grep -Eoc (vmx|svm) /proc/cpuinfo 162.检查 VT 是否在 BIOS 中启用 安装 apt install cpu-checker …

论文阅读——Painter

Images Speak in Images: A Generalist Painter for In-Context Visual Learning GitHub - baaivision/Painter: Painter & SegGPT Series: Vision Foundation Models from BAAI 可以做什么&#xff1a; 输入和输出都是图片&#xff0c;并且不同人物输出的图片格式相同&a…

隐私计算介绍

这里只对隐私计算做一些概念性的浅显介绍&#xff0c;作为入门了解即可 目录 隐私计算概述隐私计算概念隐私计算背景国外各个国家和地区纷纷出台了围绕数据使用和保护的公共政策国内近年来也出台了数据安全、隐私和使用相关的政策法规 隐私计算技术发展 隐私计算技术安全多方计…

滴滴出行:驾龄不到一年有什么办法注册网约车?

驾龄不到一年有什么办法注册网约车&#xff1f;怎么解决网约车注册审核问题。我可以为您提供一些对于如何注册滴滴快车的信息。要注册为滴滴快车司机&#xff0c;你需要符合下列规范&#xff1a; 1.年龄在12-60岁左右&#xff0c;有C1或以上驾照&#xff0c;如果驾龄不够三年是…

JVM 垃圾回收详解

前言 什么是垃圾? 垃圾是指运行程序中没有任何引用指向的对象&#xff0c;需要被回收。 内存溢出和内存泄漏 内存溢出&#xff1a;经过垃圾回收之后&#xff0c;内存仍旧无法存储新创建的对象&#xff0c;内存不够溢出。 内存泄漏&#xff1a;又叫“存储泄漏”&#xff0…

【Leetcode】旋转矩阵

题目链接&#xff1a;https://leetcode.cn/problems/rotate-matrix-lcci/description/ 题目描述 给你一幅由 N N 矩阵表示的图像&#xff0c;其中每个像素的大小为 4 字节。请你设计一种算法&#xff0c;将图像旋转 90 度。 不占用额外内存空间能否做到&#xff1f; 示例 …

cefsharp120.1.8(cef120.1.8,Chromium120.0.6099.109)版本升级测试,其他版本H264版本

此版本最新版cef120.1.8,Chromium120.0.6099.109 此更新包括一个高优先级安全更新 This update includes a high priority security update. 说明&#xff1a;本版本暂时不支持264&#xff0c;其他H264版本参考119,116&#xff0c;114&#xff0c;110&#xff0c;109等版本 c…

mysql innodb知识记录

官方文档 官网架构图 innodb 特性 内存 buffer pool 采用优化后的LRU算法&#xff0c; 3/8 of the buffer pool is devoted to the old sublist.The midpoint of the list is the boundary where the tail of the new sublist meets the head of the old sublist.When In…

地牢边缘 DUNGEON LIMBUS中文免安装版

​《地牢边缘》是一款点阵图形式的像素风经典迷宫探索类游戏。玩家需要在游戏中收集多种装备&#xff0c;随机生成的无限地下城。在生死之际遇见的迷之铁匠和管理复活之村的年轻女性。为了找回遗失的记忆&#xff0c;进入更深的地下城。玩家还可以发展村落以及进化武器的多样化…

亚信安慧AntDB数据库助力智慧高速建设

随着新型智慧交通业务的迅速发展&#xff0c;各地高速公路在管控、收费和监测方面的数据管理变得至关重要。智慧公路信息化建设已成为高速公路建设的核心。AntDB数据库在某省级客户中发挥关键作用&#xff0c;帮助构建协同共享、高效的统一智慧管理平台&#xff0c;为高速公路的…

javascript 数组处理的两个利器: `forEach` 和 `map`(上)

&#x1f90d; 前端开发工程师&#xff08;主业&#xff09;、技术博主&#xff08;副业&#xff09;、已过CET6 &#x1f368; 阿珊和她的猫_CSDN个人主页 &#x1f560; 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 &#x1f35a; 蓝桥云课签约作者、已在蓝桥云…

【AI基础设施】智算场景的资源管理系统与未来展望

高性能计算与智算场景 首先澄清两个概念&#xff0c;高性能计算与智算场景&#xff0c;高性能计算主要是面向天气预测、生物计算、材料计算等场景&#xff0c;而最近几年很火的智算主要是面向AI场景的计算&#xff0c;如语音识别、图像识别、自动驾驶等场景&#xff0c;我们可…

算法学习——栈与队列

栈与队列 栈与队列理论基础用栈实现队列思路代码 用队列实现栈思路代码 删除字符串中的所有相邻重复项思路代码 有效的括号思路代码 逆波兰表达式求值思路代码 滑动窗口最大值思路代码未完待续 前 K 个高频元素思路代码拓展 总结栈在系统中的应用括号匹配问题字符串去重问题逆波…

Linux常用网络指令

网络参数设定使用的指令 手动/自动设定与启动/关闭 IP 参数&#xff1a;ifconfig, ifup, ifdown ifconfig ifconfig常用于修改网络配置以及查看网络参数的指令 [rootwww ~]# ifconfig {interface} {up|down} < 观察与启动接口 [rootwww ~]# ifconfig interface {options…

6.s081操作系统Lab4: trap

文章目录 chapter 4概览4.1 CPU trap流程使用寄存器如果cpu想处理1个trap 4.2 用户态引发的trap4.2.1 uservec4.2.2 usertrap4.2.3 usertrapret和userretusertrapretuserret Lab4Backtrace (moderate)Alarm (hard) chapter 4 概览 trap的场景&#xff1a;系统调用&#xff0c…

CUDA C:线程、线程块与线程格

相关阅读 CUDA Chttps://blog.csdn.net/weixin_45791458/category_12530616.html?spm1001.2014.3001.5482 第一百篇博客&#xff0c;写点不一样的。 当核函数在主机端被调用时&#xff0c;它会被转移到设备端执行&#xff0c;此时设备会根据核函数的调用格式产生对应的线程(…

被我们忽略的HttpSession线程安全问题

1. 背景 最近在读《Java concurrency in practice》(Java并发实战)&#xff0c;其中1.4节提到了Java web的线程安全问题时有如下一段话&#xff1a; Servlets and JPSs, as well as servlet filters and objects stored in scoped containers like ServletContext and HttpSe…

第一个程序(STM32F103点灯)

点亮LED 看原理图确定控制LED的引脚看主芯片手册确定如何设置/控制引脚写程序 LED有很多种&#xff0c;像插脚的&#xff0c;贴片的。 它们长得完全不一样&#xff0c;因此我们在原理图中将它抽象出来。 嵌入式系统中&#xff0c;一个LED的电阻非常低&#xff0c;I U/R&…

Java 图片文件上传下载处理

Java 图片文件上传下载处理 下载 做这玩意给我恶心坏了 下载 直接访问上传的路径就可以下载图片了。但是我们往往会包一层接口&#xff0c;以流的方式读取 url 的内容然后返回给前端&#xff0c;这么做的优点是&#xff1a; 内网域名转外网域名&#xff0c;做业务校验并且让用…

Kafka 数据乱序

每个broker队列最多能缓存5个没有应答的请求&#xff1a; 发送数据1&#xff0c;2&#xff0c;3&#xff0c;4&#xff0c;5。发送到3的时候没有应答成功&#xff0c;要重发&#xff0c;结果4先过来了&#xff0c;就导致乱序。 解决&#xff1a;开启幂等性 max.in.flight.req…
最新文章