java - NullPointerException in rest webservice upload -
i trying upload file using rest webservice. code
@path(value= "/up") public class upload { @post @path(value="upload") @consumes(mediatype.multipart_form_data) public response uploadfile(@formdataparam("file") inputstream fileinputstream ) { string filepath = "c://users/marya/desktop/file" ; system.out.println("*****serverpath********"); savefile(fileinputstream, filepath); //this line 29 system.out.println("*****save********"); string output = "file saved server location : " + filepath; system.out.println("done"); return response.status(200).entity(output).build(); } private void savefile(inputstream uploadedinputstream,string serverlocation) { try {outputstream outpustream = new fileoutputstream(new file(serverlocation)); int read = 0; byte[] bytes = new byte[1024]; outpustream = new fileoutputstream(new file(serverlocation)); while ((read = uploadedinputstream.read(bytes)) != -1) //this lione 42 { outpustream.write(bytes, 0, read); system.out.println("*****read********"); } outpustream.flush(); outpustream.close(); } catch (ioexception e) { e.printstacktrace(); } } }
when run it, file created empty size = 0 !! , error:
grave: "servlet.service()" pour la servlet jax-rs servlet généré une exception java.lang.nullpointerexception @ com.example.upload.savefile(upload.java:42) @ com.example.upload.uploadfile(upload.java:29) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ com.sun.jersey.spi.container.javamethodinvokerfactory$1.invoke(javamethodinvokerfactory.java:60) @ com.sun.jersey.server.impl.model.method.dispatch.abstractresourcemethoddispatchprovider$responseoutinvoker._dispatch(abstractresourcemethoddispatchprovider.java:205) @ com.sun.jersey.server.impl.model.method.dispatch.resourcejavamethoddispatcher.dispatch(resourcejavamethoddispatcher.java:75) @ com.sun.jersey.server.impl.uri.rules.httpmethodrule.accept(httpmethodrule.java:302) @ com.sun.jersey.server.impl.uri.rules.righthandpathrule.accept(righthandpathrule.java:147) @ com.sun.jersey.server.impl.uri.rules.resourceclassrule.accept(resourceclassrule.java:108) @ com.sun.jersey.server.impl.uri.rules.righthandpathrule.accept(righthandpathrule.java:147) @ com.sun.jersey.server.impl.uri.rules.rootresourceclassesrule.accept(rootresourceclassesrule.java:84) @ com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1542) @ com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1473) @ com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1419) @ com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1409)
are there suggestions please solve issue ??
Comments
Post a Comment