Java 如何在restfulweb服务中发送XML文件端点url

Java 如何在restfulweb服务中发送XML文件端点url,java,tomcat,request,restful-architecture,endpoint,Java,Tomcat,Request,Restful Architecture,Endpoint,我需要将请求XML文件作为多部分表单数据发送到{url}。这在Restfulweb服务中是如何实现的。在我使用之前 RequestDispatcher rd = request.getRequestDispatcher("/file/message.jsp"); rd.forward(request, response); 但这不是在特定的{url}中发送的,如何发送 如果您能够做到这一点(取决于您的上下文),那么使用JAX-RS客户机就是一个解决方案 Apache CXF的示例: Input

我需要将请求XML文件作为多部分表单数据发送到{url}。这在Restfulweb服务中是如何实现的。在我使用之前

RequestDispatcher rd = request.getRequestDispatcher("/file/message.jsp");
rd.forward(request, response);
但这不是在特定的{url}中发送的,如何发送

如果您能够做到这一点(取决于您的上下文),那么使用JAX-RS客户机就是一个解决方案

Apache CXF的示例:

InputStream inputStream = getClass().getResourceAsStream("/file/message.jsp");
WebClient client = WebClient.create("http://myURL");
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=message.jsp");
Attachment att = new Attachment("root", inputStream, cd);
client.post(new MultipartBody(att));
如果您能够做到这一点(取决于您的上下文),那么使用JAX-RS客户机就是一个解决方案

Apache CXF的示例:

InputStream inputStream = getClass().getResourceAsStream("/file/message.jsp");
WebClient client = WebClient.create("http://myURL");
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=message.jsp");
Attachment att = new Attachment("root", inputStream, cd);
client.post(new MultipartBody(att));
如果您能够做到这一点(取决于您的上下文),那么使用JAX-RS客户机就是一个解决方案

Apache CXF的示例:

InputStream inputStream = getClass().getResourceAsStream("/file/message.jsp");
WebClient client = WebClient.create("http://myURL");
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=message.jsp");
Attachment att = new Attachment("root", inputStream, cd);
client.post(new MultipartBody(att));
如果您能够做到这一点(取决于您的上下文),那么使用JAX-RS客户机就是一个解决方案

Apache CXF的示例:

InputStream inputStream = getClass().getResourceAsStream("/file/message.jsp");
WebClient client = WebClient.create("http://myURL");
client.type("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=message.jsp");
Attachment att = new Attachment("root", inputStream, cd);
client.post(new MultipartBody(att));

我不认为web服务是您的最佳选择
您可以尝试本机流,然后您可以编写标题和正文,正如您所喜欢的这里是示例代码来解释我的观点

Socket socket=new new socket(InetAddress.getByName("stackoverflow.com"), 80);
// just the host and the port
Writer out = new OutputStreamWriter(socket.getOutputStream(),"UTF-8");
            out.write("POST http://" + HOST + ":" + port+ "/ HTTP/1.1\r\n");//here u can insert your end point or the page accepts the xml msg
    out.write("Host: " + HOST + "/ \r\n");
    out.write("Content-type: application/xml,text/xml\r\n");// Accept
    out.write("Content-length: " + req.length() + "\r\n");
    out.write("Accept:application/xml,text/xml\r\n");
    out.write("\r\n");
    // req the Request Body or the xml file to be sent
    out.write(req);//
    out.flush();`

我不认为web服务是您的最佳选择
您可以尝试本机流,然后您可以编写标题和正文,正如您所喜欢的这里是示例代码来解释我的观点

Socket socket=new new socket(InetAddress.getByName("stackoverflow.com"), 80);
// just the host and the port
Writer out = new OutputStreamWriter(socket.getOutputStream(),"UTF-8");
            out.write("POST http://" + HOST + ":" + port+ "/ HTTP/1.1\r\n");//here u can insert your end point or the page accepts the xml msg
    out.write("Host: " + HOST + "/ \r\n");
    out.write("Content-type: application/xml,text/xml\r\n");// Accept
    out.write("Content-length: " + req.length() + "\r\n");
    out.write("Accept:application/xml,text/xml\r\n");
    out.write("\r\n");
    // req the Request Body or the xml file to be sent
    out.write(req);//
    out.flush();`

我不认为web服务是您的最佳选择
您可以尝试本机流,然后您可以编写标题和正文,正如您所喜欢的这里是示例代码来解释我的观点

Socket socket=new new socket(InetAddress.getByName("stackoverflow.com"), 80);
// just the host and the port
Writer out = new OutputStreamWriter(socket.getOutputStream(),"UTF-8");
            out.write("POST http://" + HOST + ":" + port+ "/ HTTP/1.1\r\n");//here u can insert your end point or the page accepts the xml msg
    out.write("Host: " + HOST + "/ \r\n");
    out.write("Content-type: application/xml,text/xml\r\n");// Accept
    out.write("Content-length: " + req.length() + "\r\n");
    out.write("Accept:application/xml,text/xml\r\n");
    out.write("\r\n");
    // req the Request Body or the xml file to be sent
    out.write(req);//
    out.flush();`

我不认为web服务是您的最佳选择
您可以尝试本机流,然后您可以编写标题和正文,正如您所喜欢的这里是示例代码来解释我的观点

Socket socket=new new socket(InetAddress.getByName("stackoverflow.com"), 80);
// just the host and the port
Writer out = new OutputStreamWriter(socket.getOutputStream(),"UTF-8");
            out.write("POST http://" + HOST + ":" + port+ "/ HTTP/1.1\r\n");//here u can insert your end point or the page accepts the xml msg
    out.write("Host: " + HOST + "/ \r\n");
    out.write("Content-type: application/xml,text/xml\r\n");// Accept
    out.write("Content-length: " + req.length() + "\r\n");
    out.write("Accept:application/xml,text/xml\r\n");
    out.write("\r\n");
    // req the Request Body or the xml file to be sent
    out.write(req);//
    out.flush();`

