在我的Spring Controller中,我编写了从resources文件夹返回JSP页面上的图像的方法。但是,如果我更改资源(或)添加新的我的InputStream是null。当我重新加载应用程序时,它再次工作。
如何在不重新加载应用程序的情况下获取图像?
我在控制器中的方法:
@ResponseBody
@RequestMapping(value = "/image/{authorUsername}/{title}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] testphoto(HttpServletRequest request,
@PathVariable("authorUsername") String authorUsername,
@PathVariable("title") String title) throws IOException {
InputStream in = servletContext.getResourceAsStream("/resources/uploadImages/zeus192/" + title + "/0.jpg");
return IOUtils.toByteArray(in);
}
JSP页面上的:
<img src="/image/${advert.authorUsername}/${advert.title}/">
编辑:
@ResponseBody
@RequestMapping(value = "/image/{authorUsername}/{title}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public void testphoto(HttpServletRequest request, HttpServletResponse response,
@PathVariable("authorUsername") String authorUsername,
@PathVariable("title") String title) throws IOException {
InputStream instream = servletContext.getResourceAsStream("/resources/uploadImages/zeus192/" + title + "/0.jpg");
byte[] bytes = IOUtils.toByteArray(instream);
int contentLength = IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream());
response.setContentLength(contentLength);
response.setHeader("Content-Disposition", "inline;filename=\"" + "0.jpg" + "\"");
}
已编辑2:
@ResponseBody
@RequestMapping(value = "/image/{authorUsername}/{title}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public void testphoto(HttpServletRequest request, HttpServletResponse response,
@PathVariable("authorUsername") String authorUsername,
@PathVariable("title") String title) throws IOException {
String contextPath = request.getSession().getServletContext().getRealPath("");
String directory = contextPath.substring(0,contextPath.length() - 18) + "/target/YaPokupay/src/main/webapp/resources/uploadImages/" + authorUsername + "/" + title + "/0.jpg";
File file = new File(directory);
String filePath = file.getPath(); // D:\Works\YaPokupay\target\YaPokupay\src\main\webapp\resources\uploadImages\zeus192\Картинка\0.jpg
InputStream instream = this.getClass().getClassLoader().getResourceAsStream(filePath);
byte[] bytes = IOUtils.toByteArray(instream);
int contentLength = IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream());
response.setContentLength(contentLength);
response.setHeader("Content-Disposition", "inline;filename=\"" + "0.jpg" + "\"");
}
答案 0 :(得分:1)
以下是从Spring controller下载文件的代码。添加httpServletResponse作为参数,并将文件流设置为响应输出流。
@ResponseBody
@RequestMapping(value = "/image/{authorUsername}/{title}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public void testphoto(HttpServletRequest request,HttpServletResponse response,
@PathVariable("authorUsername") String authorUsername,
@PathVariable("title") String title) throws IOException {
InputStream in = servletContext.getResourceAsStream("/resources/uploadImages/zeus192/" + title + "/0.jpg");
Byte[] bytes = IOUtils.toByteArray(in);
int contentLength = IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream());
httpServletResponse.setContentLength(contentLength);
}
如果您想在浏览器中呈现图片,请添加标题&#34; Content-Disposition&#34;作为内联其他附件,将文件下载到本地计算机。
if(inline){ // if you want to attachment to be inline
response.setHeader("Content-Disposition", "inline;filename=\"" + fileName + "\"");
}else{
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
}
答案 1 :(得分:1)
这适用于我对同一资源的多个请求。我的资源位于(../ ProjectName / src / main / webapp / resources / images / logos)
@Autowired
private ServletContext servletContext;
@RequestMapping(value = "/sampleJavaOperation", method = RequestMethod.GET)
public void sampleJavaOperation(HttpServletRequest request, HttpServletResponse response) throws IOException {
InputStream instream = servletContext.getResourceAsStream("/resources/images/logos/wavemaker_62x62.png");
byte[] bytes = IOUtils.toByteArray(instream);
int contentLength = IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream());
response.setContentLength(contentLength);
response.setHeader("Content-Disposition", "inline;filename=\"" + "0.png" + "\"");
}