java - getResourceAsStream returns NullpointerException -
this question has answer here:
in spring, want image display on browser. image located in main project i.e.
> myproject - src - target - img.png this means img.png in root dont supply path returns nullpointerexception. tried adding image in src , changed path src\img.png still gave npe.
please note, project using maven.
stack trace:
java.lang.nullpointerexception: null @ org.apache.commons.io.ioutils.copylarge(ioutils.java:1792) ~[commons-io-2.4.jar:2.4] @ org.apache.commons.io.ioutils.copylarge(ioutils.java:1769) ~[commons-io-2.4.jar:2.4] @ org.apache.commons.io.ioutils.copy(ioutils.java:1744) ~[commons-io-2.4.jar:2.4] @ org.apache.commons.io.ioutils.tobytearray(ioutils.java:462) ~[commons-io-2.4.jar:2.4] here code use:
inputstream in = dataservice.class.getresourceasstream("img.png"); final httpheaders headers = new httpheaders(); headers.setcontenttype(mediatype.image_png); result.setdata(new responseentity<byte[]>(ioutils.tobytearray(in), headers, httpstatus.created)); comments on question being marked closure:
- the question related programming api getresourceasstream says this:
returns url object or null if no resource name found
my code returning null when tried adding img.png different locations.
- this question not duplicate of this: getresourceasstream() returning null
the answer in question says file must in same directory this in question, though added image same location class dataservice was, did not work.
the issue here /src root of classpath (it depends on how compile project), , should put img.png src folder , following code work
dataservice.class.getresourceasstream("img.png");
generally getresourceasstream() looks files root of classpath.
Comments
Post a Comment