cxf如何实现GZIP压缩数据?

晨光 2012-04-24 04:42:40
如题,求代码,在线等
...全文
435 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
晨光 2012-08-07
  • 打赏
  • 举报
回复
压缩前后的大小不一样噻。
ihic11 2012-07-19
  • 打赏
  • 举报
回复
我想问下,你怎么测的,怎么确定压缩是不是成功的?谢谢!
晨光 2012-04-24
  • 打赏
  • 举报
回复
感谢楼上提供的思路,本人已测通过。
snzke 2012-04-24
  • 打赏
  • 举报
回复

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class StreamInterceptor extends AbstractPhaseInterceptor<Message> {

public StreamInterceptor() {
super(Phase.PRE_STREAM);
System.out.println("StreamInterceptor Initializing...");
addBefore(SoapPreProtocolOutInterceptor.class.getName());
}

public void handleMessage(Message message) {
boolean isOutbound = false;
isOutbound = message == message.getExchange().getOutMessage()
|| message == message.getExchange().getOutFaultMessage();

if (isOutbound) {
OutputStream os = message.getContent(OutputStream.class);
CachedStream cs = new CachedStream();
message.setContent(OutputStream.class, cs);

message.getInterceptorChain().doIntercept(message);

try {
cs.flush();
CachedOutputStream csnew = (CachedOutputStream) message
.getContent(OutputStream.class);

GZIPOutputStream zipOutput = new GZIPOutputStream(os);
CachedOutputStream.copyStream(csnew.getInputStream(), zipOutput, 1024);
cs.close();
zipOutput.close();
os.flush();

System.out.println("------------- os = " + os);
message.setContent(OutputStream.class, os);
} catch (IOException ioe) {
ioe.printStackTrace();
}
} else {
try {
InputStream is = message.getContent(InputStream.class);
GZIPInputStream zipInput = new GZIPInputStream(is);
message.setContent(InputStream.class, zipInput);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

public void handleFault(Message message) {
}


private class CachedStream extends CachedOutputStream {
public CachedStream() {
super();
}

protected void doFlush() throws IOException {
currentStream.flush();
}

protected void doClose() throws IOException {
}

protected void onWrite() throws IOException {
}
}
}


并在配置文件中添加如下代码:
[code=Xml]
<cxf:bus>
<!--cxf:inInterceptors>
<ref bean="GZIPStream"/>
</cxf:inInterceptors>
<cxf:inFaultInterceptors>
<ref bean="GZIPStream"/>
</cxf:inFaultInterceptors-->
<!--cxf:outInterceptors>
<ref bean="GZIPStream" />
</cxf:outInterceptors-->
<!--cxf:outFaultInterceptors>
<ref bean="GZIPStream"/>
</cxf:outFaultInterceptors-->
</cxf:bus>
[/code]

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