java - URL.openStream throws an error -
please me resolve issue, trying write code generate pdf using apache fop. have developed xsl-fo, in resource folder of application i.e src/main/resource. when testing through junit working fine, when try application getting issue.
java.io.filenotfoundexception: c:\users\abc\development\eclipse\eclipse\file:\c:\users\abc\development\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\dmu-portal-ui\web-inf\lib\xyzservices-1.0.7-snapshot.jar!\xsltemplate\templatesummary.xsl (the filename, directory name or volume label syntax incorrect) @ java.io.fileinputstream.open(native method) @ java.io.fileinputstream.<init>(fileinputstream.java:146) @ java.io.fileinputstream.<init>(fileinputstream.java:101) @ sun.net.www.protocol.file.fileurlconnection.connect(fileurlconnection.java:90) @ sun.net.www.protocol.file.fileurlconnection.getinputstream(fileurlconnection.java:188) @ java.net.url.openstream(url.java:1037) code
public string createpdffile(bytearrayoutputstream xmlsource, string templatefile) throws ioexception { file file = file.createtempfile("casesummary-" + system.currenttimemillis(), extension); url url = new file(this.getclass().getresource("/" + templatefile).getpath()).touri().tourl(); //url url = new file(templatefile).touri().tourl(); //url url = new url(templatefile); // creation of transform source //streamsource transformsource = new streamsource(getclass().getresourceasstream("/" + templatefile)); streamsource transformsource = new streamsource(url.openstream()); // create instance of fop factory fopfactory fopfactory = fopfactory.newinstance(); // user agent needed transformation fouseragent fouseragent = fopfactory.newfouseragent(); // store output bytearrayoutputstream pdfoutstream = new bytearrayoutputstream(); streamsource source = new streamsource(new bytearrayinputstream(xmlsource.tobytearray())); transformer xslfotransformer; try { transformerfactory transfact = transformerfactory.newinstance(); xslfotransformer = transfact.newtransformer(transformsource); // construct fop desired output format fop fop; try { fop = fopfactory.newfop(mimeconstants.mime_pdf, fouseragent, pdfoutstream); // resulting sax events (the generated fo) // must piped through fop result res = new saxresult(fop.getdefaulthandler()); // start xslt transformation , fop processing try { // happen here.. xslfotransformer.transform(source, res); // if want save pdf file use following code outputstream out = new java.io.fileoutputstream(file); out = new java.io.bufferedoutputstream(out); fileoutputstream str = new fileoutputstream(file); str.write(pdfoutstream.tobytearray()); str.close(); out.close(); } catch (transformerexception e) { e.printstacktrace(); } } catch (fopexception e) { e.printstacktrace(); } } catch (transformerconfigurationexception e) { e.printstacktrace(); } catch (transformerfactoryconfigurationerror e) { e.printstacktrace(); } return file.getpath(); } problem see @ url.openstream when trying application, path duplicated, when accessed junit working good.
technology stack jdk : 1.7 spring : 4.1.5
please me resolve issue
first, jb nizet stated, should not use url or file reading template. don't need either of those; need read packaged resource inputstream:
try (inputstream xslstream = getclass().getresourceasstream("/" + templatefile)) { streamsource transformsource = new streamsource(xslstream); second, class.getresource , class.getresourceasstream methods (and classloader equivalents) require a string argument containing forward slashes (/), on platforms, windows.
so either change calling code passes "xsltemplate/summary.xsl", or put @ beginning of method:
templatefile = templatefile.replace('\\', '/');
Comments
Post a Comment