在PostgreSQL中使用Hibernate进行图像上传

时间:2014-11-17 14:53:52

标签: spring hibernate postgresql

我正在开发一个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";
    }

的NullPointerException:

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)

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

1 个答案:

答案 0 :(得分:2)

在您的控制器或方法中添加下面的图像代码

    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"/>