简体   繁体   English

在可执行jar中附加html文件,图像和索引文件

[英]attach html files ,images and index files in executable jar

Dear Stackflow Developers first of all thanks a lot for helping me a lot. 尊敬的Stackflow开发人员首先非常感谢您的帮助。 I am working on a project in java swing application in which client has to access the html files in swing GUI. 我正在Java Swing应用程序中的一个项目中,客户端必须在Swing GUI中访问html文件。 i want to add Html files ,images and index file to jar of the project so that client can access html files without storing them in actual .Can the client unzip the html files from jar? 我想将HTML文件,图像和索引文件添加到项目的jar中,以便客户端可以访问html文件而无需将它们存储在实际中。客户端可以从jar解压缩html文件吗? please suggest me the best way . 请给我建议最好的方法。 regards 问候

You can use eg 您可以使用例如

ImageIcon image = (new ImageIcon(getClass().getResource("yourpackage/mypackage/image.gif")));

In general, you can retrieve an InputStream in the following way: 通常,您可以通过以下方式检索InputStream:

InputStream is = this.getClass().getResourceAsStream("yourpackage/mypackage/file.html");

Then use the stream or URL to work with the files. 然后使用流或URL处理文件。

You can access jar resources directly with Class.getResource() . 您可以使用Class.getResource()直接访问jar资源。 This also works in your development environment where resources are copied to the "output" directory (where your *.class files are generated) 这在您将资源复制到“输出”目录(生成* .class文件的目录)的开发环境中也适用

For example, let's say you have an HTML file which is located in a jar at the path "/path/to/my/index.html", you can retrieve the URL to that file with 例如,假设您有一个HTML文件,该文件位于jar中的路径“ /path/to/my/index.html”,则可以使用以下方法检索该文件的URL:

URL indexURL = MyClass.class.getResource("/path/to/my/index.html");

Assuming that MyClass is a class of yours. 假设MyClass是您的一门课。

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

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