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