2014-07-01 75 views
2

有没有什么方法可以使用弹簧控制器映射图像文件?在我的春天应用程序,我想存储在目录中的src/main /资源图像(我使用maven),并用这样的方式访问它们:通过弹簧控制器映射图像文件

@RequestMapping(value="image/{theString}") 
public ModelAndView image(@PathVariable String theString) { 
    return new ModelAndView('what should be placed here?'); 
} 

字符串theString它的图像名(不含延期)。通过这种方法,我应该能够访问我的图片是这样的:

/webapp/controller_mapping/image/image_name 

任何人都可以指向一个方向来做到这一点?

+2

您可以返回'HttpEntity '包含图像,需要头。图像可以通过classloader的'getResourceAsStream'方法获得http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String) –

+0

@ KonstantinV.Salikhov这应该是一个答案,所以你可以得到一些互联网布朗尼点。 – CodeChimp

回答

0

您可以返回HttpEntity<byte[]>Construct新实例提供图像字节数组和必要的标题,如内容长度和MIME类型,然后从您的方法返回它。图像字节可以使用类加载器getResourceAsStreammethod获得。

0

这适用于我。它可以使用一些清理,但它的工作。 ServiceException只是一个简单的基本例外。

祝你好运!

package com.dhargis.example; 
 

 
     import java.io.File; 
 
     import java.io.IOException; 
 

 
     import javax.servlet.ServletOutputStream; 
 
     import javax.servlet.http.HttpServletRequest; 
 
     import javax.servlet.http.HttpServletResponse; 
 

 
     import org.apache.commons.io.FileUtils; 
 
     import org.apache.log4j.Logger; 
 
     import org.springframework.stereotype.Controller; 
 
     import org.springframework.web.bind.annotation.PathVariable; 
 
     import org.springframework.web.bind.annotation.RequestMapping; 
 
     import org.springframework.web.bind.annotation.RequestMethod; 
 

 

 

 

 
     @Controller 
 
     @RequestMapping("/image") 
 
     public class ImageController { 
 

 
    \t private static final Logger log = Logger.getLogger(ImageController.class); 
 
    \t 
 
    \t private String filestore = "C:\\Users\\dhargis"; 
 
    \t 
 
    \t //produces = "application/octet-stream" 
 
    \t @RequestMapping(value = "/{filename:.+}", method = RequestMethod.GET) 
 
    \t public void get(@PathVariable String filename, 
 
    \t \t \t HttpServletRequest request, 
 
    \t \t \t HttpServletResponse response) { 
 

 
    \t \t log.info("Getting file " + filename); 
 
    \t \t try { 
 
    \t \t \t byte[] content = null; 
 
    \t \t \t File store = new File(filestore); 
 
    \t \t \t if(store.exists()){ 
 
    \t \t \t \t File file = new File(store.getPath()+File.separator+filename); 
 
    \t \t \t \t if(file.exists()){ 
 
    \t \t \t \t \t content = FileUtils.readFileToByteArray(file); 
 
    \t \t \t \t } else { 
 
    \t \t \t \t \t throw new ServiceException("File does not exist"); 
 
    \t \t \t \t } 
 
    \t \t \t } else { 
 
    \t \t \t \t throw new ServiceException("Report store is required"); 
 
    \t \t \t } 
 
    \t \t \t 
 
    \t \t \t ServletOutputStream out = response.getOutputStream(); 
 
    \t \t \t out.write(content); 
 
    \t   out.flush(); 
 
    \t   out.close(); 
 
    \t \t } catch (ServiceException e) { 
 
    \t \t \t log.error("Error on get", e); 
 
    \t \t } catch (IOException e) { 
 
    \t \t \t log.error("Error on get", e); 
 
    \t \t } 
 
    \t \t 
 
    \t } 
 

 
    }

 

 
<!-- begin snippet: js hide: false -->