gpt4 book ai didi

java - 在 Java 中使用 HTMLEditorKit,如何找到 标签将使用的本地文件路径?

转载 作者:行者123 更新时间:2023-12-01 17:20:06 25 4
gpt4 key购买 nike

我正在运行一个 Java 应用程序,并且正在使用 HTMLDocument/HTMLEditorKit 功能来构建我的聊天室。到目前为止,在构建样式表、插入文本消息等方面取得了很大的成功。

所以现在我来了......图像!我如果我添加标签,例如:

<img src="myimage.png"> 

...当我运行 java 应用程序时,我的文件只需位于与该应用程序相同的目录中。但我已经经历了各种各样的事情并尝试了子目录,但它似乎找不到我的本地镜像。 (它可以通过“http://etcetcetc”从网络上找到它们)。

我搜索这些内容的所有文档主要讨论“您的 html 文件所在的目录”,当然我并没有“真正”有 html 文件,只是一个虚拟文件。

有没有办法询问它认为正在从哪个目录读取?

我尝试将文件放入以下目录中:

System.getProperty("user.dir");

...但没有快乐。

在这里寻找某种相对路径,以便最终与应用程序一起安装在用户计算机上的图像文件能够显示。

[编辑:哎呀,修复了我的 HTML 示例]

最佳答案

好吧,我发现(从其他问题的一些与切线相关的答案中收集到的花絮)是我需要创建一个 URL 项,然后通过 ClassLoader 或类似的方法填充它。

例如,一个人得到了他的东西:

String filename = getClass().getClassLoader().getResource("res/Kappa.png").toString();
String preTag="<PRE>filename is : "+filename+"</PRE>";
String imageTag="<img src=\""+filename+"\"/>";
kit.insertHTML(doc, doc.getLength(), preTag+imageTag, 0, 0, HTML.Tag.IMG);

同时,根据这个提示,最终让我的东西运行“最好”的方法是:

    URL url;
String keystring = "<img src=\"";
String file, tag, replace;
int base;
while (s.toLowerCase().contains(keystring)) { // Find next key (to-lower so we're not case sensitive)
//There are undoubtedly more efficient ways to do this parsing, but this miraculously ran perfectly the very first time I compiled it, so, you know, superstition :)
base = s.toLowerCase().indexOf(keystring);
file = s.substring(base + keystring.length(), s.length()).split("\"")[0]; // Pull the filename out from between the quotes
tag = s.substring(base, base + keystring.length()) + file + "\""; // Reconstruct the part of the tag we want to remove, leaving all attributes after the filename alone, and properly matching the upper/lowercase of the keystring

try {
url = GameModule.getGameModule().getDataArchive().getURL("images/" + file);
replace = "<img src=\"" + url.toString() + "\""; // Fully qualified URL if we are successful. The extra
// space between IMG and SRC in the processed
// version ensures we don't re-find THIS tag as we
// iterate.
} catch (IOException ex) {
replace = "<img src=\"" + file + "\""; // Or just leave in except alter just enough that we won't find
// this tag again.
}

if (s.contains(tag)) {
s = s.replaceFirst(tag, replace); // Swap in our new URL-laden tag for the old one.
} else {
break; // BR// If something went wrong in matching up the tag, don't loop forever
}
}

//BR// Insert a div of the correct style for our line of text.
try {
kit.insertHTML(doc, doc.getLength(), "\n<div class=" + style + ">" + s + "</div>", 0, 0, null);
} catch (BadLocationException ble) {
ErrorDialog.bug(ble);
} catch (IOException ex) {
ErrorDialog.bug(ex);
}
conversation.update(conversation.getGraphics()); //BR// Force graphics to update

我的包在 VASSAL(游戏平台)下运行,因此能够从我的 VASSAL 模块的数据存档(zip 文件)中提取内容对我来说是理想的选择,尽管当时我问的问题是我想要的很高兴能在任何地方找到我可以让它找到图像文件并将其从数据存档中取出是后来的延伸目标。

你可以看到我在这里的策略是让 html(可能是由另一个模块设计者而不是我生成的)在源标记中引用简单的文件名(例如 src="dice.png"),然后我去解析这些如果成功的话,将其替换为 URL 查询的结果。

如果有人对原始问题的主旨有更完整的答案(HTMLEditorKit 到底在哪里寻找本地路径? - 默认本地路径是什么?我怎样才能让它在我当前的工作目录中查找,或者应用程序运行的目录,或者任何类似的本地目录),那么请务必将其发布到此处以供后代使用,因为虽然这让我运行,但我不认为这是对原始问题的全面答案。

关于java - 在 Java 中使用 HTMLEditorKit,如何找到 <img src=...> 标签将使用的本地文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61310850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com