java - Jersey 2.x File Upload NPE -
hi all,
after fiddling whole day , finding solutions internet still not able resolve problem. need brilliant brains out there in universe me out. :-) trying upload file server using jersey restful services. while trying getting null pointer exception. pointers highly appreciated.
following environment configurations:
- jdk 1.8
- tomcat 8.0
jersey jars:
- jersey-media-multipart-2.12.jar
- mimepull-1.9.3.jar
- jersey-media-moxy-2.13.jar
- jersey-media-jaxb.jar
- jersey-client-2.13.jar
- jersey-server-2.13.jar
- jersey-client.jar
- javax.servlet-api-3.0.1.jar
below web.xml has url pattern restful services.
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>mixtri</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <display-name>mixtri</display-name> <servlet> <servlet-name>jersey rest service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <!-- register resources , providers under com.vogella.jersey.first package. --> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.mixtri.uploader</param-value> </init-param> <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>org.glassfish.jersey.filter.loggingfilter;org.glassfish.jersey.media.multipart.multipartfeature</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey rest service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
below simple html form 2 buttons browse , upload respectively:
<div class="container"> <form action="/mixtri/rest/upload" method="post" enctype="multipart/form-data"> <input type="file" value="browse" name="file"> <input type="submit" value="upload"> </form> </div>
below resource class has 2 input params formdatacontentdisposition
, inputstream
.
package com.mixtri.uploader; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import javax.ws.rs.consumes; import javax.ws.rs.post; import javax.ws.rs.path; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.response; import org.glassfish.jersey.media.multipart.formdatacontentdisposition; import org.glassfish.jersey.media.multipart.formdataparam; @path("/") public class uploadfileservice { @post @path("/upload") @consumes(mediatype.multipart_form_data) public response uploadfile( @formdataparam("file") inputstream uploadedinputstream, @formdataparam("file") formdatacontentdisposition filedetail) { // save savetodisk(uploadedinputstream, filedetail); string output = "d:/uploaded/" + filedetail.getname(); return response.status(200).entity(output).build(); //return "file uploaded successfully"; } // save uploaded file new location private void savetodisk(inputstream uploadedinputstream, formdatacontentdisposition filedetail) { string uploadedfilelocation = "d:/uploaded/" + filedetail.getname(); try { outputstream out = new fileoutputstream(new file( uploadedfilelocation)); int read = 0; byte[] bytes = new byte[1024]; out = new fileoutputstream(new file(uploadedfilelocation)); while ((read = uploadedinputstream.read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); out.close(); } catch (ioexception e) { e.printstacktrace(); } } }
below stacktrace has information request sent server in initial few lines , after null pointer exception occurs. stuck @ point , not able move forwards
jul 01, 2015 6:58:49 pm org.glassfish.jersey.filter.loggingfilter log info: 3 * server has received request on thread http-nio-8080-exec-8 post http://localhost:8080/mixtri/rest/upload accept: text/html, */*; q=0.01 accept-encoding: gzip, deflate accept-language: en-us,en;q=0.8 connection: keep-alive content-length: 0 cookie: jsessionid=ca18d302131aab8dd4a77eff95dd4a45 host: localhost:8080 origin: http://localhost:8080 referer: http://localhost:8080/mixtri/sample.jsp user-agent: mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.130 safari/537.36 x-requested-with: xmlhttprequest jul 01, 2015 6:58:49 pm org.apache.catalina.core.standardwrappervalve invoke severe: servlet.service() servlet [jersey rest service] in context path [/mixtri] threw exception [java.lang.nullpointerexception] root cause java.lang.nullpointerexception @ org.glassfish.jersey.media.multipart.internal.formdataparamvaluefactoryprovider$formdataparamvaluefactory.provide(formdataparamvaluefactoryprovider.java:205) @ org.glassfish.jersey.server.spi.internal.parametervaluehelper.getparametervalues(parametervaluehelper.java:81) @ org.glassfish.jersey.server.model.internal.javaresourcemethoddispatcherprovider$abstractmethodparaminvoker.getparamvalues(javaresourcemethoddispatcherprovider.java:121) @ org.glassfish.jersey.server.model.internal.javaresourcemethoddispatcherprovider$responseoutinvoker.dodispatch(javaresourcemethoddispatcherprovider.java:152) @ org.glassfish.jersey.server.model.internal.abstractjavaresourcemethoddispatcher.dispatch(abstractjavaresourcemethoddispatcher.java:104) @ org.glassfish.jersey.server.model.resourcemethodinvoker.invoke(resourcemethodinvoker.java:384) @ org.glassfish.jersey.server.model.resourcemethodinvoker.apply(resourcemethodinvoker.java:342) @ org.glassfish.jersey.server.model.resourcemethodinvoker.apply(resourcemethodinvoker.java:101) @ org.glassfish.jersey.server.serverruntime$1.run(serverruntime.java:271) @ org.glassfish.jersey.internal.errors$1.call(errors.java:271) @ org.glassfish.jersey.internal.errors$1.call(errors.java:267) @ org.glassfish.jersey.internal.errors.process(errors.java:315) @ org.glassfish.jersey.internal.errors.process(errors.java:297) @ org.glassfish.jersey.internal.errors.process(errors.java:267) @ org.glassfish.jersey.process.internal.requestscope.runinscope(requestscope.java:297) @ org.glassfish.jersey.server.serverruntime.process(serverruntime.java:254) @ org.glassfish.jersey.server.applicationhandler.handle(applicationhandler.java:1030) @ org.glassfish.jersey.servlet.webcomponent.service(webcomponent.java:373) @ org.glassfish.jersey.servlet.servletcontainer.service(servletcontainer.java:381) @ org.glassfish.jersey.servlet.servletcontainer.service(servletcontainer.java:344) @ org.glassfish.jersey.servlet.servletcontainer.service(servletcontainer.java:221) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:291) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:219) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:617) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:518) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1091) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:668) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1521) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1478) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(unknown source) ***
Comments
Post a Comment