Java 如何接受字节[]作为对POST的响应
我正在向一家服务机构发出Java 如何接受字节[]作为对POST的响应,java,web-services,grails,bytearray,apache-commons-httpclient,Java,Web Services,Grails,Bytearray,Apache Commons Httpclient,我正在向一家服务机构发出POST请求并发送一份文件。从该POST请求响应时,服务将以字节[]进行响应。如何访问代码中返回的字节[] 这就是我如何使用ApacheCommonsHttpClient在客户端代码中执行POST public void sendFile(InputStream is) throws Exception { HttpClient client = new HttpClient(); PostMethod method = new PostMethod("h
POST
请求并发送一份文件。从该POST
请求响应时,服务将以字节[]
进行响应。如何访问代码中返回的字节[]
这就是我如何使用ApacheCommonsHttpClient在客户端代码中执行POST
public void sendFile(InputStream is) throws Exception {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://service:8080/myservice/request/testimage");
ByteArrayPartSource bpa = new ByteArrayPartSource("s.jpg",IOUtils.toByteArray(is));
Part[] parts = new Part[] {
new FilePart("myFile", bpa)
};
method.setRequestEntity(
new MultipartRequestEntity(parts, method.getParams())
);
client.executeMethod(method);
}
该服务也是由我编写的,我可以控制它。它是用grails编写的,如下所示:
//Domain
class Image {
byte[] myFile
static constraints = {
myFile maxSize: 1024 * 1024 * 2
}
}
//Controller
def testimage() {
def img = new Image(params)
byte[] fileBytes = service.performChangesToFile(img.myFile)
render fileBytes
}
用于将响应数据发送到客户端
您的客户端接收Base64字符串并将其解码到字节数组。
方法。responseBody
应返回字节[]