image to blob
argonist Aug 7, 2007 5:44 AMHello,
I have problem with Blob type.
A Image should be uploaded then stored in tempory file. The image should be stored from tempory file into mysql over enitity bean as Blob Object.
How can I make from image to blob object without SQL-statement?
Enitity Bean InterpreterImage
public class InterpreterImage implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="ID") private int id; @Column(name="GALLERYFILENAME") private String galleryFilename; @Column(name="GALLERYIMAGE") private Blob galleryImage; @Column(name="PROFILFILENAME") private String profilFilename; @Column(name="PROFILIMAGE") private Blob profilImage;
Store Blob Object into entity Bean Interpreter.interpreterImage
...... interpreter.getInterpreterImage().setProfilImage( setImagetoPath(profilFile.getInputStream(), filename)); .....
From Image to Blob Object. But how???
private Blob setImagetoPath(InputStream input, String Filename) { FileOutputStream fos; Blob blob = null; String realPath=null; byte[] buff = new byte[8192]; int len = 0; try { System.out.println(Filename); realPath = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("/images"); fos = new FileOutputStream(realPath+"/"+Filename); while (0 < (len = input.read(buff))){ fos.write(buff, 0, len); } System.out.println(fos.toString()); fos.close(); input.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { //Image over byte[] buff to Blob Object // HOW???? } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return blob; }
Store simple Bean into database
public void createInterpreter(Interpreter interpreter) { manager.persist(interpreter); }
I hope, that you have an idea... Thank you in advance
Manu