I am working on a Spring-MVC application. In this, I am trying to upload an image and save it in database. During that process, I tried to convert the binary data of the image to a String, and I noticed that the images binary data is truncated. Generally, if you take a image of 20-30kb, then in String one can see atleast 500-600 characters, I can only see 6-8. I am showing how I have implemented the image save and display. Kindly let me know what I am doing wrong..
Entity :
@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;
}
}
Product service while saving. :
@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);
}
}
Controller for sending image as 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 :
<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>
Also, when I use the below function in ServiceImpl, the saved image is just 10bytes instead of 80Kb, which is the actual image size.
@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);
}
Any pointers would be nice. i am using PostgreSQL 9.3, and in database i am saving it as bytea.