您可以使用
Jersey Rest客户端
post请求
的形式发送XML消息

try {
    Client client = Client.create();

    WebResource webResource = client.resource(http://<your URI>);

    // POST method
    ClientResponse response = webResource.accept("multipart/form-data").type("multipart/form-data").post(ClientResponse.class, "<your XML message>");

    // check response status code
    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }

    // display response
    String output = response.getEntity(String.class);
    System.out.println("Output from Server .... ");
    System.out.println(output + "\n");
} catch (Exception e) {
     e.printStackTrace();
}
试试看{
Client=Client.create();
WebResource=client.resource(http://);
//POST方法
ClientResponse response=webResource.accept(“多部分/表单数据”).type(“多部分/表单数据”).post(ClientResponse.class“”);
//检查响应状态代码
if(response.getStatus()!=200){
抛出新的RuntimeException(“失败:HTTP错误代码:+response.getStatus());
}
//显示响应
字符串输出=response.getEntity(String.class);
System.out.println(“从服务器输出…”);
System.out.println(输出+“\n”);
}捕获(例外e){
e、 printStackTrace();
}
对于Jersey Client,您可以在此处找到文档:


您可以使用
Jersey Rest客户端
post请求
的形式发送XML消息

try {
    Client client = Client.create();

    WebResource webResource = client.resource(http://<your URI>);

    // POST method
    ClientResponse response = webResource.accept("multipart/form-data").type("multipart/form-data").post(ClientResponse.class, "<your XML message>");

    // check response status code
    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }

    // display response
    String output = response.getEntity(String.class);
    System.out.println("Output from Server .... ");
    System.out.println(output + "\n");
} catch (Exception e) {
     e.printStackTrace();
}
试试看{
Client=Client.create();
WebResource=client.resource(http://);
//POST方法
ClientResponse response=webResource.accept(“多部分/表单数据”).type(“多部分/表单数据”).post(ClientResponse.class“”);
//检查响应状态代码
if(response.getStatus()!=200){
抛出新的RuntimeException(“失败:HTTP错误代码:+response.getStatus());
}
//显示响应
字符串输出=response.getEntity(String.class);
System.out.println(“从服务器输出…”);
System.out.println(输出+“\n”);
}捕获(例外e){
e、 printStackTrace();
}
对于Jersey Client,您可以在此处找到文档:


您可以使用
Jersey Rest客户端
post请求
的形式发送XML消息

try {
    Client client = Client.create();

    WebResource webResource = client.resource(http://<your URI>);

    // POST method
    ClientResponse response = webResource.accept("multipart/form-data").type("multipart/form-data").post(ClientResponse.class, "<your XML message>");

    // check response status code
    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }

    // display response
    String output = response.getEntity(String.class);
    System.out.println("Output from Server .... ");
    System.out.println(output + "\n");
} catch (Exception e) {
     e.printStackTrace();
}
试试看{
Client=Client.create();
WebResource=client.resource(http://);
//POST方法
ClientResponse response=webResource.accept(“多部分/表单数据”).type(“多部分/表单数据”).post(ClientResponse.class“”);
//检查响应状态代码
if(response.getStatus()!=200){
抛出新的RuntimeException(“失败:HTTP错误代码:+response.getStatus());
}
//显示响应
字符串输出=response.getEntity(String.class);
System.out.println(“从服务器输出…”);
System.out.println(输出+“\n”);
}捕获(例外e){
e、 printStackTrace();
}
对于Jersey Client,您可以在此处找到文档:


您可以使用
Jersey Rest客户端
post请求
的形式发送XML消息

try {
    Client client = Client.create();

    WebResource webResource = client.resource(http://<your URI>);

    // POST method
    ClientResponse response = webResource.accept("multipart/form-data").type("multipart/form-data").post(ClientResponse.class, "<your XML message>");

    // check response status code
    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }

    // display response
    String output = response.getEntity(String.class);
    System.out.println("Output from Server .... ");
    System.out.println(output + "\n");
} catch (Exception e) {
     e.printStackTrace();
}
试试看{
Client=Client.create();
WebResource=client.resource(http://);
//POST方法
ClientResponse response=webResource.accept(“多部分/表单数据”).type(“多部分/表单数据”).post(ClientResponse.class“”);
//检查响应状态代码
if(response.getStatus()!=200){
抛出新的RuntimeException(“失败:HTTP错误代码:+response.getStatus());
}
//显示响应
字符串输出=response.getEntity(String.class);
System.out.println(“从服务器输出…”);
System.out.println(输出+“\n”);
}捕获(例外e){
e、 printStackTrace();
}
对于Jersey Client,您可以在此处找到文档: