当前位置:网站首页>[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 .
边栏推荐
- Génération automatique de fichiers d'annotation d'images vgg
- 八年测开经验,面试28K公司后,吐血整理出高频面试题和答案
- c语言里怎么设立优先级,细说C语言优先级
- GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
- Overview of browser caching mechanism
- pytorch 模型保存的完整例子+pytorch 模型保存只保存可訓練參數嗎?是(+解决方案)
- ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题
- Why do I have a passion for process?
- at编译环境搭建-win
- HDL design peripheral tools to reduce errors and help you take off!
猜你喜欢
随机推荐
【实习】解决请求参数过长问题
Postman下载安装
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
AcWing 383. Sightseeing problem solution (shortest circuit)
Génération automatique de fichiers d'annotation d'images vgg
Zabbix5 client installation and configuration
pxe装机「建议收藏」
KT148A语音芯片ic的开发常见问题以及描述
JASMINER X4 1U deep disassembly reveals the secret behind high efficiency and power saving
中缀表达式转换为后缀表达式(C语言代码+详解)
JS how to get integer
Development skills of rxjs observable custom operator
疫情封控65天,我的居家办公心得分享 | 社区征文
KT148A语音芯片ic的软件参考代码C语言,一线串口
Implementation of online shopping mall system based on SSM
浏览器缓存机制概述
AcWing 1131. Saving Private Ryan (the shortest way)
AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
测试人员如何做不漏测?这7点就够了
CheckListBox control usage summary








