我有使用Spring Boot的Java Web Application,我想在JSP页面中显示文件系统中的extern文件夹(E:/ images)中的图像。 我在google看了太多页面,我在stackoverflow上发现了一个帖子,说我应该写一个servlet来获取图片:我错过了什么或者我应该用其他方式做什么,请给我更多细节我有点新春天谢谢你的帮助。
在我的控制器中:
@RequestMapping(value="/images",method = RequestMethod.GET)
public @ResponseBody void affichimage(@RequestParam("id") Integer Iddd,HttpServletResponse response,HttpServletRequest request) throws IOException
{
Annonce annonce=new Annonce();
annonce=annoncedao.findOne(Iddd); // get the right annonce from
//database
File imageFile = new File(annonce.getimage()); // in image I have
//the link to images ex : E:/images/image1.jpeg
response.setContentType("image/jpeg");
BufferedImage image = ImageIO.read(imageFile);
ImageIO.write(image, "image/jpeg", response.getOutputStream());
}
JSP中的:
< img class="imagesaffichage" src="/images?id=${annonce.id}" alt="No image"/>
我还将此添加到我的应用程序中:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("file:///E:/images/");
}
答案 0 :(得分:0)
我找到了解决方案,我不得不改变上面的方法:
@RequestMapping(value="/images",method = RequestMethod.GET)
public @ResponseBody void affichimage(@RequestParam("id") Integer
Iddd,HttpServletResponse response,HttpServletRequest request) throws
IOException,NullPointerException
{
Annonce annonce=new Annonce();
annonce=annoncedao.findOne(Iddd); // get the right annonce from
//database
File imageFile = new File(img);
response.setContentType("image/jpeg");
InputStream in=new FileInputStream(imageFile);
IOUtils.copy(in, response.getOutputStream());
}