Spring File upload and persisting object -
i have form has 2 submit buttons. save button save object database , @ same time save upload file local directory. while send button generate email automatically recipient.
what have far
@requestmapping(value = "/staff/setter/addpage", method = requestmethod.post) public string processmodule(@requestparam("name") string name, @requestparam("file") multipartfile file, @modelattribute("module") module module, staff staff, httpservletrequest request, bindexception errors, httpsession session) { if( request.getparameter("save") != null ) { if(module != null){ try { multipartfile filea = module.getfiledata(); inputstream inputstream = null; outputstream outputstream = null; if (filea.getsize() > 0) { inputstream = filea.getinputstream(); outputstream = new fileoutputstream("c:\\test\\" + filea.getoriginalfilename()); system.out.println("==========uploaded file name========="); system.out.println(filea.getoriginalfilename()); system.out.println("===================================="); int readbytes = 0; byte[] buffer = new byte[8192]; while ((readbytes = inputstream.read(buffer, 0, 8192)) != -1) { outputstream.write(buffer, 0, readbytes); } outputstream.close(); inputstream.close(); session.setattribute("success", "file uploaded successfully"); session.setattribute("uploadfile", "c:\\test\\" + filea.getoriginalfilename()); } } catch (exception e) { e.printstacktrace(); } //save specific processing moduleservice.save(module); } return "redirect:/staff/setter/addpage"; } else if( request.getparameter("send") != null ) { //send specific processing. simplemailmessage msg = new simplemailmessage(this.templatemessage); //msg.setto(module.getcustomer().getemailaddress()); msg.setto(module.getchecker().getemail()); //msg.setto(staff.getchecker().getemail()); msg.settext( "dear " + module.getchecker().getname() //"dear " + staff.getchecker().getemail() //+ module.getcustomer().getlastname() + "a coursework has been submitted checking" + module.getmodulecode() +module.getmodulename()); try{ this.mailsender.send(msg); } catch(mailexception ex) { // log , go on... system.err.println(ex.getmessage()); } } return "/staff/setter/view_submission"; }
so far error
http status 400 - request sent client syntactically incorrect.
i appreciate help.
edit: jsp page
<c:url var="processurl" value="/staff/setter/addpage" /> <form:form modelattribute="module" method="post" action="${processurl}" name="module" enctype="multipart/form-data"> <form:hidden path="moduleid" /> <div class="admin"> <table> <tr> <td class="module"> </td> <td>module code:</td> <td>module name:</td> </tr> <tr> <td>convenor:</td> <td>checker:</td> </tr> <tr> <td>weights:</td> </tr> </table> </div> <div class ="lecturer"> <h4>lecturer section</h4> <ul> <form:label path="filename">document title:</form:label> <form:input path="filename" name="name"/><br/> <form:label path="documentpath">coursework sample:</form:label> <form:input path="documentpath" type="file" name="file" id="file"/><br/> <form:label path="livedate">live date:</form:label> <form:input path="livedate"/><br/> <form:label path="submissiondate">submission date:</form:label> <form:input path="submissiondate"/><br/> <form:label path="filename">marked sample title:</form:label> <form:input path="filename" id="file" name="name"/><br/> <form:label path="markedsamplepath">marked sample:</form:label> <form:input path="markedsamplepath" type="file" name="file"/><br/> </ul> </div> </form:form>
if avoid hassle of handling multipart forms , possibly give user nicer experience. example form data has error , user needs resubmit form fileupload lost.
i use fineuploader build uploadedfilequeue hidden inputs , uploads themself seperate requests.
explained here: preserving value <form:input type="file"> spring mvc
Comments
Post a Comment