webservice (axis) interface to upload file attachments and compressed using zlib solution

webservice transfer files, I usually webservice framework that is used Axis and CXF, these two frameworks can be used to transfer files DataHandler, which pass the contents of the file, no file name, type, so these have their own interface fields added; Another embodiment is a file converted into a byte array, and then Base64 encoded byte array into a string field type into the interface for transmission, the party that receives the first decoded and then save the file.

The latter is currently used, whether the former or the latter, file transfers both ways can not be too big, the former is not clear how much concrete can pass a dozen or twenty trillion after Katherine can still pass, but the relatively slow transmission, but the receiving end to extend the timeout.

About compression, currently only in a test environment I tried, but do not know because zlib compression is not unfriendly to pdf, pdf compression file compression rate is relatively low, I measured the files A square pass over the compressed, and before decompression is not much difference after decompression, compression rate of 6%, that is only compressed to 10M 9.4M, compression pressure is not no difference, because the on-line time is tight, so there is no other compression method to acute test, this time recorded on the first recording under the zlib compression / decompression.

The review process under way also read and write files, input and output streams on the brother said quite wide https://blog.csdn.net/msk_zn/article/details/73385467.

A. DataHandler for file transfer

1.Axis framework

2.CXF framework

NOTE: The DataHandler way these two frameworks to upload files temporarily to not finishing, because of a temporary lack of demand in this area, so put up later on, have the time to make up :).

II. Coded transmission

1. The transmitting end

Do is the transmitting end to read the file input stream, the input stream is read and written to the output stream, and then outputs the byte stream into an array, as an object of type String String with Base64 encoding, this character string object into the field corresponding to the interface transmits the ok;

 1 InputStream in = new FileInputStream(filePath);
 2 ByteArrayOutputStream out = new ByteArrayOutputStream();
 3 byte[] buffer = new byte[1024];
 4 int n = 0;
 5 while ((n = in.read(buffer)) != -1) {
 6 out.write(buffer, 0, n);
 7 }
 8 byte[] data = out.toByteArray();
 9 in.close();
10 
11 BASE64Encoder encoder = new BASE64Encoder();//Many Base64 encoding, select their own, with the same kind of attention codec can be. 
12 is String encodedText = encoder.encode (textByte);

2. The receiving end

 Receiving end and sending end opposite done, first received first decoding Base64 string object into a byte array, and the byte array input stream converted to an array of bytes into a file;

. 1 String Content = "pass over the content transmission side" ;
 2 File DestFile = new new File ( "want to store a file path name +" );
 . 3  
. 4 Base64Decoder Decoder = new new Base64Decoder (); // Base64 decoding method more, select their own , with the same attention to the codec. 
. 5  byte [] bytes = decoder.decodeBuffer (Content);
 . 6 the InputStream IS = new new A ByteArrayInputStream (bytes);
 . 7  byte [] = BUFF new new  byte [1024 ];
 . 8  int len = 0 ;
 . 9 a FileOutputStream fos = new new a FileOutputStream (DestFile) ;
10 bos = new BufferedOutputStream(fos);
11 while((len=is.read(buff))!=-1){
12        fos.write(buff, 0, len);
13 }
14 is.close();
15 fos.close(); 

III. Compressed transmission

The compression method is recorded Zlib, as used in the project so recorded. Zlib online articles about the introduction of a lot, but mostly stereotyped, are the source of my brother https://www.iteye.com/blog/snowolf-465433, the article not only records the Zlib compression and decompression, also record the decompressed zip compression, compression format under Linux Gzip, compression and decompression of BZip2, written in great detail, great.

1.Zlib compression

Zlib compression in two ways, one is compressed byte array, the other one is compressed to the output stream.

Compression Solutions 2.Zlib

Decompression Similarly, byte arrays can be decompressed, the input may be decompressed.

 1 byte[] bytes = decoder.decodeBuffer(content);
 2 File file=new File(filePath+"/"+fileName);
 3 FileOutputStream fos = new java.io.FileOutputStream(file);
 4 BufferedOutputStream bos = new BufferedOutputStream(fos);
 5 
 6 //输入流形式进行解压缩
 7 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
 8 InflaterInputStream iis = new InflaterInputStream(byteArrayInputStream);
 9 int i = 1024;
10 Integer sum = 0;
11 byte[] buf = new byte[i];
12 while ((i = iis.read(buf, 0, i)) > 0) {
13      bos.write(buf, 0, i);
14 15 }
16 iis.close;
17 bos.close;
 1 byte[] bytes = decoder.decodeBuffer(content);
 2 File file=new File(filePath+"/"+fileName);
 3 FileOutputStream fos = new java.io.FileOutputStream(file);
 4 BufferedOutputStream bos = new BufferedOutputStream(fos);
 5 
 6 //字节数组形式解压缩
 7 Inflater decompresser = new Inflater();
 8 decompresser.reset();
 9 decompresser.setInput(bytes);
10 byte[] buf = new byte[1024];
11 while (!decompresser.finished()) {
12     int i = decompresser.inflate(buf);
13     bos.write(buf, 0, i);
14 }
15 bos.close;
16 fos.close;

3.zip compression

Compression Solutions 4.zip

Note: decompressed zip compression to temporarily useless, but when the test also used a little time after finishing :).

 

Finally, after the compression Base64 encoded transmission may refer to the above two methods, the first content transmission decoding and decompression. Seems to leave a lot of the pit, but the basic functions can be achieved, so when the contents of the completion, and then modify the title of the article!

related articles:

java- file stream resources are properly closed    https://www.cnblogs.com/tfper/p/9833722.html (file stream closed in the above is not strict, close the file on the correct flow may refer to this article)

Java compression technology https://www.iteye.com/blog/snowolf-465433

java- string or byte array, etc., into an input stream https://blog.csdn.net/y41992910/article/details/80176552

Java input and output streams Detailed https://blog.csdn.net/msk_zn/article/details/73385467

 

Guess you like

Origin www.cnblogs.com/thePeaceOftheLord/p/12670537.html