Reputation: 159
I'm having next issue - each image sends request:
<img src="getImage/9.jpg"/>
Spring MVC Controller:
@Secured( "ROLE_ADMIN")
@RequestMapping(value = "/getImage/{img_name:.+}")
public byte[] getImage(@PathVariable String img_name) {
byte[] data;
try {
String realpath = "D:\\Project\\images\\" + img_name;
Path path = Paths.get(realpath);
data = Files.readAllBytes(path)
}catch (IOException e){
data = null;
}
return data;
}
On the side of browser I'm receiving next error:
406 Not Acceptable
What could be the reason of such issue? What data should I send from the server side to make this <img src="getImage/9.jpg"?>
work properly.
Upvotes: 0
Views: 1878
Reputation: 3205
Kindly add this code in the spring-context.xml file which registers a ByteArrayHttpMessageConverter
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>image/jpeg</value> <value>image/png</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven
and the class should be
@Secured( "ROLE_ADMIN")
@RequestMapping(value = "/getImage/{img_name:.+}")
public ResponseEntity<byte[]> getImage(@PathVariable String img_name)
throws InternalServerError {
byte[] data;
try {
String realpath = "D:\\Project\\images\\" + img_name;
Path path = Paths.get(realpath);
data = Files.readAllBytes(path)
}catch (IOException e){
data = null;
}
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
return new ResponseEntity<byte[]>(data , headers, HttpStatus.OK);
}
Upvotes: 4