来自数据库的图像不会显示在 JSP 中:找不到 HTTP 请求的映射
Image from DB won't display in JSP: No mapping found for HTTP request
我在我的控制器中得到了这个:
@RequestMapping(value = "details", method = RequestMethod.GET)
public ModelAndView details(@Valid @ModelAttribute("MessageForm") final MessageForm form, @RequestParam("publicationid") String publicationid) {
final Publication pub = ps.findById(Integer.valueOf(publicationid));
final User user = us.findById(pub.getUserid());
final ModelAndView mav = new ModelAndView("details");
long myid = pub.getPublicationid();
UploadFile ufa = imageServiceImp.findFirstById(Long.parseLong(publicationid));
System.out.println("mi publication id es " +ufa.getPublicationId());
System.out.println("mi id de upload es " + ufa.getId());
System.out.println("mis bytes son " + ufa.getData().toString());
// these prints work fine
try {
Image img = ImageIO.read(new ByteArrayInputStream(ufa.getData()));
mav.addObject("image1", img);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//other stuff
return mav;
}
所以我在 JSP 中做了以下内容来显示图像:
<img class="mySlides" src="<c:out value="${image1}" />" >
当我进入这个页面尝试我的程序时,我收到了以下消息(当然不显示图像):
Sep 26, 2018 11:27:23 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/null/meinHaus/BufferedImage@4c8d98b4:%20type%20=%205%20ColorModel:%20] in DispatcherServlet with name 'dispatcher'
可能发生了什么?
我正在使用 spring-mvc。
还有 getData() returns byte[] 类型的数组。
这不是您加载图片的方式。
在控制器中编写一个方法 class 如下所示。
@RequestMapping(value = "/users/getProfilePic/{uniqueId}",method = RequestMethod.GET)
public ResponseEntity<byte[]> getProfilePic(@PathVariable long uniqueId) {
try{
//reading image from tomcat folder you can read from the place where you stored the images
String rootPath = System.getProperty("catalina.home");
String profilePicDirectory = rootPath + File.separator + "profilePictures" + File.separator + uniqueId;
String profilePicFile = profilePicDirectory + File.separator + uniqueId +".jpg";
RandomAccessFile f = new RandomAccessFile(profilePicFile, "r");
byte[] b = new byte[(int)f.length()];
f.readFully(b);
f.close();
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
return new ResponseEntity<byte[]>(b, headers, HttpStatus.OK);
}catch(Exception ex){
//ex.printStackTrace();
final HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<byte[]>(null, headers, HttpStatus.NOT_FOUND);
//return null;
}
}
然后在 jsp 页面中调用这个 url 来加载图像。
<img class="mySlides" src="/users/getProfilePic/${imageUniqueIdURL}" />
我在我的控制器中得到了这个:
@RequestMapping(value = "details", method = RequestMethod.GET)
public ModelAndView details(@Valid @ModelAttribute("MessageForm") final MessageForm form, @RequestParam("publicationid") String publicationid) {
final Publication pub = ps.findById(Integer.valueOf(publicationid));
final User user = us.findById(pub.getUserid());
final ModelAndView mav = new ModelAndView("details");
long myid = pub.getPublicationid();
UploadFile ufa = imageServiceImp.findFirstById(Long.parseLong(publicationid));
System.out.println("mi publication id es " +ufa.getPublicationId());
System.out.println("mi id de upload es " + ufa.getId());
System.out.println("mis bytes son " + ufa.getData().toString());
// these prints work fine
try {
Image img = ImageIO.read(new ByteArrayInputStream(ufa.getData()));
mav.addObject("image1", img);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//other stuff
return mav;
}
所以我在 JSP 中做了以下内容来显示图像:
<img class="mySlides" src="<c:out value="${image1}" />" >
当我进入这个页面尝试我的程序时,我收到了以下消息(当然不显示图像):
Sep 26, 2018 11:27:23 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/null/meinHaus/BufferedImage@4c8d98b4:%20type%20=%205%20ColorModel:%20] in DispatcherServlet with name 'dispatcher'
可能发生了什么?
我正在使用 spring-mvc。
还有 getData() returns byte[] 类型的数组。
这不是您加载图片的方式。
在控制器中编写一个方法 class 如下所示。
@RequestMapping(value = "/users/getProfilePic/{uniqueId}",method = RequestMethod.GET)
public ResponseEntity<byte[]> getProfilePic(@PathVariable long uniqueId) {
try{
//reading image from tomcat folder you can read from the place where you stored the images
String rootPath = System.getProperty("catalina.home");
String profilePicDirectory = rootPath + File.separator + "profilePictures" + File.separator + uniqueId;
String profilePicFile = profilePicDirectory + File.separator + uniqueId +".jpg";
RandomAccessFile f = new RandomAccessFile(profilePicFile, "r");
byte[] b = new byte[(int)f.length()];
f.readFully(b);
f.close();
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
return new ResponseEntity<byte[]>(b, headers, HttpStatus.OK);
}catch(Exception ex){
//ex.printStackTrace();
final HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<byte[]>(null, headers, HttpStatus.NOT_FOUND);
//return null;
}
}
然后在 jsp 页面中调用这个 url 来加载图像。
<img class="mySlides" src="/users/getProfilePic/${imageUniqueIdURL}" />