我在使用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