如何获取 XML 文本作为输入流
How to get XML text as an Inputstream
我有以下用于调用 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-作者的建议
我现在有以下内容:
服务器端:
@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 表示减去我现在写入磁盘的元素。
我假设您知道如何在您的代码中与 HTTP 协议交互..所以您知道如何将字节从输入流读取到字节缓冲区...下面的代码将接管这一点.. .
public void readBinaryAttachment(HTTPInputStream input) throws VTDException, IOException{
// first read xml bytes into XMLBytes
....
VTDGen vg = new VTDGen();
vg.setDoc(XMLBytes);
vg.parse(false);//
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/ProcessRequest/BinaryAttachment/text()");
int i=0;
while((i=ap.evalXPath())!=-1){
//i points to text node of
String s = vn.toRawString(i);
// you need to decode them
}
}
我有以下用于调用 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-作者的建议
我现在有以下内容:
服务器端:
@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 表示减去我现在写入磁盘的元素。
我假设您知道如何在您的代码中与 HTTP 协议交互..所以您知道如何将字节从输入流读取到字节缓冲区...下面的代码将接管这一点.. .
public void readBinaryAttachment(HTTPInputStream input) throws VTDException, IOException{
// first read xml bytes into XMLBytes
....
VTDGen vg = new VTDGen();
vg.setDoc(XMLBytes);
vg.parse(false);//
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/ProcessRequest/BinaryAttachment/text()");
int i=0;
while((i=ap.evalXPath())!=-1){
//i points to text node of
String s = vn.toRawString(i);
// you need to decode them
}
}