当前位置:网站首页>[internship] solve the problem of too long request parameters
[internship] solve the problem of too long request parameters
2022-07-02 19:57:00 【Wang Liuliu's it daily】
And to solve bug La !!!
Due to the front-end transmission json Too long data leads to too long request parameters , Finally, a null pointer exception appears .
HTTP/1.1 414 Request-URI Too Large
resolvent :
Reference resources :Java Use GZIP Compress and decompress (GZIPOutputStream,GZIPInputStream)
Use gzip Compress / Decompress the string
One 、 Use gzip Compress string (str The string to compress )

①
Reference resources :
Java ByteArrayOutputStream class
// Create a 32 byte ( Default size ) The buffer
ByteArrayOutputStream out = new ByteArrayOutputStream();
Source code :
②
// Create with default buffer size out New byte array output stream object .
GZIPOutputStream gzip = new GZIPOutputStream(out);
Reference resources :
Java_io System of FilterInputStream/FilterOutputStream brief introduction 、 Go into the source code and examples ——07
Source code :
Create a new output stream with the default buffer size .
The new output stream instance is through the call 2 Parameter constructor GZIPOutputStream(out,false) Created .

Create a new output stream using the default buffer size and the specified refresh mode .
Parameters :
out– Output stream syncFlush– If the inheritance of this instance flush() The method call is true, Then use flush mode Deflater Refresh compressor . Before refreshing the output stream SYNC\u Refresh , Otherwise, only refresh the output stream .
③
gzip.write(str.getBytes());
getBytes() yes Java The programming language converts a string into a byte array byte[] Methods .
//getBytes() Source code :
public byte[] getBytes() {
return StringCoding.encode(value, off:0, value.length);
}
write Source code :
take b.length Bytes written to this output stream .
FilterOutputStream Of write Method uses parameters b、0 and b.length Call its three parameters write Method .
Be careful , This method does not use a single parameter b Call the single parameter of its bottom layer flow write Method .
Of its three parameters write Method source code :
From the offset off Start , Will specify... In the byte array len Bytes written to this output stream .
FilterOutputStream Of write Method calls the... Of the last parameter on each byte write Method to output .
Please note that , This method will not call its underlying input stream with the same parameters write Method .FilterOutputStream Subclasses of should provide a more efficient implementation of this method .
Writes the specified bytes to this output stream .
FilterOutputStream Of write Method calls its underlying output stream write Method , The perform . write in (b). Realization OutputStream Abstract writing method of .
Writes the specified bytes to this output stream .
The general convention of writing is to write a byte to the output stream . The bytes to be written are parameters b Eight low positions . Ignore b Of 24 A high position .OutputStream Subclasses of must provide implementations of this method .
④
if (gzip != null) {
try {
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
close Source code :
Write the remaining compressed data to the output stream and close the underlying stream .
private boolean closed = false;
boolean usesDefaultDeflater = false;

Write compressed data to the output stream without closing the underlying stream .
When multiple filters are applied continuously to the same output stream , Please use this method .
Turn off the compressor and discard any unprocessed input . When the compressor is no longer in use , This method should be called , But it will also be finalize() Method auto call . After calling this method ,Deflater The behavior of the object is undefined .
Close this output stream and release any system resources associated with it .
The general convention for closing is to close the output stream . Closed stream cannot perform output operation , Can't reopen .
OutputStream Of close Method doesn't work .
⑤
// code - Compress
return new sun.misc.BASE64Encoder().encode(out.toByteArray());
Reference resources :
- sun.misc.BASE64Encoder Detailed explanation
- base64 Encoding, decoding and sun.misc.BASE64Decoder Usage of
- About Base64 code (Encode) And decoding (Decode) In several ways , There are many roads in here
stay JAVA In order to achieve Base64 It is very easy to encode and decode , because JDK There are already ready-made classes in :
// code :
String src ="BASE64 Coding test ";
sun.misc.BASE64Encoder en = new sun.misc.BASE64Encoder();
String encodeStr = en.encode(src.getBytes());
// decode :
sun.misc.BASE64Decoder dec = newsun.misc.BASE64Decoder();
byte[] data = dec.decodeBuffer(decodeStr);

Two 、 Use gzip decompression (compressedStr Compress string )


①
// initialization , Create a buffer partition of default size
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = null;
GZIPInputStream ginzip = null;
byte[] compressed = null;
String decompressed = null;
②
//decodeBuffer() decode
compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
③
in = new ByteArrayInputStream(compressed);
establish ByteArrayInputStream, For use buf As an array of buffers . Buffer array not copied .pos The initial value of 0,count The initial value of buf The length of .
④
ginzip = new GZIPInputStream(in);
Create a new input stream with the default buffer size .
⑤
while ((offset = ginzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
Read bytes at most . Set the data length bytes in this input stream as a byte array . This method will block , Until some input is available .
This method only performs call reading (b,0,b.length) And return the result . It is important to , It doesn't apply to . Change it to (b);FilterInputStream Some subclasses of depend on the implementation strategy actually used .
Read up to... From this input stream len Bytes of data into a byte array . If len Not zero , This method will block , Until some input is available ; otherwise , Do not read any bytes , And back to 0.
This method is only executed in . Read (b,off,len) And return the result .
Parameters :
b– Buffer for reading data .off– Target array b len Start offset in – The maximum number of bytes read .
From the offset off Start , Will specify... In the byte array len Bytes are written to this byte array output stream .
⑥
// Convert to String type
decompressed = out.toString();
in the light of java, Be careful :
- If the JDK Version less than 1.8, Please use org.apache.commons.codec.binary.Base64;
- If the JDK The version is greater than 1.8, Please use java.util.Base64;
- Use org.apache.commons.codec.binary.Base64 when , To select and project JDK coincidental JAR package , Otherwise, the effect will not be achieved ;
- java.util.Base64 And org.apache.commons.codec.binary.Base64 Packet collision , Cannot exist in a class at the same time ;
- Be careful ,UTF-8 and GBK Chinese format Base64 The coding results are different .
3、 ... and 、 modify service Implementation class -Json Convert to sql In the method conditionJson Compression format for
original :
params.put("conditionJson", URLEncoder.encode(tagInfo.getGenerationLogic(),"UTF-8")); // conditionJson Conditions json Field Same as Generate logical fields :generationLogic
After modification : request url The compressed parameters will not be too long
// Compressed logic json
String compressGenerationLogic = CompressUtil.compress(tagInfo.getGenerationLogic());
params.put("conditionJson", URLEncoder.encode(Base64.encodeBase64String(compressGenerationLogic.getBytes()),"UTF-8"));
URLEncoder.encode(String s, String enc):
s– String to translate .enc– The name of the supported character encoding .
URLEncoder.encode(tagInfo.getGenerationLogic(),"UTF-8")
Use a specific encoding scheme to convert the string to application/x-www-form-urlencoded Format . This method uses the provided encoding scheme to obtain the bytes of unsafe characters .
String encode(String s, String enc)
Use base64 Algorithm encodes binary data , But do not block the output .
notes : We block the behavior of this method from multiple lines (commons-codec-1.4) Change to single line non blocking (commons-codec-1.5).
Use base64 Algorithm encodes binary data , You can choose to block the output into 76 A block of characters .
isChunked– If true, Then this encoder will base64 Output is divided into 76 A block of characters 
Use base64 Algorithm encodes binary data , You can choose to block the output into 76 A block of characters .
Parameters :
binaryData– An array containing binary data to be encoded .
isChunked– If true, Then this encoder will base64 Output is divided into 76 A block of characters
urlSafe– If true, Then this encoder will send - and _ Not the usual + and / character . Be careful : Use URL Do not add padding when encoding the safety alphabet .
Use base64 Algorithm encodes binary data , You can choose to block the output into 76 A block of characters .
Parameters :
binaryData– An array containing binary data to be encoded .
isChunked– If true, Then this encoder will base64 Output is divided into 76 A block of characters
urlSafe– If true, Then this encoder will send - and _ Not the usual + and / character .
Be careful : Use URL Do not add padding when encoding the safety alphabet .
maxResultSize– The maximum result size to accept .
Four 、 modify Controller The interface of
Go to request Add conditions to json— It is decompressed .
String decodeConditionJson = new String(Base64.decodeBase64(request.getConditionJson()));
request.setConditionJson(CompressUtil.uncompress(decodeConditionJson));
take Base64 The string is decoded into octets :
Decode contains N String of characters in hexadecimal letters .
Decode contains N Byte array of characters in hexadecimal letters .
The processing flow of the sender before data transmission is as follows ( The receiving ends are mutually inverse ):
1. First sign the original string , To ensure that the signature is faithful to the original content ;
2. Then compress , In order to simplify the size of the content , Improve the efficiency of subsequent encryption and transmission ;
3. Finally, encrypt , Ensure data security .
边栏推荐
- checklistbox控件用法总结
- GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
- 【Hot100】22. bracket-generating
- 蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么
- How to avoid duplicate data in gaobingfa?
- [daily question] 241 Design priorities for operational expressions
- JASMINER X4 1U deep disassembly reveals the secret behind high efficiency and power saving
- R language uses econcharts package to create microeconomic or macroeconomic maps, and indifference function to visualize indifference curve
- 八年测开经验,面试28K公司后,吐血整理出高频面试题和答案
- AcWing 1127. Sweet butter solution (shortest path SPFA)
猜你喜欢

Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1

Zabbix5 client installation and configuration

KT148A语音芯片ic的用户端自己更换语音的方法,上位机

《MongoDB入门教程》第03篇 MongoDB基本概念
In depth understanding of modern web browsers (I)

KT148A语音芯片ic的开发常见问题以及描述

Conscience summary! Jupyter notebook from Xiaobai to master, the nanny tutorial is coming!

AcWing 1126. Minimum cost solution (shortest path Dijkstra)
![[daily question] 241 Design priorities for operational expressions](/img/27/4ad1a557e308e4383335f51a51adb0.png)
[daily question] 241 Design priorities for operational expressions

rxjs Observable 自定义 Operator 的开发技巧
随机推荐
基于SSM实现网上购物商城系统
定了,就是它!
pxe装机「建议收藏」
sql-labs
For (Auto A: b) and for (Auto & A: b) usage
Istio1.12:安装和快速入门
Kt148a voice chip IC user end self replacement voice method, upper computer
【JS】获取hash模式下URL的搜索参数
Cuckoo filter
Shardingsphere jdbc5.1.2 about select last_ INSERT_ ID () I found that there was still a routing problem
RPD出品:Superpower Squad 保姆级攻略
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
数据库模式笔记 --- 如何在开发中选择合适的数据库+关系型数据库是谁发明的?
自動生成VGG圖像注釋文件
KS004 基于SSH通讯录系统设计与实现
burp 安装 license key not recognized
Detailed tutorial on installing stand-alone redis
Postman下载安装
From 20s to 500ms, I used these three methods
Workplace four quadrant rule: time management four quadrant and workplace communication four quadrant "suggestions collection"