我正在尝试显示客户个人资料的图像。并且图像以blob类型存储在mysql中。我在休眠状态下使用spring mvc。我知道要显示blob类型的图像,我们必须转换为字节。我做了一定程度的努力。现在卡住了,无法显示任何图像。
my controller:
@RequestMapping(value="/myProfile.htm", method=RequestMethod.GET)
public ModelAndView Profilelist(HttpServletRequest request,ModelMap model,Customer
customer,Profile profile, HttpServletResponse response) throws SQLException ,
Exception{
//Profile profile = new Profile();
String customerName = request.getUserPrincipal().getName();
customer = customerService.getCustomerId(customerName);
profile = profileService.getBycustomerId(customer);
System.out.println("cust: "+ customer);
System.out.println("profile: "+ profile);
logger.error("Viewing Profile" +customerName);
//Customer customer = new Customer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
/*byte[] encodeBase64 = Base64.encodeBase64(buf);
String base64Encoded = new String(encodeBase64, "UTF-8");
ModelAndView mav = new ModelAndView();
mav.addObject("profile", base64Encoded);*/
Blob blob = profile.getContent();
InputStream in = blob.getBinaryStream();
System.out.println("id content" +in);
int n = 0;
while ((n=in.read(buf))>=0)
{
baos.write(buf, 0, n);
}
in.close();
byte[] bytes = baos.toByteArray();
System.out.println("bytes" +bytes);
byte[] encodeBase64 = Base64.encodeBase64(buf);
String base64Encoded = new String(encodeBase64, "UTF-8");
ModelAndView mav = new ModelAndView();
//mav.addObject("content", base64Encoded);
customer.setEmailId(customerName);
profile.setCustomer(customer);
//profile.setContent(blob);
System.out.println();
profile = profileService.findProfileById(customer);
model.addAttribute("content",base64Encoded);
model.addAttribute("profile", profile);
mav = new ModelAndView("myProfile");
return mav;}
在jsp中我称它为
<img src= "data:image/jpeg;bytes,${profile.content}"/>
我的jsp是
<form:form method="GET" modelAttribute="profile" action="myProfile.htm"
enctype="multipart/form-data">
<div class="containerdiv" align="center" >
<img src= "data:image/jpeg;bytes,${content}"/>
</div>
</form:form>
最佳答案
在数据库上保留Image Blob对象不是一个好习惯。只需将图像的文件名持久保存在db中,并将图像本身保存到特定路径即可。
我知道这不是您要的,但是如果您知道如何将照片上传到特定目录,并且由于使用的是Spring,则可以将其作为替代方法。
您为什么不创建一个控制器来为您读取图像。看到这个:
@Controller
public class ImageReadFile{
// this is for mapping your image related path.
@RequestMapping(value="/image/*")
public void readImage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = request.getServletContext();
//here i uploaded my image in this path
String imagePath = "/home/somefolder/Workspaces/Images/";
String [] fragmentFilename = request.getServletPath().split("/");
//Check if image isn't set
if(fragmentFilename.length <= 2){
return;
}
String filename = fragmentFilename[2];
String requestedImage = "/"+filename;
if(filename == null){
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8"));
if(!image.exists()){
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String contentType = sc.getMimeType(image.getName());
response.reset();
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(image.length()));
Files.copy(image.toPath(), response.getOutputStream());
}
}
这就是您要显示图像的方式
<img alt="${profile.filename}" src="${pageContext.request.contextPath}/image/${profile.filename}">
这就是我在Webapp中显示图像的方式。