2014-10-27 89 views
1

我在mysql數據庫中有一個BLOB類型的圖像,我想在jsp上顯示圖像。我使用Hibernate和Spring MVC。這是我的模型類:在jsp上用spring mvc和hibernate顯示圖像

@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中顯示圖像?

回答

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

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(); 
} 
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}" />