我在mysql数据库中有一个BLOB类型的图像,我想在jsp上显示图像。我正在使用Hibernate和Spring MVC。这是我的Model类:
@Repository
@Entity
@Table(name = "foto")
public class Image {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_id_users", nullable = false)
private Users user;
@Id
@Column(name = "id_foto")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id_foto;
@Column(name = "tipo")
private String tipo;
@Column(name = "size")
private String size;
@Column(name = "nome")
private String nome;
@Column(name = "image")
private byte[] image;
//Getters and Setters
这是我的控制者:
@Controller
@SessionAttributes("UserSession")
public class LoginController {
@Autowired
private UsersService usersService;
@RequestMapping(value = "loginUsers", method = RequestMethod.POST)
public ModelAndView loginUsers(HttpServletRequest request,@RequestParam("username") String username,
@RequestParam("password") String password) {
Users user = usersService.loginUsers(username, password);
if( user == null ) {
ModelAndView MV = new ModelAndView("login");
MV.addObject("erroreLogin", "username e/o password errati");
return MV;
} else if ( user.getAmministratore() == false ){
request.getSession().setAttribute("UserSession",user);
ModelAndView mav = new ModelAndView("homeUtente");
mav.addObject("galleria", usersService.getAllFoto());
return mav;
} else {
request.getSession().setAttribute("UserSession",user);
ModelAndView mav = new ModelAndView("utenti");
mav.addObject("lista", usersService.getAllUtenti());
return mav;
}
}
@RequestMapping(value = "logout", method = RequestMethod.GET)
public ModelAndView logout(HttpServletRequest request) {
request.getSession().invalidate(); //invalido i dati presenti in sessione
return new ModelAndView("login");
}
}
在我的jsp中,我使用它来显示图像列表中的图像,因为每个用户都有一个要显示的图库:
<img alt="Kangoo_image" src="data:image/jpeg;base64,${galleria.image}" />
当我试图在我的jsp中显示它时,它显示的是二进制文件,如[B @ 59e73b47。如何在jsp中显示图像?
答案 0 :(得分:2)
要在JSP上显示图像而不存储到文件系统并链接到它,您必须对字节数组进行Base64编码。通过以下行轻松完成
byte[] encodeBase64 = Base64.encodeBase64(usersService.getAllFoto());
String base64Encoded = new String(encodeBase64, "UTF-8");
mav.addObject("galleria", usersService.getAllFoto());
IOUtils和Base64都来自org.apache.commonsEndFragment
答案 1 :(得分:0)
这附加因为galleria.image返回一个byte []类型,并且在jsp的结果html上出现byte [] .toString()值。完全是[B @ 59e73b47。
你应该使用类似的东西:
<img alt="Kangoo_image" src="data:image/jpeg;base64,new String(${galleria.image})" />
或
<img alt="Kangoo_image" src="/getImage/${galleria.id_foto}" />
并且在 getImage 控制器中就像这样
@Autowired
private HttpServletRequest request;
@RequestMapping("/getImage/*")
public void getImage(ModelMap model, HttpServletResponse response)
throws IOException {
requestUri = requestUri.substring((request.getContextPath() + "/getImage/")
.length());
Image image = DAO.findById(requestUri);
String requestUri = request.getRequestURI();
InputStream is = new ByteArrayInputStream(image.getImage());
response.setContentType("image/jpeg");
String name = image.getName() + ".jpeg";
String attachment = "inline; filename=" + name;
response.setHeader("content-Disposition", attachment);
response.setContentLength((int) baos.toByteArray().length);
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
is.close();
}
答案 2 :(得分:0)
@Controller
@SessionAttributes("UserSession")
public class LoginController {
@Autowired
private UsersService usersService;
@RequestMapping(value = "loginUsers", method = RequestMethod.POST)
public ModelAndView loginUsers(HttpServletRequest request,@RequestParam("username") String username,
@RequestParam("password") String password) {
Users user = usersService.loginUsers(username, password);
if( user == null ) {
ModelAndView MV = new ModelAndView("login");
MV.addObject("erroreLogin", "username e/o password errati");
return MV;
} else if ( user.getAmministratore() == false ){
request.getSession().setAttribute("UserSession",user);
ModelAndView mav = new ModelAndView("homeUtente");
byte[] encodeBase64 = Base64.encode(usersService.getAllFoto());
String base64Encoded = new String(encodeBase64, "UTF-8");
mav.addObject("userImage", base64Encoded )
return mav;
} else {
request.getSession().setAttribute("UserSession",user);
ModelAndView mav = new ModelAndView("utenti");
mav.addObject("lista", usersService.getAllUtenti());
return mav;
}
}
@RequestMapping(value = "logout", method = RequestMethod.GET)
public ModelAndView logout(HttpServletRequest request) {
request.getSession().invalidate(); //invalido i dati presenti in sessione
return new ModelAndView("login");
}
}
并在jsp代码中使用此代码显示图像
<img src="data:image/jpeg;base64,${userImage}" />