web中gzip,deflate的压缩与解压

来源:互联网 发布:java自动按键 编辑:程序博客网 时间:2024/06/03 03:10

一,对发送请求进行gzip,deflate压缩

1:gzip的情况

Java代码  收藏代码
  1. Sring url = "http://localhost/save";  
  2. PostMethod post = new PostMethod(url);  
  3. //请求体内容  
  4. String body = "sample";  
  5. //用gzip方式压缩请求体并赋给request  
  6. ByteArrayInputStream bis = new ByteArrayInputStream(body.getBytes());  
  7. ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  8. GZIPOutputStream gos = new GZIPOutputStream(bos);  
  9. for (int c = bis.read(); c != -1; c = bis.read()) {  
  10.     gos.write(c);  
  11. }  
  12. gos.close();  
  13. InputStreamRequestEntity entity = new InputStreamRequestEntity(new ByteArrayInputStream(bos.toByteArray()), "text/html");  
  14. post.setRequestEntity(entity);  
  15. post.addRequestHeader("Content-Encoding""gzip");  

 

2:deflate的情况

Java代码  收藏代码
  1. Sring url = "http://localhost/save";  
  2. PostMethod post = new PostMethod(url);  
  3. //请求体内容  
  4. String body = "sample";  
  5. //用deflate方式压缩请求体并赋给request  
  6. ByteArrayInputStream bis = new ByteArrayInputStream(body.getBytes());  
  7. ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  8. DeflaterOutputStream dos = new DeflaterOutputStream(bos);  
  9. for (int c = bis.read(); c != -1; c = bis.read()) {  
  10.     dos.write(c);  
  11. }  
  12. dos.close();  
  13. InputStreamRequestEntity entity = new InputStreamRequestEntity(new ByteArrayInputStream(bos.toByteArray()), "text/html");  
  14. post.setRequestEntity(entity);  
  15. post.addRequestHeader("Content-Encoding""deflate");  

 

二,在服务器端使用过滤器对压缩过的请求进行解压

新建一个filter继承UserAgentFilter.java,截取req,进行包装,接着继续执行别的filters

Java代码  收藏代码
  1. @Override  
  2. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,  
  3.     ServletException {  
  4.     HttpServletRequest request = (HttpServletRequest) req;  
  5.     String ce = request.getHeader("Content-Encoding"); // gzip|deflate|inflate  
  6.     if (ce != null) {  
  7.         if (ce.indexOf("deflate") >= 0 || ce.indexOf("inflate") >= 0) {  
  8.         // uncompress using inflate  
  9.         request = new InflateRequestWrapper(request);  
  10.         } else if (ce.indexOf("gzip") >= 0) {  
  11.         // uncompress using gzip  
  12.         request = new GZipRequestWrapper(request);  
  13.         }  
  14.     }  
  15.     ......  
  16.     super.doFilter(request, res, chain);  
  17.     ......  
  18. }  

 

下面是InflateRequestWrapper的内容,GZipRequestWrapper与之类似

Java代码  收藏代码
  1. public class InflateRequestWrapper extends HttpServletRequestWrapper {  
  2.     private BufferedServletInputStreamWrapper _stream;  
  3.   
  4.     public InflateRequestWrapper(HttpServletRequest request) throws IOException {  
  5.         super(request);  
  6.         _stream = new BufferedServletInputStreamWrapper(new InflaterInputStream(request.getInputStream()), request.getContentLength());  
  7.     }  
  8.   
  9.     @Override  
  10.     public BufferedServletInputStreamWrapper getInputStream() {  
  11.         return _stream;  
  12.     }  
  13.   
  14.     @Override  
  15.     public int getContentLength() {  
  16.         return _stream.getBytes().length;  
  17.     }  
  18. }  

 

下面是BufferedServletInputStreamWrapper的内容

