这2天用axis2编写webservice,需求里面要传送照片,并且系统里的照片大小不一样,有的是几M,有的是几十K。
解决方法记录如下:
1.从数据库里取出Blob类型的字段值转换为byte数组
private byte[] compressionTx(java.sql.Blob bl) throws Exception{
InputStream inStream = bl.getBinaryStream();
byte[] data = null;
long nLen = bl.length();
int nSize = (int) nLen;
//System.out.println("img data size is :" + nSize);
data = new byte[nSize];
inStream.read(data);
inStream.close();
data = ChangeImgSize(data, 333, 500);
return data;
}
2.对byte[]数组进行同比例压缩
经测试照片缩小6倍图像不会失真,3M左右的照片进行压缩后是20k以内
private byte[] ChangeImgSize(byte[] data, int nw, int nh)throws Exception{
byte[] newdata = null;
BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data));
int w = bis.getWidth();
int h = bis.getHeight();
double sx = (double) nw / w;
double sy = (double) nh / h;
AffineTransform transform = new AffineTransform();
transform.setToScale(sx, sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
//原始颜色
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis, bid);
//转换成byte字节
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bid, "jpeg", baos);
newdata = baos.toByteArray();
return newdata;
}
3.对byte数组进行Base64编码,并转换为字符串
String uploadBuffer = new String(Base64.encodeBase64(data)); //进行Base64编码
把这个字符串放到xml里即可进行传输,在客户端进行解码,即可获取到照片
4.开始在做axis2的测试客户端时,用的是axis1,调用后出现了find nonamespace的错误,查资料后也没有解决。
后来用Myeclipse里的JAX-WS框架轻松搞定,如图:
客户端调用代码如下:
public static void main(String[] args) {
ServiceImpl client = new ServiceImpl();
ServiceImplPortType service = client.getServiceImplHttpSoap11Endpoint();
try{
//机构信息
String s = service.getJGXX("889900");
System.out.println("s===>"+s);
}catch(Exception e){
e.printStackTrace();
}
System.exit(0);
}
5.参考的页面
http://blog.csdn.net/xiangqiao123/article/details/12653617
http://www.cnblogs.com/xd502djj/p/3370876.html
http://www.blogjava.net/zhangqingping/articles/JAVA.html