Reputation:
I'm having some trouble reading a blob back from a MySQL database. I've gotten it to successfully insert into the database, but can't seem to get it to read back. I know some of you might be thinking "why is he using a database to store blobs for images, and not just the file paths / file names", but i want to have flexibility and as a lot of these images will be stored on a server and not locally, this optimises efficiency, as well as allowing me to move images to local if needed. I've followed a (short) tutorial and have written this following method for recieving a blob.
the below is my domain class:
@Entity
@Table(name="PROFILE")
public class Profile implements Serializable{
private static final long serialVersionUID = 3641L;
@Id
@Column(name="PROFILE_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private int profileId;
@OneToOne
@JoinColumn(name="CUSTOMER_ID")
private Customer customer;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "ADDRESS")
private String address;
@Column(name = "CONTACT_NUMBER")
private String contactNumber;
@Column(name = "PROJECT_NAME")
private String projectName;
@Column(name="CONTENT")
@Lob
private Blob content;
@Column(name="filename")
private String filename;
@Column(name="content_type")
private String contentType;
//getters and setter
}
this is 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();
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;
}
here am unable to set the bytes to my profile.setContent...... please give any solution to get rid of my issue
Upvotes: 0
Views: 7382
Reputation: 28519
You should not do it in the controller, from the docs
By default drivers implement Blob using an SQL locator(BLOB), which means that a Blob object contains a logical pointer to the SQL BLOB data rather than the data itself. A Blob object is valid for the duration of the transaction in which is was created.
What you should do instead, if you need an access to your byte array, is create a JPA transient property in your Profile class, e.g. something like
@Transient byte[] image; // do add getters and setters
and set the byte array to that property. Transient properties are ignored by your persistence mechanism.
UPDATE with respect to the comment
Since the intention is to show the picture in jsp, you can go two ways, first one, is to store as file at a web accessible location and simply point to it from a src element, but I understand from your question that this is not what you need.
Second option is to render an image as a sequence of bytes, but in order to do so you first must encode your byte array into a Base64 encoded String. Here's an example
To show the image on JSP without storing to filesystem and linking to it, you'll have to do a Base64 encoding of the byte array. Change your controller method to
@RequestMapping(value="/myProfile.htm", method=RequestMethod.GET)
public String 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];
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");
customer.setEmailId(customerName);
profile.setCustomer(customer);
//profile.setContent(blob);
System.out.println();
profile = profileService.findProfileById(customer);
model.addAttribute("content",base64Encoded);
model.addAttribute("profile", profile);
return "myProfile";
}
Upvotes: 2