Java代码  收藏代码
  1. public class BufferedServletInputStreamWrapper extends ServletInputStream {  
  2.   
  3.     private static final int DEFAULT_READ_BUFFER_SIZE = 1024;  
  4.     private final byte[] EMPTY_ARRAY = new byte[0];  
  5.     private ByteArrayInputStream _is;  
  6.     private byte[] _bytes;  
  7.   
  8.     /** 
  9.      * takes in the actual input stream that we should be buffering 
  10.      */  
  11.     public BufferedServletInputStreamWrapper(InflaterInputStream stream, int length) throws IOException {  
  12.         _bytes = (length == 0) ? EMPTY_ARRAY : toBytes(stream, length);  
  13.         _is = new ByteArrayInputStream(_bytes);  
  14.     }  
  15.   
  16.     @Override  
  17.     public int read() throws IOException {  
  18.         return _is.read();  
  19.     }  
  20.   
  21.     @Override  
  22.     public int read(byte[] buf,  
  23.                     int off,  
  24.                     int len) {  
  25.         return _is.read(buf, off, len);  
  26.     }  
  27.   
  28.     @Override  
  29.     public int read(byte[] buf) throws IOException {  
  30.         return _is.read(buf);  
  31.     }  
  32.   
  33.     @Override  
  34.     public int available() {  
  35.         return _is.available();  
  36.     }  
  37.   
  38.     /** 
  39.      * resets the wrapper's stream so that it can be re-read from the stream. if we're 
  40.      * using this somewhere were we expect it to be done again in the chain this should 
  41.      * be called after we're through so we can reset the data. 
  42.      */  
  43.   
  44.     public void resetWrapper() {  
  45.         _is = new ByteArrayInputStream(_bytes);  
  46.     }  
  47.   
  48.     public byte[] getBytes() {  
  49.         return _bytes;  
  50.     }  
  51.   
  52.     private byte[] toBytes(InputStream is, int bufferSize) throws IOException {  
  53.         bufferSize = (bufferSize <= 0) ? DEFAULT_READ_BUFFER_SIZE : bufferSize;  
  54.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  55.         byte[] buffer = new byte[bufferSize];  
  56.         int read = is.read(buffer);  
  57.   
  58.         while (-1 != read) {  
  59.             bos.write(buffer, 0, read);  
  60.             read = is.read(buffer);  
  61.         }  
  62.   
  63.         return bos.toByteArray();  
  64.     }  
  65. }  

 

三,服务器对response进行压缩可参考jetty相关源码

 

四,java客户端对压缩过的返回值进行解压

1,gzip的情况

Java代码  收藏代码
  1. GZIPInputStream gzip = new GZIPInputStream(post.getResponseBodyAsStream());  
  2. StringBuffer out = new StringBuffer();  
  3. byte[] b = new byte[4096];  
  4. for (int n; (n = gzip.read(b)) != -1;) {  
  5.     out.append(new String(b, 0, n));  
  6. }  
  7. return out.toString();  

 

2,deflate的情况

Java代码  收藏代码
  1. InflaterInputStream iis = new InflaterInputStream(post.getResponseBodyAsStream());  
  2. contentLength = (contentLength <= 0) ? 1024 : contentLength;  
  3. ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  4. byte[] buffer = new byte[contentLength];  
  5. int read = iis.read(buffer);  
  6.   
  7. while (-1 != read) {  
  8.     bos.write(buffer, 0, read);  
  9.     read = iis.read(buffer);  
  10. }  
  11.   
  12. byte[] _bytes = (contentLength == 0) ? EMPTY_ARRAY : bos.toByteArray();  
  13. ByteArrayInputStream _is = new ByteArrayInputStream(_bytes);  
  14.   
  15. StringBuffer out = new StringBuffer();  
  16. byte[] b = new byte[4096];  
  17. for (int n; (n = _is.read(b)) != -1;) {  
  18.     out.append(new String(b, 0, n));  
  19. }  
  20. return out.toString();  
 原地址:http://crazysky.iteye.com/blog/775333
原创粉丝点击