我在使用Spring Web Service发送图像时遇到问题。如何在Spring中从Web服务发送图像
我已经写控制器如下
@Controller
public class WebService {
@RequestMapping(value = "/image", headers = "Accept=image/jpeg, image/jpg, image/png, image/gif", method = RequestMethod.GET)
public @ResponseBody byte[] getImage() {
try {
InputStream inputStream = this.getClass().getResourceAsStream("myimage.jpg");
BufferedImage bufferedImage = ImageIO.read(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage , "jpg", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@ResponseBody
转换响应转换JSON。
我正在使用RestClient来测试Web服务。
但是当我用http://localhost:8080/my-war-name/rest/image
URL击中时。
Header
Accept=image/jpg
我面临以下错误上RESTClient实现
响应体转换为字符串使用窗口1252编码失败。响应主体未设置!
当我使用的浏览器Chrome和Firefox
头是不添加这样预期的错误(请指导我在此)
HTTP Status 405 - Request method 'GET' not supported type Status report message Request method 'GET' not supported description The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported).
我也面临着以下错误一旦
该请求所标识的资源只能够生成具有不可接受的特征的响应 accordi ng请求“接受”标题()
我跟着 http://krams915.blogspot.com/2011/02/spring-3-rest-web-service-provider-and.html教程。
我的需求是将图像以字节格式发送到Android客户端。
[Spring MVC:How to return image in @ResponseBody?](http://stackoverflow.com/questions/5690228/spring-mvc-how-to-return-image-in-responsebody) – skaffman 2011-12-28 12:51:53