Extract some content from jar-file downloaded with JWS

余生长醉 提交于 2019-12-02 07:52:19

the logs says: url=http://localhost:8080/myjarfile.jar

That was a security decision made by Sun, before Oracle acquired them. They decided it was no business of applets or JWS apps. to know the location of the resources on the local file-system, so the URI returned will now always point back to the server, even if they are cached locally & the app. has all-permissions security level.

So any good ideas of how to proceed?

Redesign the app. That is the only practical solution.

There are a number of ways to iterate a Zip or Jar for the content, but the simplest method is to include a list of the content in a known location of the Jar, locate it using getResource(), read it, then extract each resource.

Below is an implementation of your idea Andrew, it utilizes a DirUtil package I found here: http://codingjunkie.net/java-7-copy-move/

public class Zipper {

    private static final String TEMP_FILE_PREFIX = "temp-";
    private static final String TEMP_FILE_SUFIX = ".jar";

    private Logger logger = Logger.getLogger(getClass().getName());

    public Path extractProgram(String locationOfEmbeddedJar, String installDir) {
        Path installPath = null;
        try {
            installPath = Paths.get(installDir);
            if (Files.isDirectory(installPath)) {
                logger.warn("program already installed");
            } else {
                installPath = Files.createDirectory(installPath);
                Path tempJar = Files.createTempFile(TEMP_FILE_PREFIX,
                        TEMP_FILE_SUFIX);
                this.extractEmbeddedJar(locationOfEmbeddedJar, tempJar.toFile());

                logger.warn("in jarfile");
                // in jar file
                FileSystem fs = FileSystems.newFileSystem(tempJar, null);
                Path programPath = fs.getPath("/");

                logger.warn("programPath=" + programPath + " fileSystem="
                        + programPath.getFileSystem());

                DirUtils.copy(programPath, installPath);
            }
        } catch (IOException e) {
            logger.warn(e);
        }

        return (installPath);

    }

    private void extractEmbeddedJar(String locationOfEmbeddedJar,
            File locationOfTargetJar) {
        logger.warn("extractEmbeddedJar() " + locationOfEmbeddedJar);
        ClassLoader loader = this.getClass().getClassLoader();

        InputStream inputStream = loader
                .getResourceAsStream(locationOfEmbeddedJar);
        try {
            OutputStream out = new FileOutputStream(locationOfTargetJar);
            byte buf[] = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.close();
            inputStream.close();

        } catch (IOException e) {
            logger.warn(e);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!