简体   繁体   中英

Download file / installer jar which is part of war from tomcat server (window and linux)

I was facing problem to download installer jar from war deployed on tomcat server on window machine. The code was working on linux machine but not on window machine.

   public Response downloadPOST(@Context HttpServletRequest request) {
    long length = 0;
    byte[] data;
    try {
        WebApplicationContext context = (WebApplicationContext) WebApplicationContextProvider
                .getApplicationContext();

        String sPath = context.getServletContext()
                .getResource("/installer/installer-" + Version.VERSION + ".jar").getPath();

        java.nio.file.Path path = Paths.get(sPath);

        data = Files.readAllBytes(path);
        length = data.length;
    } catch (Exception e) {
        logger.error( e);
        throw new WebApplicationException("File Not Found !!");
    }
    StreamingOutput fileStream = new StreamingOutput() {
        @Override
        public void write(java.io.OutputStream output) throws IOException, WebApplicationException {
            try {
                output.write(data);
                output.flush();
            } catch (Exception e) {
                logger.error( e);
                throw new WebApplicationException("File Not Found !!");
            }
        }
    };
    return Response.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
            .header("content-disposition",
                    "attachment; filename = installer-" + Version.VERSION + ".jar")
            .header("Content-Length", String.valueOf(length)).build();
}

The above code was throwing NoSuchFileException due to special character and prefix '/' in file path on window machine.

java.nio.file.NoSuchFileException: /C:\Program%20Files\Apache%20Software%20Foundation\Tomcat%208.0\webapps\ROOT\installer\installer-x.x.x.jar

I have fixed this issue by using UrlDecoder and using regular expression to remove prefix '/' from url.

The code which is working for window and linux machine

    public Response downloadPOST(@Context HttpServletRequest request) {
    long length = 0;
    byte[] data;
    try {
        WebApplicationContext context = (WebApplicationContext) WebApplicationContextProvider
                .getApplicationContext();

        String sPath = context.getServletContext()
                .getResource("/installer/installer-" + AgentVersion.STRING_VERSION + ".jar").getPath();

        sPath = URLDecoder.decode(sPath, "utf-8");
        // get the right path on Windows or Linux on any drive letter
        // window : if the beginning of the string is a slash, then a
        // character, then a colon and another slash, replace it with the
        // character, the colon, and the slash (leaving the leading slash
        // off).
        sPath = sPath.replaceFirst("^/(.:/)", "$1");

        java.nio.file.Path path = Paths.get(sPath);

        data = Files.readAllBytes(path);
        length = data.length;
    } catch (Exception e) {
        logger.error(e.getClass().getSimpleName() + " at " + request.getRequestURI(), e);
        throw new WebApplicationException("File Not Found !!");
    }
    StreamingOutput fileStream = new StreamingOutput() {
        @Override
        public void write(java.io.OutputStream output) throws IOException, WebApplicationException {
            try {
                output.write(data);
                output.flush();
            } catch (Exception e) {
                logger.error(e.getClass().getSimpleName() + " at " + request.getRequestURI(), e);
                throw new WebApplicationException("File Not Found !!");
            }
        }
    };
    return Response.ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
            .header("content-disposition",
                    "attachment; filename = installer-" + Version.VERSION + ".jar")
            .header("Content-Length", String.valueOf(length)).build();
}

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