2014-10-08 49 views
0

我有一个控制器从外部目录(例如c:\ images \ userID \ photo.png)提供图像,而且这个控制器很好地完成了它的工作。但是,我的JSP文件中的img标签显示图像图标,而不是此控制器返回的图像。SPRING MVC 3 - 不在JSP中显示图像

这里是我的控制器:

@RequestMapping(value = "/load/{imageId}/", method = RequestMethod.GET) 
public ResponseEntity<byte[]> loadImage(@PathVariable("imageId") Long imageId, HttpServletRequest request) 
{ 
    final org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders(); 
    BufferedImage image; 
    Photo photo = photoManager.getSinglePhoto(imageId); 
    headers.setContentType(MediaType.IMAGE_PNG); 

    try 
    { 
     if (photo == null) 
     { 
      File defaultFile = new File("c:/images/default.png"); 
      image = ImageIO.read(defaultFile); 

      return new ResponseEntity<byte[]>(((DataBufferByte)image.getData().getDataBuffer()).getData(), headers, HttpStatus.CREATED); 
     } 

     File file = new File(photo.getPath()); 
     image = ImageIO.read(file); 
     return new ResponseEntity<byte[]>(((DataBufferByte)image.getData().getDataBuffer()).getData(), headers, HttpStatus.CREATED); 
    } 
    catch (IOException ex) 
    { 
     return new ResponseEntity<byte[]>(null, headers, HttpStatus.NOT_FOUND); 
    } 
} 

我发现这里阅读其他答案,我需要在我的应用程序上下文了MessageConverter,我做到了。

这里是我的应用程序的context.xml

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="messageConverters"> 
     <util:list> 
      <bean id="byteArrayMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> 
     </util:list> 
    </property> 
</bean> 

月食XML编辑器抱怨MethodHandlerAdapter被弃用的一部分。

JSP:

<img src="/mavenspringapp/photo/load/131/" width="128" height="128" alt="laf02.jpg"> 

为什么没有在图像显示得到即使当控制器发送正确的响应(201)。提前致谢。

+0

这春天的版本是U使用 – 2014-10-08 17:30:29

+0

这是3.2.1.RELEASE – 2014-10-08 17:39:40

+0

它显示断开的图像图标即使响应为201 – 2014-10-08 18:25:07

回答

0

问题在于控制器方法。按照我做的方式显然加载图像,但没有正确完成。所以我修改了我的方法,如下图所示:

@RequestMapping(value = "/load/{imageId}/", method = RequestMethod.GET) 
public ResponseEntity<byte[]> loadImage(@PathVariable("imageId") Long imageId, HttpServletRequest request) 
{ 
    final org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders(); 
    Photo photo = photoManager.getSinglePhoto(imageId); 
    headers.setContentType(MediaType.IMAGE_PNG); 

    try 
    { 
     if (photo == null) 
     { 
      File defaultFile = new File("c:/images/default.png"); 
      byte[] content = FileUtils.readFileToByteArray(defaultFile); 

      return new ResponseEntity<byte[]>(content, headers, HttpStatus.OK); 
     } 

     File file = new File(photo.getPath()); 
     byte[] content = FileUtils.readFileToByteArray(file); 
     return new ResponseEntity<byte[]>(content, headers, HttpStatus.OK); 
    } 
    catch (IOException ex) 
    { 
     return new ResponseEntity<byte[]>(null, headers, HttpStatus.NOT_FOUND); 
    } 
} 

现在它工作的很棒!我希望这可以帮助别人。谢谢大家的回复。

+0

不要忘记尽快接受你自己的答案...... – 2014-10-09 05:51:52

0

有点谷歌搜索告诉我,HTTP 201意味着created。 如果图像存在,你为什么要发送一个响应代码告诉客户你刚创建的图像?

我不确定web浏览器如何处理它,但也许尝试将您的响应代码更改为200,因为您不是真的创建任何东西。

+0

我将响应代码修改为HttpStatus.OK(200),但仍无法正常工作。 – 2014-10-09 00:12:13

0

你可以考虑的替代,而不是控制器显示可以直接访问图片你的JSP图像为此,你需要把映射信息在Spring配置XML一样
<mvc:resources mapping="/image/**" location="file:///D:/images/" />,并在你的JSP文件可以直接可以直接调用
<img src="<spring:url value='/images/logo.png'/>" />,并确保你已经在你的JSP中提到的春天标签

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>