Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过Spring控制器映射图像文件_Java_Spring_Spring Mvc - Fatal编程技术网

Java 通过Spring控制器映射图像文件

Java 通过Spring控制器映射图像文件,java,spring,spring-mvc,Java,Spring,Spring Mvc,有没有办法使用spring控制器映射图像文件?在我的spring应用程序中,我希望将图像存储在src/main/resources目录中(我使用的是maven),并使用如下方法访问它们: @RequestMapping(value="image/{theString}") public ModelAndView image(@PathVariable String theString) { return new ModelAndView('what should be placed he

有没有办法使用spring控制器映射图像文件?在我的spring应用程序中,我希望将图像存储在src/main/resources目录中(我使用的是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

任何人都可以指出这样做的方向?

您可以返回
HttpEntity
。新实例提供图像字节数组和必要的头,如内容长度和mime类型,然后从方法返回它。可以使用classloader
getResourceAsStream
获取图像字节

这对我很有用。它可能需要一些清理,但它的工作。ServiceException只是一个简单的基本异常

祝你好运

package com.dhargis.example;
导入java.io.File;
导入java.io.IOException;
导入javax.servlet.ServletOutputStream;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入org.apache.commons.io.FileUtils;
导入org.apache.log4j.Logger;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
@控制器
@请求映射(“/image”)
公共类图像控制器{
私有静态最终记录器log=Logger.getLogger(ImageController.class);
私有字符串filestore=“C:\\Users\\dhargis”;
//products=“应用程序/八位字节流”
@RequestMapping(value=“/{filename:.+}”,method=RequestMethod.GET)
public void get(@PathVariable字符串文件名,
HttpServletRequest请求,
HttpServletResponse(响应){
log.info(“获取文件”+文件名);
试一试{
字节[]内容=空;
文件存储=新文件(文件存储);
if(store.exists()){
File File=新文件(store.getPath()+File.separator+文件名);
if(file.exists()){
content=FileUtils.readFileToByteArray(文件);
}否则{
抛出新的ServiceException(“文件不存在”);
}
}否则{
抛出新的ServiceException(“需要报表存储”);
}
ServletOutputStream out=response.getOutputStream();
写出(内容);
out.flush();
out.close();
}捕获(服务异常e){
log.error(“获取时出错”,e);
}捕获(IOE异常){
log.error(“获取时出错”,e);
}
}
}


您可以返回包含图像和所需标题的
HttpEntity
。图像可以通过classloader@KonstantinV.Salikhov的
getResourceAsStream
方法获得,这应该是一个答案,这样您就可以获得一些互联网布朗尼点数。