在PostgreSQL中使用Hibernate上传图像



我正在开发一个Spring-MVC应用程序,我必须使用图像上传来将图像存储在数据库中。我试图通过互联网上的示例,但我的总体观察是关于上传类型以及谁管理它存在一些争论。尽管如此,我还是从 HTML 上传图像,将其作为 OID 保存在数据库中,使用带有@Lob的字节数组进行保存。当我使用时,我无法看到图像预览:

我将发布我如何实现它的代码,请让我知道出了什么问题。

用于图像预览的HTML代码:

 <img src="${product.productimage}" width="100" height="100"/>

当我在服务中使用product.getImage().toString时,我得到'B[jlkisf12'的奇怪字符,但只有12-13个,我想这就是路径。

实体:

@Column(name = "productimage")
byte[] productimage;  // With getters and setters

JSP 文件 :

   <c:url var="add" value="/product/add"></c:url>
<form:form action="${add}" commandName="product">
    <table>
        <c:if test="${!empty product.productname}">
            <tr>
                <td>
                    <form:label path="productid">
                        <spring:message text="productid"/>
                    </form:label>
                </td>
                <td>
                    <form:input path="productid" readonly="true" size="8"  disabled="true" />
                    <form:hidden path="productid" />
                </td>
            </tr>
        </c:if>
        <tr>
            <td>
                <form:label path="productname">
                    <spring:message text="productname:"/>
                </form:label>
            </td>
            <td>
                <form:input path="productname"/>
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="productdescription">
                    <spring:message text="productdescription"/>
                </form:label>
            </td>
            <td>
                <form:input path="productdescription"/>
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="productcondition">
                    <spring:message text="productcondition"/>
                </form:label>
            </td>
            <td>
                <form:input path="productcondition"/>
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="productage">
                    <spring:message text="productage"/>
                </form:label>
            </td>
            <td>
                <form:input path="productage" />
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="productean">
                    <spring:message text="productean"/>
                </form:label>
            </td>
            <td>
                <form:input path="productean" />
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="productisbn">
                    <spring:message text="productisbn"/>
                </form:label>
            </td>
            <td>
                <form:input path="productisbn" />
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="ordernumber">
                    <spring:message text="ordernumber"/>
                </form:label>
            </td>
            <td>
                <form:input path="ordernumber" />
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="productimage">
                    <spring:message text="productimage"/>
                </form:label>
            </td>
            <td>
                <form:input type="file" path="productimage" />
            </td>
        </tr>

    </table>
    <tr>
        <td colspan="2">
            <c:if test="${!empty product.productname}">
                <input type="submit"
                       value="<spring:message text="Edit Product"/>" />
            </c:if>
            <c:if test="${empty product.productname}">
                <input type="submit"
                       value="<spring:message text="Add Product"/>" />
            </c:if>
        </td>
    </tr>
</form:form>
<br>
<h3>Product List</h3>
<c:if test="${!empty listProducts}">
    <table class="tg">
        <tr>
            <th width="80">Product ID</th>
            <th width="80">Product image</th>
            <th width="120">Product name</th>
            <th width="120">Product description</th>
            <th width="120">Product condition</th>
            <th width="120">Product age</th>
            <th width="120">Product EAN</th>
            <th width="120">Product ISBN</th>
            <th width="120">Product ordernumber</th>
            <th width="120">Product owners id</th>
            <th width="60">Edit</th>
            <th width="60">Delete</th>
        </tr>
        <c:forEach items="${listProducts}" var="product">
            <tr>
                <td>${product.productid}</td>
                <td>${product.productimage} </td>
                <td>${product.productname}</td>
                <td>${product.productdescription}</td>
                <td>${product.productcondition}</td>
                <td>${product.productage}</td>
                <td>${product.productean}</td>
                <td>${product.productisbn}</td>
                <td>${product.ordernumber}</td>
                <td>${product.user1id}</td>
          <img src="${image}" width="100" height="100"/> //image not found
                <td><a href="<c:url value='/editproduct/${product.productid}' />" >Edit Product</a></td>
                <td><a href="<c:url value='/removeproduct/${product.productid}' />" >Delete Product</a></td>
            </tr>
            <img src="${product.saveimage}" height="100" width="100">
        </c:forEach>

    </table>
</c:if>

控制器:

     @RequestMapping(value="/product/show",method= RequestMethod.GET)
     public String listProducts(@ModelAttribute("product") ProductBasic productBasic,Model model)   {
     User user = userService.getCurrentlyAuthenticatedUser();
     model.addAttribute("product", new ProductBasic());
     model.addAttribute("listProducts",this.productBasicService.listProduct(user));
     BASE64Encoder base64Encoder = new BASE64Encoder();
     StringBuilder stringBuilder = new StringBuilder();
     stringBuilder.append("data:image/png;base64,");
     stringBuilder.append(base64Encoder.encode(productBasic.getProductimage())); 
     String image = stringBuilder.toString();
     model.addAttribute("saveimage",image);
     return "product";
 }
@RequestMapping(value="/product/add")
public String addProduct(@ModelAttribute("product") ProductBasic productBasic,Model model){
    User user = userService.getCurrentlyAuthenticatedUser();
    model.addAttribute("product", new ProductBasic());
    productBasicService.addProduct(user,productBasic);
    return "redirect:/product/show";
}
  @RequestMapping("/removeproduct/{id}")
    public String removeProduct(@PathVariable("id") int productid,Model model) {
        User user = userService.getCurrentlyAuthenticatedUser();
        model.addAttribute("listProducts",this.productBasicService.listProduct(user));
        this.productBasicService.removeProduct(productid,user);
        return "redirect:/product/show";
    }
    @RequestMapping("/editproduct/{id}")
    public String updateProduct(@ModelAttribute("product") ProductBasic productBasic,@PathVariable("id") Integer id,Model model){
        User user = userService.getCurrentlyAuthenticatedUser();
        model.addAttribute("product", this.productBasicService.getProductById(id));
        model.addAttribute("listProducts",this.productBasicService.listProduct(user));
        return "product";
    }

空指针异常:

java.lang.NullPointerException
    java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:106)
    sun.misc.CharacterEncoder.encode(CharacterEncoder.java:188)
    com.WirTauschen.UserController.listProducts(UserController.java:67)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

我做错了什么?请告诉我。

在控制器或方法中添加以下图像代码

    BASE64Encoder base64Encoder = new BASE64Encoder();
    StringBuilder imageString = new StringBuilder();
    imageString.append("data:image/png;base64,");
    imageString.append(base64Encoder.encode(bytes)); //bytes will be image byte[] come from DB 
    String image = imageString.toString();
    modelView.put("imagetoDisplay",image);// put in your model object for using in your JSP

在您的 JSP 中

  <img src="${imagetoDisplay}" width="100" height="100"/>

最新更新