首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

软件资讯设计 工具 系统 开发 安全 办公 陶吧 IT教育 Vista频道 | 下载中心酷我音乐盒 腾讯QQ
天极网 > 开发频道>向web服务传递文件

向web服务传递文件

2008-03-31 10:15作者:xqzhao笔记出处:天极网责任编辑:孙蓬阳

  清单 3. 使用 SAAJ

 

以下是引用片段:
 public File resize(String endPoint,File file)
  {
  SOAPConnection connection =
  SOAPConnectionFactory.newInstance().createConnection();
  SOAPMessage message = MessageFactory.newInstance().createMessage();
  SOAPPart part = message.getSOAPPart();
  SOAPEnvelope envelope = part.getEnvelope();
  SOAPBody body = envelope.getBody();
  SOAPBodyElement operation =
  body.addBodyElement(
  envelope.createName("resize",
  "ps",
  "http://psol.com/2004/ws/resize"));
  operation.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
  DataHandler dh = new DataHandler(new FileDataSource(file));
  AttachmentPart attachment = message.createAttachmentPart(dh);
  SOAPElement source = operation.addChildElement("source",""),
  percent = operation.addChildElement("percent","");
  message.addAttachmentPart(attachment);
  source.addAttribute(envelope.createName("href"),
  "cid:" + attachment.getContentId());
  width.addTextNode("20");
  SOAPMessage result = connection.call(message,endPoint);
  part = result.getSOAPPart();
  envelope = part.getEnvelope();
  body = envelope.getBody();
  if(!body.hasFault())
  {
  Iterator iterator = result.getAttachments();
  if(iterator.hasNext())
  {
  dh = ((AttachmentPart)iterator.next()).getDataHandler();
  String fname = dh.getName();
  if(null != fname)
  return new File(fname);
  }
  }
  return null;
  }
  注意,清单 3 清楚地表明附件是在 XML 消息的 外部!为了提高效率这是必需的。

  谈到效率,看一看清单 4,这是 清单 3 更常见的 JAX-RPC 版本(也短得多)。JAX-RPC 预处理程序生成一个存根程序,极大简化了编码。您把 DataHandler 对象作为参数传递,JAX-RPC 自动生成附件。

  清单 4. 更有效的 JAX-RPC

  

以下是引用片段:
public File resize(File file)
  throws ServiceException, RemoteException
  {
  AttachmentService service = new AttachmentServiceLocator();
  AttachmentTip port = service.getAttachmentTip(); // get stub
  DataHandler dh = new DataHandler(new FileDataSource(file));
  DataHandler result = port.resize(dh,20);
  return new File(result.getName());
  }
  结束语

  选择是一件好事,而 SOAP 为您处理二进制数据提供了选择:您可以在 XML有效负载中使用 base 64编码――对于小的数据集这种方法很好,也可以向请求中附加大的没有编码的二进制文件。

原文链接:http://www.cnblogs.com/xqzhao/archive/2008/03/31/1130610.html

共3页。 上一页123
进入 最权威的Windows 7论坛 查看网友讨论

软件频道最新更新

热点推荐

IT嘉年华

编辑推荐

软件下载

热门
推荐

网友关注

软件
资料
游戏

装机推荐

文章排行

本周
本月
最新更新
天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络