Spring MVC POST中文乱码解决方案

SpringMVC post请求中文乱码问题解决 主要介绍了SpringMVC post请求中文乱码问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 立即下载

POST中文乱码解决方案

以前,我都是自己编写一个filter,来实现编码,代码如下:

 1 public class CharacterEncodingFilter implements Filter {
 2 
 3     private final static Logger log= LoggerFactory.getLogger(CharacterEncodingFilter.class);
 4 
 5     private String characterEncoding; //编码方式配置在web.xml文件中
 6     private boolean enabled;   //是否启用此Filter,配置在web.xml中
 7 
 8     @Override
 9     public void destroy() {
10         characterEncoding = null;
11     }
12 
13     @Override
14     public void doFilter(ServletRequest request, ServletResponse response,
15                          FilterChain chain) throws IOException, ServletException {
16         if (enabled || characterEncoding != null) {
17             request.setCharacterEncoding(characterEncoding);
18             response.setCharacterEncoding(characterEncoding);
19         }
20         chain.doFilter(request, response);
21     }
22 
23     @Override
24     public void init(FilterConfig config) throws ServletException {
25         characterEncoding = config.getInitParameter("characterEncoding");
26         enabled = "true".equalsIgnoreCase(config.getInitParameter("enabled").trim());
27     }
28 }

然后自己在web.xml文件中做如下配置

 1  <filter>
 2         <filter-name>characterEncodingFilter</filter-name>
 3         <filter-class>com.qunar.training.model.filter.CharacterEncodingFilter</filter-class>
 4         <init-param>
 5             <param-name>characterEncoding</param-name>
 6             <param-value>UTF-8</param-value>
 7         </init-param>
 8         <init-param>
 9             <param-name>enabled</param-name>
