简体   繁体   中英

Accessing files inside a jar file

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.

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.

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

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" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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