问题描述
我正在使用 Plant UML API 在我的 Java 项目中生成测试的 UML 图。 API 生成图像并将其保存到文件系统中的 PNG 或 SVG 文件中。
我想知道是否有一种方法可以直接在控制台中加载图像而不将其保存到文件系统(或加载文件系统中已有的文件)。
作为参考,我正在使用 PUML 提供的文档 here 并且我正在调用类似的内容:
import net.sourceforge.plantuml.sourceStringReader;
OutputStream png = ...;
String source = "@startuml\n";
source += "Bob -> Alice : hello\n";
source += "@enduml\n";
SourceStringReader reader = new SourceStringReader(source);
// Write the first image to "png"
String desc = reader.outputimage(png).getDescription();
// Return a null string if no generation
解决方法
我想这应该可以。
public byte[] createUmlImage(String source){
try {
SourceStringReader reader = new SourceStringReader(source);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
reader.outputImage(os);
return os.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
}
我还要再提一个建议。使用 StringBuilder
的 append()
代替 +
运算符进行字符串连接。