java - How to upload a File with hibernate-ogm and Mongodb? -
i have been able upload strings , dates mongodb, cannot figure out how file in there using webapp.
i'd rather not use gridfs, byte[] or blob (but if must use gridfs, then, must).
here have far:
index.jsp:
<form action="./fileuploadservlet" method="post"> <input type="text" name="subject"/><br> <input type="date" name="docdate"/> <%--todo: accept file db--%> <input type="file" name="filecontent"/> <input type="submit" value="submit"> </form>
fileuploadservlet.java:
//imports... @webservlet(name = "fileuploadservlet", urlpatterns = {"/fileuploadservlet"}) public class fileuploadservlet extends httpservlet { throws servletexception, ioexception, exception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); try { fileuploaddao fileuploaddao = new fileuploaddao(); fileuploadentity fileuploadentity = new fileuploadentity(); string subject = request.getparameter("subject"); string datestr = request.getparameter("docdate"); //todo: file contents date date = new simpledateformat("yyyy-mm-dd").parse(datestr); fileuploadentity.setsubject(subject); fileuploadentity.setdocdate(date); //todo: set file contents fileuploaddao.persistaction(fileuploadentity); response.sendredirect("/jdc/index.jsp"); } { out.close(); } } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { processrequest(request, response); } catch (exception ex) { logger.getlogger(fileuploadservlet.class.getname()).log(level.severe, null, ex); } } }
how file in there? tried
request.getparameter("filecontent"); request.getattribute("filecontent");
but did not work.
i able upload binary data database, i'm not sure how retrieve it. here have though:
add these lines processrequest method:
byte[] b = request.getparameter("filecontent").getbytes(); fileuploadentity.setfilecontent(b);
Comments
Post a Comment