针对项目中的大报文,想把报文进行压缩传输,降低网络消耗,所以想到了报文压缩后进行传输,从网络找了压缩的代码写了一下,成功了,现在贴出来,方便以后使用
1、先把测试类贴出来
1 package com.isoftstone.iics.bizsupport.sms.restful; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.nio.charset.Charset; 8 import java.util.zip.GZIPOutputStream; 9 10 import org.apache.commons.httpclient.HttpClient; 11 import org.apache.commons.httpclient.SimpleHttpConnectionManager; 12 import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; 13 import org.apache.commons.httpclient.methods.PostMethod; 14 import org.apache.commons.io.FileUtils; 15 import org.apache.commons.io.IOUtils; 16 17 public class GzipHttp { 18 19 20 public static void main(String[] args) { 21 GzipHttp http=new GzipHttp(); 22 try { 23 String url="http://localhost:8081/***************/test"; 24 String readFileToString = FileUtils.readFileToString(new File("f:/123.xml"), "utf-8"); 25 http.sendHttp(url, readFileToString); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 30 } 31 32 33 34 35 36 37 public String sendHttp(String url,String message){ 38 HttpClient client=new HttpClient(new SimpleHttpConnectionManager()); 39 PostMethod post=new PostMethod(url); 40 post.setContentChunked(true);//内容是否应该分块传输 41 post.setRequestHeader("Content-Encoding", "gzip"); 42 post.setRequestHeader("Transfer-Encoding", "chunked"); 43 ByteArrayOutputStream bao=new ByteArrayOutputStream(); 44 String result=null; 45 try { 46 bao.write(message.getBytes(Charset.forName("UTF-8"))); 47 ByteArrayOutputStream base=new ByteArrayOutputStream(); 48 GZIPOutputStream gops=new GZIPOutputStream(base); 49 bao.writeTo(gops); 50 gops.finish(); 51 post.setRequestEntity(new ByteArrayRequestEntity(base.toByteArray(),"text/xml;charset=utf-8")); 52 client.executeMethod(post); 53 InputStream responseBodyAsStream = post.getResponseBodyAsStream(); 54 result = IOUtils.toString(responseBodyAsStream); 55 System.out.println(result); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 60 return result; 61 } 62 63 }
2、这个是Controller
1 package com.isoftstone.iics.bizsupport.sms.web.controller; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletInputStream; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.apache.commons.io.IOUtils; 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.stereotype.Controller; 13 import org.springframework.web.bind.annotation.RequestMapping; 14 import org.springframework.web.bind.annotation.RequestMethod; 15 16 @Controller 17 public class GzipController { 18 19 private final static Logger logger=LoggerFactory.getLogger(GzipController.class); 20 21 @RequestMapping(value="/test",method=RequestMethod.POST) 22 public void gZipTest(HttpServletRequest req,HttpServletResponse rep){ 23 try { 24 ServletInputStream inputStream = req.getInputStream(); 25 String string = IOUtils.toString(inputStream); 26 logger.error("request Message:{}",string); 27 } catch (IOException e) { 28 logger.error(" appear Exception"); 29 e.printStackTrace(); 30 } 31 } 32 33 }
3、这个是过滤器
1 package com.isoftstone.iics.bizsupport.sms.web.filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.http.HttpServletRequest; 12 13 public class RequestFilter implements Filter { 14 15 @Override 16 public void destroy() { 17 18 } 19 20 @Override 21 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, 22 ServletException { 23 chain.doFilter(new GzipRequestWrapper((HttpServletRequest)req), res); 24 25 } 26 27 @Override 28 public void init(FilterConfig arg0) throws ServletException { 29 30 } 31 32 }
1 package com.isoftstone.iics.bizsupport.sms.web.filter; 2 3 import java.io.IOException; 4 import java.util.zip.GZIPInputStream; 5 6 import javax.servlet.ServletInputStream; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletRequestWrapper; 9 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 13 public class GzipRequestWrapper extends HttpServletRequestWrapper { 14 15 private final static Logger logger=LoggerFactory.getLogger(GzipRequestWrapper.class); 16 17 private HttpServletRequest request; 18 19 public GzipRequestWrapper(HttpServletRequest request){ 20 super(request); 21 this.request = request; 22 } 23 24 @Override 25 public ServletInputStream getInputStream() throws IOException{ 26 ServletInputStream inputStream = request.getInputStream(); 27 String content = request.getHeader("Content-Encoding"); 28 if(content!=null && content.indexOf("gzip")!=-1){ 29 try { 30 final GZIPInputStream gzip=new GZIPInputStream(inputStream); 31 32 ServletInputStream newStrem = new ServletInputStream() { 33 34 @Override 35 public int read() throws IOException { 36 return gzip.read(); 37 } 38 }; 39 return newStrem; 40 } catch (IOException e) { 41 logger.error("decompression abnormal ,please check!!!"); 42 e.printStackTrace(); 43 } 44 45 } 46 return inputStream; 47 } 48 49 50 51 }
4、在web.xml中加上过滤器地址即可
1 <filter> 2 <filter-name>gzipFilter</filter-name> 3 <filter-class>com.isoftstone.iics.bizsupport.sms.web.filter.RequestFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>gzipFilter</filter-name> 7 <url-pattern>/test</url-pattern> 8 </filter-mapping>