* 读取图片
* 用@RequestParam
* @param userId
* @return
*/
@RequestMapping("/showimage")
public @ResponseBody InputStream showImage(@RequestParam("userId") Long userId) {
User user = null;
try {
Session session = HibernateSessionFactory.getSession();
user = (User) session.get(User.class, id);
if (null == user)
return null;
session.close;
} catch (Exception e) {
e.printStackTrace();
}
Blob blob = user.getPortrait();
byte[] data = null;
PrintWriter out = null;
InputStream inputStream = null;
try(InputStream is = blob.getBinaryStream();) {
int length = (int) blob.length();
data = new byte[length];
is.read(data);
out = response.getWriter();
inputStream = new ByteArrayInputStream(data);
int a = inputStream.read();
while (a != -1) {
out.print((char) a);
a = inputStream.read();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != out) {
out.flush();
out.close();
}
if (null != inputStream) {
inputStream.cloase();
}
}
}