我正在向一个服务发出一个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
}发布于 2014-06-30 20:45:03
使用Base64 encoding将响应数据发送到您的客户端。
您的客户端接收Base64字符串并将其解码为字节数组。
https://stackoverflow.com/questions/24490354
复制相似问题