Java 休眠图像在保存时被截断
我正在开发一个SpringMVC应用程序。在这里,我试图上传一个图像并保存在数据库中。在这个过程中,我尝试将图像的二进制数据转换为字符串,我注意到图像的二进制数据被截断。一般来说,如果你拍摄一张20-30kb的图像,那么在字符串中你可以看到至少500-600个字符,我只能看到6-8个字符。我正在演示如何实现图像保存和显示。请让我知道我做错了什么 实体:Java 休眠图像在保存时被截断,java,spring,hibernate,Java,Spring,Hibernate,我正在开发一个SpringMVC应用程序。在这里,我试图上传一个图像并保存在数据库中。在这个过程中,我尝试将图像的二进制数据转换为字符串,我注意到图像的二进制数据被截断。一般来说,如果你拍摄一张20-30kb的图像,那么在字符串中你可以看到至少500-600个字符,我只能看到6-8个字符。我正在演示如何实现图像保存和显示。请让我知道我做错了什么 实体: @Entity @Table(name = "product") public class ProductBasic { @Column(na
@Entity
@Table(name = "product")
public class ProductBasic {
@Column(name = "productimage")
byte[] productimage;
public byte[] getProductimage() {
return productimage;
}
public void setProductimage(byte[] productimage) {
this.productimage = productimage;
}
}
保存时的产品服务:
@Transactional
@Service
public class ProductBasicServiceImpl implements ProductBasicService{
private ProductBasicDAO productBasicDAO;
public void setProductBasicDAO(ProductBasicDAO productBasicDAO) { this.productBasicDAO = productBasicDAO; }
@Override
@Transactional
public void addProduct(User user, ProductBasic p){
p.setProductimage(org.apache.commons.codec.binary.Base64.encodeBase64(p.getProductimage()));
this.productBasicDAO.addProduct(user, p);
}
}
用于将图像作为bytearray发送的控制器:
@RequestMapping(value = "/product/{id}/image")
public byte[] retrieveimage(@PathVariable("id")Integer id){
ProductBasic productBasic1 = productBasicService.getProductById(id);
if(productBasic1.getProductimage()==null){
System.out.println("product by id image is null");
return null;
}else {
System.out.println("Product by id image is not null");
byte[] data = org.apache.commons.codec.binary.Base64.decodeBase64(productBasic1.getProductimage());
// When I convert above byte[] to String using String builder and append data:image/png;bas64, I get String as "data:image/png;base64,B[jak23424=="
return data;
}
}
JSP:
任何指示都很好。我使用的是PostgreSQL 9.3,在数据库中我将其保存为bytea。将其保存为blob,而不是byte[],其中,在Java或数据库中,Postgres中没有blob数据类型,只有OID或byte[]或bytea。请查看以下答案:是的,在数据库中,我已将其保存为bytea,如我文章最后一行所述。如果这就是你想告诉我的。为什么会被截断?试着用java中的Blob映射它
<tr>
<td>
<form:label path="productimage">
<spring:message text="productimage"/>
</form:label>
</td>
<td>
<form:input type="file" path="productimage" />
</td>
</tr>
<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="${product.productimage}" height="100" width="100"/>--%>
<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/${product.productid}/image" height="100" width="100"/>
<%--<img src="${saveimage}" height="100" width="100">--%>
</c:forEach>
</table>
</c:if>
@Override
@Transactional
public void addProduct(User user, ProductBasic p){
try {
FileOutputStream imageoutfile = new FileOutputStream("/home/akshay/check.png");
imageoutfile.write(p.getProductimage());
imageoutfile.close();
File file = new File("/home/akshay/check.png");
FileInputStream fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
p.setProductimage(org.apache.commons.codec.binary.Base64.encodeBase64(p.getProductimage()));
this.productBasicDAO.addProduct(user, p);
}