apache commons fileupload - Spring MVC file upload controller - I'd like the controller to be called as soon as the upload starts -
using naked servlet's dopost, when file upload starts, dopost called. can stream files request object using commons fileitemiterator.
using spring mvc, can't seem controller method fire until after file(s) have been received server, not ideal.
i want servlet/controller method process many files can , perform rollback operations if upload interrupted. can't spring mvc currently.
public void dopost(httpservletrequest request, httpservletresponse res){ //i can stream response here } vs.
@requestmapping(value="/uploadfiles", method= requestmethod.post) public @responsebody string addfiles(contentmanagerticket ticket, httpservletrequest request){ //i can't until files received - whether utilize httpservletrequset or multipartfile } any ideas? thanks!
you want streaming file uploads when using spring’s multipart (file upload) support uses the classic approach. means multipart parts of request parsed before request handed downwards controller. needed because multipartfile can used method argument , work needs available controller.
if want handle streaming file uploads have disable spring's multipart back upwards , the parsing in controller, same way in servlet.
@controller public class fileuploadcontroller { @requestmapping("/upload") public void upload(httpservletrequest request) { boolean ismultipart = servletfileupload.ismultipartcontent(request); if (!ismultipart) { // inform user invalid request } // create new file upload handler servletfileupload upload = new servletfileupload(); // parse request fileitemiterator iter = upload.getitemiterator(request); while (iter.hasnext()) { fileitemstream item = iter.next(); string name = item.getfieldname(); inputstream stream = item.openstream(); if (item.isformfield()) { system.out.println("form field " + name + " value "+ streams.asstring(stream) + " detected."); } else { system.out.println("file field " + name + " file name " + item.getname() + " detected."); // process input stream ... } } } } see how upload file using commons file upload streaming api , apache commons fileupload "streaming api"
spring-mvc apache-commons-fileupload
No comments:
Post a Comment