将blob图像转换为数组以在spring mvc的网页中显示 [英] converting blob image to array to display in the web page in spring mvc
问题描述
我的控制器:
@RequestMapping(value =/ myProfile.htm,method = RequestMethod.GET)
public ModelAndView Profilelist(HttpServletRequest请求,ModelMap模型,Customer
customer,Profile profile,HttpServletResponse响应)throws SQLException,
Exception {
// Profile profile = new Profile();
字符串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);
//客户客户=新客户();
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; ((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 =GETmodelAttribute =profileaction =myProfile.htm
enctype =multipart / form-data>
< div class =containerdivalign =center>
< img src =data:image / jpeg; bytes,$ {content}/>
< / div>
< / form:form>
实践。只需将图像的文件名保存在数据库中,并将图像本身保存到特定路径即可。
我知道这不是您要求的,但是如果您知道如何上传一张照片到一个特定的目录,因为你正在使用春天你可以把它作为一种选择。
为什么不创建控制器来为您读取图像?看看这个:
@Controller
public class ImageReadFile {
//这是为映射您的图像相关路径。
@RequestMapping(value =/ image / *)
public void readImage(HttpServletRequest请求,HttpServletResponse响应)
throws ServletException,IOException {
ServletContext sc = request.getServletContext();
//我在这里上传了我的图片
String imagePath =/ home / somefolder / Workspaces / Images /;
String [] fragmentFilename = request.getServletPath()。split(/);
//检查图像是否未设置
if(fragmentFilename.length <= 2){
return;
}
字符串文件名= 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());
}
}
这就是你要如何使用image
< img alt =$ {profile.filename}src =$ {pageContext.request.contextPath} /图像/ $ {profile.filename}>
这是我在webapp中显示图片的方式。
Am trying to display an image for the profile of a customer. and the image is stored in blob type in mysql. am using spring mvc with hibernate. i know that to display an image which is blob type, we have to convert into bytes. i did upto some extent. now am stuck that am not able to display any image.
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;}
And in jsp am calling it as
<img src= "data:image/jpeg;bytes,${profile.content}"/>
My jsp is
<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>
Persisting Image Blob object on your database is not a good practice. just persist the filename of your image in db and the image itself to a certain path.
I know that this is not what you asking for but If you know how to upload a photo to a specific directory and since you are using spring You can take it as an alternatives.
why dont you create a Controller to read the image for you. see this:
@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());
}
}
This is how you gonna diplay the image
<img alt="${profile.filename}" src="${pageContext.request.contextPath}/image/${profile.filename}">
this is how i display the image in my webapp.
这篇关于将blob图像转换为数组以在spring mvc的网页中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!