我正在使用Spring 3.0.1.RELEASE进行我的webapp(我无法升级它),我正试图在网页上从数据库中渲染一些图像。
我遵循简单的Spring配置:
弹簧-application.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:annotation-driven />
<context:annotation-config />
<context:spring-configured />
<context:component-scan base-package="com.me" />
<bean id="hibernateSessionFactory" class="com.me.dbaccess.HibernateSessionFactory">
<constructor-arg ref="sessionFactory"/>
</bean>
</beans>
弹簧mvc.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<bean id="tilesViewResolver" class="com.me.util.TilesExposingBeansViewResolver">
<property name="viewClass" value="com.me.util.TilesExposingBeansView"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/config/tiles-defs.xml</value>
</list>
</property>
</bean>
</beans>
我有以下控制器:
@Controller
public class PhotoController {
@RequestMapping("/carPhoto.html")
@ResponseBody
public byte[] getCarPhoto(
@RequestParam(UrlParameters.PHOTO_ID) Integer photoId,
@RequestParam(UrlParameters.PHOTO_TYPE) String photoType) {
//return image's bytes array from db by photo Id and Type;
}
}
最后,我有简单的jsp-page:
<%@page contentType="text/html; charset=utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<img id="photoImage" src="<c:url value="/carPhoto.html?photoType=1&photoId=22556793"/>" />
如果我打开这个页面 - 我可以毫无问题地看到这张图片。
但是如果我复制图像“src”属性并将其粘贴到浏览器的地址栏(Firefox 19.0.2)中 - 那么浏览器会让我保存carPhoto.html,而不是仅仅渲染图像。 我应该进行一些额外的设置吗?
答案 0 :(得分:6)
问题是,您必须指定mime类型(如果您的图像较大,则还需要指定其长度)。
另一个解决方案()是返回Spring ResponseEntity
或HttpEntity
(如果你总是返回http状态代码200,则HttpEntity就足够了,如果你想要的话,例如需要ResponseEntity(HttpEntity的一个子类))返回其他http状态代码,如未找到的。)
@Controller
public class PhotoController {
@RequestMapping("/carPhoto.html")
@ResponseBody
public HttpEntity<byte[]> getCarPhoto(
@RequestParam(UrlParameters.PHOTO_ID) Integer photoId,
@RequestParam(UrlParameters.PHOTO_TYPE) String photoType) {
byte[] image = image's bytes array from db by photo Id and Type;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG); //or what ever type it is
headers.setContentLength(image.length);
return new HttpEntity<byte[]>(image, headers);
}
}
答案 1 :(得分:2)
有几种方法。最简单的方法是使用带有byte []的@ResponseBody作为返回类型,设置Content-Type等,使用HttpServletResponse将字节写入输出流。
更优雅的解决方案(IMO)将返回ModelAndView,然后将View设置为自定义视图,该视图设置正确的标头(Content-Type等)并将字节写出到HttpServletResponse的输出流。