我已经尝试过Swizzle Stream库来替换输入流中的标记。
String RESOURCE_PATH = "FakePom.xml";
InputStream pomIS = JarFinderServlet.class.getClassLoader().getResourceAsStream( RESOURCE_PATH );
if( null == pomIS )
throw new MavenhoeException("Can't read fake pom template - getResourceAsStream( RESOURCE_PATH ) == null");
Map map = ArrayUtils.toMap( new String[][]{
{"@GRP@", artifactInfo.getGroup() },
{"@ART@", artifactInfo.getName() },
{"@VER@", artifactInfo.getVersion() },
{"@PACK@", artifactInfo.getPackaging() },
{"@NAME@", artifactInfo.getFileName() },
{"@DESC@", req.getQueryString() },
} );
// This does not replace anything, no idea why. //
ReplaceStringsInputStream replacingIS = new ReplaceStringsInputStream(pomIS, map);
ReplaceStringInputStream replacingIS2 = new ReplaceStringInputStream(pomIS, "@VER@", "0.0-AAAAA");
ReplaceStringInputStream replacingIS3 = new ReplaceStringInputStream(pomIS, "@", "#");
ServletOutputStream os = resp.getOutputStream();
IOUtils.copy( replacingIS, os );
replacingIS.close();
这不起作用。它只是没有取代。所以我采用了“PHP方式”......
String pomTemplate = IOUtils.toString(pomIS)
.replace("@GRP@", artifactInfo.getGroup() )
.replace("@ART@", artifactInfo.getName() )
.replace("@VER@", artifactInfo.getVersion() )
.replace("@PACK@", artifactInfo.getPackaging() )
.replace("@NAME@", artifactInfo.getFileName() )
.replace("@DESC@", req.getQueryString() );
ServletOutputStream os = resp.getOutputStream();
IOUtils.copy( new StringInputStream(pomTemplate), os );
os.close();
作品。
怎么了?
答案 0 :(得分:3)
IOUtils.copy调用read(byte [])方法而不是read(),它被FixedTokenReplacementInputStream(一个ReplaceStringInputStream的超类)覆盖。 您应该自己实现复制,例如,如下所示:
try {
int b;
while ((b = pomIS.read()) != -1) {
os.write(b);
}} finally { os.flush();os.close(); }