【发布时间】:2016-07-13 12:54:24
【问题描述】:
我有以下用于调用 Jersey REST 服务的客户端。
public class JerseyClient {
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
String response = service.accept(MediaType.TEXT_XML)
.header("Content-Type", "text/xml; charset=UTF-8")
.entity(new File("E:/postx.xml"))
.post(String.class);
System.out.println(response);
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/MyService/rest/xmlServices/doSomething").build();
}
}
目前在服务器端我有以下内容:
@POST
@Path("/doSomething")
@Consumes(MediaType.TEXT_XML)
@Produces(MediaType.TEXT_XML)
public Response consumeXMLtoCreate(ProcessJAXBObject jaxbObject) {
如何更改上述服务器端代码,以便我可以使用 stAX 并将一个特定元素流式传输到磁盘,而不是将所有元素转换为对象到内存中。我的目标是将这个包含二进制编码数据的元素流式传输到磁盘。
我收到的payload是这样的:
<?xml version="1.0" encoding="utf-8"?> <ProcessRequest> <DeliveryDate>2015-12-13</DeliveryDate> <AttachmentBinary>iVBORw0KGgoAAAANSUhEUgAAAFoA</AttachmentBinary> <AttachmentBinary>iVBORw0KGgoAAAANSUhEUgAAAFoA</AttachmentBinary> </ProcessRequest>
听从@vtd-xml-author 的建议
我现在有以下内容:
服务器端:
@POST
@Produces(MediaType.TEXT_XML)
@Path("/doSomething")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response consumeXMLtoCreate(@Context HttpServletRequest a_request,
@PathParam("fileId") long a_fileId,
InputStream a_fileInputStream) throws IOException {
InputStream is;
byte[] bytes = IOUtils.toByteArray(a_fileInputStream);
VTDGen vg = new VTDGen();
vg.setDoc(bytes);
try {
vg.parse(false);
} catch (EncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (EOFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (EntityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
try {
ap.selectXPath("/ProcessRequest/BinaryAttachment/text()");
} catch (XPathParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i=0;
try {
while((i=ap.evalXPath())!=-1){
//i points to text node of
String s = vn.toRawString(i);
System.out.println("HAHAHAHA:" + s);
// you need to decode them
}
} catch (XPathEvalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NavException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
和客户端我有这个:
File file = new File("E:/postx.xml");
FileInputStream fileInStream = null;
fileInStream = new FileInputStream(file);
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
String tempImage = myfile.jpg;
String sContentDisposition = "attachment; filename=\"" + tempImage+"\"";
ClientResponse response = service.type(MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", sContentDisposition)
.post(ClientResponse.class, fileInStream);
我对此解决方案有疑问,首先,如果我最终在堆中得到一个需要解码的 String 对象,我究竟如何避免相同的内存问题?
其次,我可以在删除图像元素后使用 vtd-xml 重构对象或 inputStream,因为我想使用 JAXB 处理它吗?
我相信 XMLModifier 的 remove() 方法应该允许我使用某种 XML 表示减去我现在写入磁盘的元素。
【问题讨论】:
-
@vtd-xml-author STaX 不是必需的。任何有关如何从传入请求中实现 vtd-xml 的信息将不胜感激。
-
如果您可以通过包含一些 XML 来扩展您的问题,也许我可以输入一些代码 sn-p 也许?
-
@vtd-xml-author 谢谢,我刚刚添加了一个 XML sn-p
-
@vtd-xml-author: stax 如何将 xml 转换为“各种临时”对象?
-
字符串对象、字节数组对象和属性对象......下面会发生各种各样的事情,将这些事件发送到您的应用程序逻辑中
标签: java jaxb inputstream stax vtd-xml