url参数中有+、空格、=、\ 、%、&、#等特殊符号的问题解决

前端url带参数传参出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,后台无法获得正确的参数值

错误代码例如:

function uploadFile() {
        var superior = "\\user\\liuyinlong\\"; //参数含有 \
        window.location.href = "/toWebuploader?superior="+superior;
    }

错误提示

前端报错400    后端报错如下:

 Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
 
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
	at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:479) ~[tomcat-embed-core-8.5.35.jar:8.5.35]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:684) [tomcat-embed-core-8.5.35.jar:8.5.35]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.35.jar:8.5.35]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806) [tomcat-embed-core-8.5.35.jar:8.5.35]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) [tomcat-embed-core-8.5.35.jar:8.5.35]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.35.jar:8.5.35]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_121]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_121]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.35.jar:8.5.35]
	at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]

正确代码如下:

function uploadFile() {
        var superior = "\\user\\liuyinlong\\"; //参数含有 \
        superior = superior.replace('\\','%2F'); //把 \ 进行转义
        window.location.href = "/toWebuploader?superior="+superior;
    }

解决办法

将这些字符转化成服务器可以识别的字符,对应关系如下:
URL字符转义

用其它字符替代吧,或用全角的。

+    URL 中+号表示空格                                 %2B  
空格 URL中的空格可以用+号或者编码           %20
/   分隔目录和子目录                                     %2F    
?    分隔实际的URL和参数                             %3F    
%    指定特殊字符                                          %25    
#    表示书签                                                  %23    
&    URL 中指定的参数间的分隔符                  %26    

=    URL 中指定参数的值                                %3D

更多字符编码参考w3c学校https://www.w3school.com.cn/tags/html_ref_urlencode.html

版权声明:
作者:黯然gg
链接:https://3zi.cn/archives/121
来源:阿龙爱吃肉
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>