10             <param-value>true</param-value>
11         </init-param>
12     </filter>
13     <filter-mapping>
14         <filter-name>characterEncodingFilter</filter-name>
15         <url-pattern>/*</url-pattern>
16     </filter-mapping>

其实我们可以不必这么麻烦,重复造轮子。

spring Web MVC框架提供了org.springframework.web.filter.CharacterEncodingFilter用于解决POST方式造成的中文乱码问题,具体配置如下:

 1 <filter>  
 2     <filter-name>CharacterEncodingFilter</filter-name>  
 3     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
 4     <init-param>  
 5         <param-name>encoding</param-name>  
 6         <param-value>utf-8</param-value>  
 7     </init-param>  
 8 </filter>  
 9 <filter-mapping>  
10     <filter-name>CharacterEncodingFilter</filter-name>  
11     <url-pattern>/*</url-pattern>  
12 </filter-mapping>  

  以后我们项目及所有页面的编码均为UTF-8。

国央企创新负责人如何通过平台实现科研成果的快速识别与转化?.docx 科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。 立即下载

相关推荐

手把手教你解决Windows 10网络驱动器无法访问问题(附详细排查步骤)

本文详细指导Windows 10用户解决网络驱动器无法访问的常见问题。通过从现象快速定位到系统排查,提供了从检查网络连通性、修复凭据权限到调整高级设置的完整步骤,特别是针对“找不到网络路径”等错误,帮助用户高效恢复对共享资源的访问。

weixin_29233857的博客 517

轻松解决springmvc中 post 请求中文乱码

配置过滤器,指定所有请求的编码 一、配置spring的编码过滤器,为了防止spring中post方式提交的时候中文乱码。(这种方式是由Springmvc框架提供,不需要自己写) 方法:修改web.xml文件,添加spring的编码过滤器 <!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filt

我是泛滥 3878

AI图像去噪实战:5种最新深度学习模型在超声图像处理中的效果对比(附代码)

本文对比了2022至2024年间五种前沿深度学习模型在超声图像去噪领域的实战效果,包括FDDL-Net、MG-CNFNet、Dual-TranSpeckle、可控去噪模型及Wave-Mamba。文章深入剖析了各模型的核心创新与架构,并提供了可运行的代码片段,旨在帮助医学影像算法工程师根据不同的性能、速度与临床需求,选择最合适的AI图像去噪解决方案。

1a2s3d4f5g的博客 915

SpringMVC解决请求中文乱码问题

SpringMVC处理请求中文乱码问题

qingfengmax的博客 1470

OpenPCDet安装及其3D检测算法实现

OpenPCDet及pointpillar复现

weixin_58207716的博客 1万+

华为OD机试真题 新系统2026-04-01 C语言 实现【空间占用计算】

员工A的磁盘空间经常被耗尽,他需要找到占用空间最大的目录或文件,然后决定如何清理文件释放空间。给定某一目录,请编写程序帮助他统计该目录内一级子目录和文件的占用空间,并返回目标目录一级子项(文件或子目录)中占用空间最大的项。

MISAYAONE的博客 230

Spring MVC学习——解决请求参数中文乱码

解决请求参数中文乱码的问题,GET请求和POST请求

weixin_57445851的博客 1282

实战指南:Qwen3-ASR-1.7B 语音识别

本文介绍了Qwen3-ASR语音识别模型的安装与使用。

举世誉之而不加劝,举世非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。 1804

SpringMVC 彻底解决中文乱码

SpringMVC乱码解决 1、UTF-8国际编码,GBK中文编码。GBK包含GB2312,即如果通过GB2312编码后可通过GBK解码,反之可能不成立。 2、web tomcat:默认是ISO8859-1,不支持中文的。 3、中文乱码问题:其实是因为编码和解码的不一致造成的。首先确保html或jsp页面的编码格式是某个编码,比如utf-8,因为客户端对正文内容的编码是根据页面的编码来设定的...

Charge8的博客 3404

图片风格自动分析模型

对于分类任务,我们可以使用交叉熵损失作为损失函数。同时,我们可以选择随机梯度下降(SGD)或Adam等优化器进行模型优化。# 使用SGD优化器# 或使用Adam优化器在这部分,总结整个项目的过程,包括数据准备、模型构建、训练、优化和评估等。同时,讨论可能的改进方向和未来工作,例如尝试更多的模型结构、优化技巧等。

a871923942的博客 1698

YOLOv11 改进 - SPPF模块 | 发论文神器!LSKA注意力改进SPPF,增强多尺度特征提取能力,高效涨点!!!

本文介绍了Large Separable Kernel Attention (LSKA)模块及其在YOLOv11中的结合应用。LSKA旨在解决Visual Attention Networks (VAN)使用大内核卷积时的计算效率问题,通过将2D深度卷积层的卷积核分解为级联的水平和垂直1-D卷积核,降低了计算复杂度和内存占用,且使VAN更关注物体形状。我们将集成LSKA的SPPF_LSKA模块引入YOLOv11,大量实验表明,改进后的模型在对象识别、检测、分割和稳健性测试上表现出色。

魔改工程师的博客 1279

浅析SpringMVC POST请求中文乱码

笔者近日遇到了SpringMVC框架POST提交中文乱码的问题。对此进行了调查,解决方法在网上有很多,在此进行总结。 在SpringMVC框架下,解决中文提交乱码的问题,首先要保证页面设定的form的字符编码是UTF-8格式。 其次,Servlet服务器实现的Servlet遇到URL和POST提交的数据,它会按照指定的字符集解码。这里所说的"指定的字符集"是在应用服务器的配置文

feather_cool的博客 4434

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

SpringMVC中解决POST和GET请求的乱码问题

解决获取请求参数的乱码问题,可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter,但是必须在web.xml中进行注册

m0_51465608的博客 1927

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Springmvc中文乱码问题

Springmvc的中文乱码问题主要是两部分,请求乱码和响应乱码。而解决的方法就是将字符集设置为 UTF-8 1.请求乱码 Get请求乱码 如果使用的是Tomcat 8及以上的版本,无需设置Tomcat的字符集,因为Tomcat已经默认支持UTF-8编码。但如果使用的Tomcat 8 以下的版本,则需要修改Tomcat的字符集编码,打开Tomcat安装路径,找到Tomcat的配置文件server.xml。 找到一段代码,在后面添加 URIEncoding="UTF-8"保存即可 ...

陈大猫在努力 4888

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Springboot SpringMVC的GET/POST中文参数乱码解决

SpringMVC的GET/POST中文参数乱码解决 POST请求参数中文乱码: 产生原因: spring MVC中默认的编码格式为“ISO-8859-1”,因此造成乱码。 解决: 在web.xml中配置Spring字符过滤器 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web

matafeiyanll的博客 3539

彻底解决Spring MVC 中文乱码 问题

1:表单提交controller获得中文参数后乱码解决方案 注意: jsp页面编码设置为UTF-8 form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果 修改web.xml,增加编码过滤器,如下(注意,需要设置forceEncoding参数值为true) characterEncodingFilter org.spring

passport_daizi的博客 28万+

SpringMVC中如何解决POST中文乱码的问题?GET的又如何处理?

post: ①配置CharacterEncodingFilter 过滤器:只需配置encoding即可,forceEncoding为响应(session)字符乱码. &lt;!--解决post中文乱码问题--&gt; &lt;filter&gt; &lt;filter-name&gt;ChracterEncodingFilter&lt;/filter-name&gt; &lt...

sinat_37127264的博客 530

springMVC解决中文乱码几种方式,以及使用fastJSOn调整时间 的格式

为什么@ResponseBody向前台传JSON输数据会有乱码 首先要知道乱码的原因是在入参之前就应经决定的,就是springMVC 中HandlerMapping会找到对应的handel(即具体去处理请求的方法) ,但是请注意!在真正调用处理请的handle的来处理请求前,或者确切的说是在请求的参数未绑定到入参的方法上的时候,springMVC完成了数据转换,格式化的校验,其中数据的转换spr...

茄子的博客 1074

springMVC处理POST乱码问题

配置解决POST乱码问题的过滤器,将下列代码段放入web.xml中 &lt;!-- 配置解决POST乱码问题的过滤器 --&gt; &lt;filter&gt; &lt;filter-name&gt;encoding&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.we...

keyuzhang的博客 768

springMVC如何解决post乱码问题?springMVC如何解决get乱码问题?

springMVC如何解决post乱码问题?springMVC如何解决get乱码问题?

; 910
上一篇: Java里数字转字符串前面自动补0的实现
下一篇: 关于span不能设置宽度及span自动换行的解决方法
Johnny__Tang
博客等级 码龄18年 9粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值