如何在不重新加载应用程序的情况下在 Spring 控制器中上传后获取图像?
How to get images after upload in Spring controller without reload application?
在我的 Spring 控制器中,我编写了 return 资源文件夹中 JSP 页面上的图像的方法。但是,如果我更改资源(或)添加新资源,我的 InputStream 为空。当我重新加载应用程序时,它会再次运行。
What can I do to get images without reloading application?
我在控制器中的方法:
@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\Картинка[=14=].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" + "\"");
}
这是从 Spring controller.Adding 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);
}
如果您想在浏览器中呈现图像,请添加一个 header "Content-Disposition" 作为内联 else 附件,这会将文件下载到您的本地计算机。
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 + "\"");
}
这对我有用,因为同一 resource.My 资源的多个请求位于 (../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" + "\"");
}
在我的 Spring 控制器中,我编写了 return 资源文件夹中 JSP 页面上的图像的方法。但是,如果我更改资源(或)添加新资源,我的 InputStream 为空。当我重新加载应用程序时,它会再次运行。
What can I do to get images without reloading application?
我在控制器中的方法:
@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\Картинка[=14=].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" + "\"");
}
这是从 Spring controller.Adding 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);
}
如果您想在浏览器中呈现图像,请添加一个 header "Content-Disposition" 作为内联 else 附件,这会将文件下载到您的本地计算机。
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 + "\"");
}
这对我有用,因为同一 resource.My 资源的多个请求位于 (../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" + "\"");
}