简体   繁体   English

将图像添加到Eclipse可运行jar并将其插入HTML文件

[英]add image to Eclipse runnable jar + insert it into HTML file

I'm trying to insert an image into a HTML file from a java method with Eclipse. 我正在尝试使用Eclipse的java方法将图像插入HTML文件。 When I export the project into a Runnable jar, the image doesn't appear. 当我将项目导出到Runnable jar时,图像没有出现。

I searched a lot on the internet, and i did a ressource folder, but it doesn't work. 我在互联网上搜索了很多内容,并做了一个资源文件夹,但是它不起作用。

src SRC
--test4 --test4
----IHM2.java ----IHM2.java
----test4.java ----test4.java
--res --res
----logo2.png ---- logo2.png

public class IHM2
{
private File htmlFile;
private FileWriter fw;
private BufferedWriter writer;

public IHM2() throws IOException {
    htmlFile = new File("IHM2.html");
    fw = new FileWriter("IHM2.html");
    writer = new BufferedWriter(fw);
}

public void HTML_opening() throws IOException
{

    writer.write("<html>");
    writer.write("<head>");
    writer.write("</head>");
    writer.write("<body>");
    writer.write("  <img src='res/logo2.png'>");

}

public void HTML_closing() throws IOException
{
    writer.write("</body>");
    writer.write("</html>");
    writer.close();
    Desktop.getDesktop().browse(htmlFile.toURI()); 
}
}

Any one please provide me an answer ? 有人可以给我一个答案吗?

SOLUTION : I put writer.write(" <img src='./logo2.png'>"); 解决方案:我把writer.write(" <img src='./logo2.png'>"); Because I looked into the .jar file and logo2.png was out of the folder test4. 因为我查看了.jar文件,所以logo2.png不在文件夹test4中。

Check inside the jar, do you have logo2.png ? 检查罐子内部,您是否有logo2.png

If not, while exporting the jar from eclipse as runable jar, use the radio button "package required libraries into generated jar". 如果不是,从eclipse中将jar导出为可运行jar时,请使用单选按钮“将所需的库打包到生成的jar中”。 If the res folder is a source folder then the png image will be copied into the root. 如果res文件夹是源文件夹,则png图像将被复制到根目录中。

so instead of res/logo2.png you simply need to use logo2.png 因此,您只需使用logo2.png即可代替res/logo2.png logo2.png

Perhaps the best way to port your html with your jar file is to convert it into base64 data. 可能,用jar文件移植html的最好方法是将其转换为base64数据。

You can get your base64 of your image from the following link: 您可以从以下链接获取图像的base64:

http://beta.base64-image.de/ http://beta.base64-image.de/

Follow the instruction and get your image code. 按照说明进行操作并获取图像代码。 Once you uploaded your image, get the code and use it in your src="" tag. 上载图片后,获取代码并在src =“”标记中使用它。

Or else if you want to show the html, you must have the image next to it. 否则,如果要显示html,则必须在其旁边有图片。

For more information you can check: Embedding Base64 Images 有关更多信息,您可以检查: 嵌入Base64图像

You can also find ways to convert your image directly from java using: 您还可以找到使用以下方法直接从Java转换图像的方法:

Java - Convert image to Base64 Java-将图像转换为Base64

Finally, if you are interested to use fines in your resources, in somewhere else than your jar, you must save them into your hard disk, so they will be accessible by other apps, and then you can address it to open with any program. 最后,如果您有兴趣在jar以外的其他地方使用资源中的罚款,则必须将其保存到硬盘中,以便其他应用程序可以访问它们,然后可以使用任何程序打开它。

Check this: 检查一下:

How to correctly get image from 'Resources' folder in NetBeans 如何从NetBeans的“资源”文件夹中正确获取图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM