java - how to store image or any type of document in database using mvc -
i have 1 pojo class setter , getter method , 1 servlet class in getting values jsp page , calling function of dataaccess class employeebean , passing parameter problem coming not getting inserted databse because blob , datatype probelm please me here...
public class employee { private int empid; private string iname; private string photo; private string contenttype; private int contentlength; private inputstream inputstream; public inputstream getinputstream() { return inputstream; } public void setinputstream(inputstream inputstream) { this.inputstream = inputstream; } public string getcontenttype() { return contenttype; } public void setcontenttype(string contenttype) { this.contenttype = contenttype; } public int getcontentlength() { return contentlength; } public void setcontentlength(int contentlength) { this.contentlength = contentlength; } public string getiname() { return iname; } public void setiname(string iname) { this.iname = iname; } } @multipartconfig(maxfilesize = 18177215) @webservlet(name = "fileuploadservlet", urlpatterns = {"/fileuploadservlet"}) public class fileuploadservlet extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(true); inputstream inputstream = null; employee emp1 = (employee)session.getattribute("emp1"); int n = emp1.getempid(); emp1.setempid(emp1.getempid()); emp1.setiname(request.getparameter("iname")); part filepart = request.getpart("photo"); if (filepart != null) { emp1.setcontenttype(filepart.getcontenttype()); emp1.setcontentlength((int) filepart.getsize()); inputstream = filepart.getinputstream(); emp1.setinputstream(inputstream); employeebean eb = new employeebean(); emp1 = eb.addimage(emp1); session.setattribute("emp1",emp1); response.sendredirect("employeedetail.jsp"); } } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override public string getservletinfo() { return "short description"; } } public employee addimage(employee emp1) { connection con = null; statement stmt = null; preparedstatement pstmt = null; resultset rs = null; try { con = connectionmanager.getconnection(); stmt = con.createstatement(); pstmt = con.preparestatement("insert upload_documents(name, contenttype, contentlength, empid, photo) values(?,?,?,?,?)"); pstmt.setstring(1, emp1.getiname()); pstmt.setstring(2, emp1.getcontenttype()); pstmt.setint(3, emp1.getcontentlength()); pstmt.setint(4, emp1.getempid()); pstmt.setblob(5,emp1.getinputstream()); pstmt.execute(); } catch (sqlexception ex) { } { try { if (stmt != null) { stmt.close(); } if (con != null) { con.close(); } } catch (sqlexception ex) { logger.getlogger(employeebean.class.getname()).log(level.severe, null, ex); } } return emp1; }
public class employee { .... private byte[] filebytes; ..... } @multipartconfig(maxfilesize = 18177215) @webservlet(name = "fileuploadservlet", urlpatterns = {"/fileuploadservlet"}) public class fileuploadservlet extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { .... inputstream = filepart.getinputstream() emp1.setfilebytes(ioutils.tobytearray(is)) } ...... } public employee addimage(employee emp1) { connection con = null; statement stmt = null; .... pstmt.setbytes(5,emp1.getfilebytes()); .... }
Comments
Post a Comment