在Java中使用HTMLEditorKit,如何查找本地文件路径< img src = ...>标签会用吗? [英] Using HTMLEditorKit in Java, how do I find what local file path <img src=...> tags will use?

查看:220
本文介绍了在Java中使用HTMLEditorKit,如何查找本地文件路径< img src = ...>标签会用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a java app running, and I'm using the HTMLDocument/HTMLEditorKit functionality to build my chat room. So far with plenty of success as far as building my stylesheet, inserting text messages, and so forth.

所以现在我来...图像!我有思想,如果我要添加诸如以下标签:

So now I've come to... images! I had thought that if I were adding tags such as:

<img src="myimage.png"> 

...运行该文件时,我的文件仅需要与Java应用程序位于同一目录中.但是我经历了各种各样的事情并尝试了子目录,但似乎找不到我的本地图像. (它发现使用" http://etcetcetc 即可在网络上正常运行)).

... that my file then just needed to be in the same directory as the java app when I ran it. But I've run through all sorts of things and tried subdirectories, and it can't seem to find my local image. (It finds them just fine off the web with "http://etcetcetc").

我搜索这些东西的所有文档都主要谈论您的html文件所在的"目录,当然,我没有真正"拥有一个html文件,只是一个虚拟文件.

All the documentation I search for these things mostly talks about the directory "your html file is in", and of course I don't "really" have an html file, just a virtual one.

有没有办法问它想读的目录是什么?

Is there a way to ask it what directory it THINKS it's reading from?

我尝试将文件放在以下原因产生的目录中:

I tried putting the file in the directory that resulted from:

System.getProperty("user.dir");

...但是没有喜悦.

... but no joy.

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

Looking for some kind of relative-pathing here, such that image files eventually installed on a user's machine along with the app would be able to be displayed.

推荐答案

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

Okay, what I have discovered (from tidbits gleaned in a few tangentially-related answers to other questions) is that I needed to create a URL item, and then get it filled by ClassLoader or a similar method.

例如,一个人把事情弄糟了:

For example one person got his thingy going with:

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);

与此同时,从该提示中得出的方法最终使我的东西最佳"运行:

Meanwhile, drawing from this hint the method that finally got MY stuff running "the best" was:

    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文件)中提取内容,尽管当时我问的问题是很高兴能找到任何地方我可以使它找到图像文件,并将其从数据存档中删除是后来的目标.

My package runs under VASSAL (the game platform), and so it was ideal for me to be able to pull things from my VASSAL module's data archive (zip file), though at the time I asked the question I would have been happy just to find out anywhere I could make it find an image file and getting it out of the data archive was a later stretch goal.

您可以在这里看到我的策略是让html(可能由其他模块设计人员生成,而不是由我生成)具有在源代码中引用的简单文件名(例如src ="dice.png"),然后我对这些文件名进行解析如果成功,则将其替换为URL查询的结果.

You can see my strategy here is to let the html (which might be generated by another module designer, not me) have simple filenames referenced in the source tags (e.g. src="dice.png") and I go parse those out and replace them with the result of the URL query if it is successful.

如果有人对原始问题有更完整的答案(HTMLEditorKit到底在哪里寻找本地路径?-默认的本地路径是什么?如何使它在我当前的工作目录中显示,或者应用程序正在运行的目录,或者实际上是任何本地目录),然后请在此处发布以供后代使用,因为尽管这使我得以运行,但我并不认为这是对原始问题的全面解答.

If someone has a more complete answer to the thrust of the original question (where on earth does HTMLEditorKit look for local PATHS? -- what is the default local path? How can I make it look in my present working directory, or the directory the app is running from, or really anything local like that) then please do post it here for posterity, because although this got me running I don't feel like it's a comprehensive answer to the original question.

这篇关于在Java中使用HTMLEditorKit,如何查找本地文件路径&lt; img src = ...&gt;标签会用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