1

私は、テンプレートの html ドキュメントをロードし、その上でキーワードの置換を行い、それを出力するものを書いています。テンプレートに画像が含まれている場合を除いて、これは正常に機能します。テンプレート .html を参照すると、画像は正常に表示されますが (パスは問題ないと思います)、最終出力では空のスペースとして表示されます。

テンプレート html は次のようなものです。

<html>
<body>
<img src="file://c:/temp/my-logo.png" width="50" height="50"/>
[[[some stuff I want to replace]]]
</body>
</html>

十分に単純です。このテンプレートを次のようにロードします。

public void test() {
   JEditorPane text = new JEditorPane("text/html", "default");
   HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
   HTMLDocument htmlDocument = (HTMLDocument)htmlEditorKit.createDefaultDocument();

   text.setEditorKit(htmlEditorKit);
   // read the html template into the JEditorPane's text
   text.read(new BufferedReader(new InputStreamReader(new FileInputStream(new File("path to my template html")))), htmlDocument);

   // then do some replacements
   text.setText(magicReplacements(text.getText()));

   text.repaint();
   // and then print job stuff, fire off the job, check if it worked etc...
}

テキストは正しく表示およびフォーマットされますが、画像だけが表示されることはありません。誰が何が間違っているかを見つけることができますか?

乾杯。

4

1 に答える 1

6

パスが間違っていると思います。

代わりにfile://c:/temp/my-logo.png試してみてくださいfile:/c:/temp/my-logo.png

私の例をテストするとき、私は と の両方を使用//c://c:/、最初のものは失敗しましたが、2 番目のものは機能しました。

私はこれを機能させることができました...

ここに画像の説明を入力

HTML

<html>
<body>
<img src="file:/c:/backgroundtext.png"/>
[[[some stuff I want to replace]]]
</body>
</html>

サンプルコード

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.File;
import java.io.FileReader;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTextPane {

    public static void main(String[] args) {
        new TestTextPane();
    }

    public TestTextPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                FileReader fr = null;

                try {
                    fr = new FileReader(new File("c:/Test.html"));
                    JEditorPane editor = new JEditorPane();
                    editor.setContentType("text/html");
                    editor.read(fr, "Test");

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(editor));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (Exception exp) {
                    exp.printStackTrace();
                } finally {
                    try {
                        fr.close();
                    } catch (Exception e) {
                    }
                }
            }
        });
    }
}
于 2013-02-26T04:06:16.153 に答える