访问jar文件中的文件 [英] Accessing files inside a jar file

查看:47
本文介绍了访问jar文件中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用Java编程一个简单的Web服务器,尽管它可以在Eclipse中运行,但是我无法将其导出到jar中.我正在尝试将一些文件与二进制文件一起打包,并且我知道我必须使用InputStream从jar中读取内容,但是每次它似乎正常工作时,我都会从chrome访问它,并说:空响应".这可能意味着该jar无法读取自身,因此我决定从头开始,但实际上并不知道该怎么做.

I've been programming a simple webserver in Java, and though it works in Eclipse, I can't export it to a jar. I'm trying to package some files along with the binaries, and I know that I have to use InputStream to read from within the jar, but every time it seems to work, I access it from chrome and it says: "empty response". That probably means that the jar can't read itself, so I've decided to start over but don't really know how.

这是有问题的类的代码:

Here's the code for the problematic class:

    package handlers;

import java.io.*;
import com.sun.net.httpserver.*;

public final class Greeter implements HttpHandler {

    package handlers;

    import java.io.*;
    import com.sun.net.httpserver.*;

    public final class Greeter implements HttpHandler {

        static Headers h;
        static File file;
        static byte [] bytes;
        static FileInputStream fis;
        static BufferedInputStream bis;
        static OutputStream os;

        public void handle(HttpExchange t) throws IOException {

            h = t.getResponseHeaders();
            os = t.getResponseBody();
            file = new File("GreetingText.html");
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            bytes = new byte[(int) file.length()];

            h.add("Content-Type", "text/html");
            bis.read(bytes, 0, bytes.length);
            t.sendResponseHeaders(200, file.length());
            os.write(bytes, 0, bytes.length);
            os.close();

        }

    }

这不是重复的,因为我已经尝试了其他类似帖子中给出的解决方案.请注意,没有资源的罐子可以完美地工作,但不能.

this is not a duplicate, since i've already tried the solutions given in other, similar posts. Note that a jar without the resources works perfectly, but with, it doesn't.

推荐答案

这是因为文件位于JAR 内部,并且无法在文件系统上访问-使用 FileInputStream 仍在使用文件.

That's because the file is inside the JAR and is no longer accessible on the file system - using a FileInputStream is still using files.

您需要将其视为资源,并从中获取 InputStream ,然后它既可以打包为JAR,也可以打包为JAR.

You need to treat it as a resource instead and get an InputStream from that, then it will work both when packaged as a JAR and when not.

h = t.getResponseHeaders();
os = t.getResponseBody();

// Get an URL to the file
URL url = getClass().getResource("GreetingText.html");

// Open the stream and read the contents into a byte array
byte[] bytes;
try(InputStream in = url.openStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream()) {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) > 0) {
        bytes.write(buffer, 0, read);
    }
    bytes = out.toByteArray();
}

h.add("Content-Type", "text/html");
t.sendResponseHeaders(200, bytes.length);
os.write(bytes, 0, bytes.length);
os.close();

将文件放在您的类路径中,如果在另一个包中,则需要在路径前加上/path/to/file 前缀,因此,如果将其放置在目录 html 在类路径的根目录下,您将必须使用"/html/GreetingText.html" .

Place the file on your classpath, if it's in another package you need to prefix the path with /path/to/file, so if you put it in the directory html at the root of your classpath you will have to use "/html/GreetingText.html".

这篇关于访问jar文件中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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