java - Upload file via rest service -
i want upload files restservice. found in web, still can't fix problem. maybe because i'am using spring in rest service?! here code:
html
<input type="button" value="upload document" id="button"> <input id="filetoupload" type="file">
jquery
jquery("document").ready(function () { $("#button").on("click", function () { var fileinput= $('input[name="fileinput"]')[0].files[0]; var data = new formdata(); data.append('file', fileinput); $.ajax({ url: '.../upload', type: 'post', data: data, cache: false, contenttype: false, processdata: false, success: function(data){ alert("successfully."); }, error: function(data){ alert("failed."); } }); }); });
java (restservice)
@requestmapping(value = "/upload", method = { requestmethod.post }, produces = { mediatype.multipart_form_data_value, mediatype.application_form_urlencoded_value, mediatype.application_json_value }) public void uploadingfile(@requestbody inputstream uploadedinputstream) throws ioexception { system.out.println("uploadedinputstream: " + uploadedinputstream); }
exception
servlet.service() servlet rest api dispatcher threw exception: java.lang.illegalstateexception: parameters processing failed.
is rest service ok? can't find bug :(
pom.xml
<!-- spring --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> </dependency> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>1.3.1</version> </dependency> <dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version>2.4</version> </dependency>
application-context.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name="maxuploadsize" value="50000000" /> </bean>
solution
java (restservice)
@requestmapping(value = "/upload", method = requestmethod.post) public void uploadingfile(@requestparam("file") multipartfile uploadedinputstream) throws ioexception { system.out.println("...the parameter binding works now"); }
java (spring config)
@bean public static commonsmultipartresolver multipartresolver() { commonsmultipartresolver cmr = new commonsmultipartresolver(); cmr.setmaxuploadsize(50000000); return cmr; }
(alternative - application.xml)
<bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name="maxuploadsize" value="50000000" /> </bean>
your handler method should bellow:
@requestmapping(value = "/upload", method = requestmethod.post) public void uploadingfile(multiparthttpservletrequest request, httpservletresponse response) throws ioexception { iterator<string> itr = request.getfilenames(); multipartfile file = request.getfile(itr.next()); system.out.println(file.getoriginalfilename() +" uploaded!"); }
don't forget add following lines in application context:
<mvc:annotation-driven /> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name="maxuploadsize" value="1048576"/> </bean>
Comments
Post a Comment