私のSpring Controllerでは、リソースフォルダからJSPページにイメージを返すメソッドを書きました。しかし、リソースを変更した場合(または)新しいInputStreamがnullの場合アプリをリロードすると、再び動作します。アプリケーションをリロードせずにSpringコントローラでアップロードした後にイメージを取得する方法は?
私は、アプリケーションをリロードせずに画像を取得するために何ができますか?
コントローラの私の方法:JSPページで
@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);
}
:
<img src="/image/${advert.authorUsername}/${advert.title}/">
EDITED:
@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" + "\"");
}
EDITED 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" + "\"");
}
_ @ WMAccessVisibility(value = AccessSpecifier.APP_ONLY)_と_ @ ApiOperation(value = "")_はどういう意味ですか? – Dmitry
sry、これらは自分の注釈です。更新されたコードで、このサービスには影響しません。 –
このコードは、ターゲットフォルダから読み込まれた画像と同じように動作します。 – Dmitry