我正在向服务发出POST
请求并发送文件。在此POST
请求的响应中,服务以byte[]
回复。如何在代码中访问返回的byte[]
?
这就是我使用apache-commons-httpclient
在我的客户端代码中执行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
}