tomcat重启会重新解压war包吗_tomcat解压版

tomcat重启会重新解压war包吗_tomcat解压版tomcat解压war包的一点例外

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用
我在项目的开发过程中,发现Tomcat解压war 的一点例外。
   
   现象如下:

       使用ANT工具把web应用程序打为war文件。然后把war文件放到tomcat的webapps,让tomcat自己解压。结果出现解压的web应用程序文件丢失。使用rar工具打开war文件。文件都齐全。怎么有这种现象呢??查看tomcat的log文档。发现在解压war文档NullpointException.我升级tomcat到5.0还是现这种现象。

jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/HostConfig.java

   解决方法:

       我从tomcat网站下载了catalina 的原代码,进行分析。发现是在解压war文件现input为null,而 input= jar.getInputStream(entry);然后提高tomcat的debug级别。可以在tomcat的log文档看到tomcat解压war文档的过程。发现如果某些文件名为???.txt,经检查发现原来这个文件的文件名为汉字。
      噢!才发现war文件在解压的过程中无法处理汉字的文件名。(因为找不到文件名为???.txt的文件而导致null例外。原来这个文件是个注释文档),所以在使用ant把web应用程序打为war文件,一定要把文件名为汉字的文件去掉。使用Forte for java的IDE工具把web应用程序打为war文件会不含这些文件名为汉字的文件


下面是部分war文档解压的部分代码

代码采自jakarta.org 



 类HostConfig.java

jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/HostConfig.java



   解决方法:

       我从tomcat网站下载了catalina 的原代码,进行分析。发现是在解压war文件现input为null,而 input= jar.getInputStream(entry);然后提高tomcat的debug级别。可以在tomcat的log文档看到tomcat解压war文档的过程。发现如果某些文件名为???.txt,经检查发现原来这个文件的文件名为汉字。
      噢!才发现war文件在解压的过程中无法处理汉字的文件名。(因为找不到文件名为???.txt的文件而导致null例外。原来这个文件是个注释文档),所以在使用ant把web应用程序打为war文件,一定要把文件名为汉字的文件去掉。使用Forte for java的IDE工具把web应用程序打为war文件会不含这些文件名为汉字的文件


下面是部分war文档解压的部分代码

代码采自jakarta.org 



 类HostConfig.java

jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/HostConfig.java


  1.  protected void deployWARs(File appBase, String[] files) {
  2.         for (int i = 0; i < files.length; i++) {
  3.             if (files[i].equalsIgnoreCase(“META-INF”))
  4.                 continue;
  5.             if (files[i].equalsIgnoreCase(WEB-INF”))
  6.                 continue;
  7.             if (deployed.contains(files[i]))
  8.                 continue;
  9.             File dir = new File(appBase, files[i]);
  10.             if (files[i].toLowerCase().endsWith(“.war”)) {
  11.                 deployed.add(files[i]);
  12.                 // Calculate the context path and make sure it is unique
  13.                 String contextPath = “/” + files[i];
  14.                 int period = contextPath.lastIndexOf(“.”);
  15.                 if (period >= 0)
  16.                     contextPath = contextPath.substring(0, period);
  17.                 if (contextPath.equals(“/ROOT”))
  18.                     contextPath = “”;
  19.                 if (host.findChild(contextPath) != null)
  20.                     continue;
  21.                 // Checking for a nested /META-INF/context.xml
  22.                 JarFile jar = null;
  23.                 JarEntry entry = null;
  24.                 InputStream istream = null;
  25.                 BufferedOutputStream ostream = null;
  26.                 File xml = new File
  27.                     (configBase, files[i].substring
  28.                      (0, files[i].lastIndexOf(“.”)) + “.xml”);
  29.                 if (!xml.exists()) {
  30.                     try {
  31.                         jar = new JarFile(dir);
  32.                         entry = jar.getJarEntry(“META-INF/context.xml”);
  33.                         if (entry != null) {
  34.                             istream = jar.getInputStream(entry);
  35.                             ostream =
  36.                                 new BufferedOutputStream
  37.                                 (new FileOutputStream(xml), 1024);
  38.                             byte buffer[] = new byte[1024];
  39.                             while (true) {
  40.                                 int n = istream.read(buffer);
  41.                                 if (n < 0) {
  42.                                     break;
  43.                                 }
  44.                                 ostream.write(buffer, 0, n);
  45.                             }
  46.                             ostream.flush();
  47.                             ostream.close();
  48.                             ostream = null;
  49.                             istream.close();
  50.                             istream = null;
  51.                             entry = null;
  52.                             jar.close();
  53.                             jar = null;
  54.                             deployDescriptors(configBase(), configBase.list());
  55.                             return;
  56.                         }
  57.                     } catch (Exception e) {
  58.                         // Ignore and continue
  59.                         if (ostream != null) {
  60.                             try {
  61.                                 ostream.close();
  62.                             } catch (Throwable t) {
  63.                                 ;
  64.                             }
  65.                             ostream = null;
  66.                         }
  67.                         if (istream != null) {
  68.                             try {
  69.                                 istream.close();
  70.                             } catch (Throwable t) {
  71.                                 ;
  72.                             }
  73.                             istream = null;
  74.                         }
  75.                         entry = null;
  76.                         if (jar != null) {
  77.                             try {
  78.                                 jar.close();
  79.                             } catch (Throwable t) {
  80.                                 ;
  81.                             }
  82.                             jar = null;
  83.                         }
  84.                     }
  85.                 }
  86.                 if (isUnpackWARs()) {
  87.                     // Expand and deploy this application as a directory
  88.                     log.debug(sm.getString(“hostConfig.expand”, files[i]));
  89.                     URL url = null;
  90.                     String path = null;
  91.                     try {
  92.                         url = new URL(“jar:file:” +
  93.                                       dir.getCanonicalPath() + “!/”);
  94.                         path = ExpandWar.expand(host, url);
  95.                     } catch (IOException e) {
  96.                         // JAR decompression failure
  97.                         log.warn(sm.getString
  98.                                  (“hostConfig.expand.error”, files[i]));
  99.                         continue;
  100.                     } catch (Throwable t) {
  101.                         log.error(sm.getString
  102.                                   (“hostConfig.expand.error”, files[i]), t);
  103.                         continue;
  104.                     }
  105.                     try {
  106.                         if (path != null) {
  107.                             url = new URL(“file:” + path);
  108.                             ((Deployer) host).install(contextPath, url);
  109.                         }
  110.                     } catch (Throwable t) {
  111.                         log.error(sm.getString
  112.                                   (“hostConfig.expand.error”, files[i]), t);
  113.                     }
  114.                 } else {
  115.                     // Deploy the application in this WAR file
  116.                     log.info(sm.getString(“hostConfig.deployJar”, files[i]));
  117.                     try {
  118.                         URL url = new URL(“file”null,
  119.                                           dir.getCanonicalPath());
  120.                         url = new URL(“jar:” + url.toString() + “!/”);
  121.                         ((Deployer) host).install(contextPath, url);
  122.                     } catch (Throwable t) {
  123.                         log.error(sm.getString(“hostConfig.deployJar.error”,
  124.                                          files[i]), t);
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.     }


类 ExpandWar.java
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/ExpandWar.java

  1. package org.apache.catalina.startup;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.IOException;
  7. import java.net.JarURLConnection;
  8. import java.net.URL;
  9. import java.util.Enumeration;
  10. import java.util.jar.JarEntry;
  11. import java.util.jar.JarFile;
  12. import org.apache.catalina.Host;
  13. import org.apache.catalina.Logger;
  14. import org.apache.catalina.core.StandardHost;
  15. import org.apache.catalina.util.StringManager;
  16. /**
  17.  * Expand out a WAR in a Host‘s appBase.
  18.  *
  19.  * @author Craig R. McClanahan
  20.  * @author Remy Maucherat
  21.  * @author Glenn L. Nielsen
  22.  * @version $Revision: 1.4 $
  23.  */
  24. public class ExpandWar {
  25.     /**
  26.      * The string resources for this package.
  27.      */
  28.     protected static final StringManager sm =
  29.         StringManager.getManager(Constants.Package);
  30.     /**
  31.      * Expand the WAR file found at the specified URL into an unpacked
  32.      * directory structure, and return the absolute pathname to the expanded
  33.      * directory.
  34.      *
  35.      * @param host Host war is being installed for
  36.      * @param war URL of the web application archive to be expanded
  37.      *  (must start with “jar:”)
  38.      *
  39.      * @exception IllegalArgumentException if this is not a “jar:” URL
  40.      * @exception IOException if an input/output error was encountered
  41.      *  during expansion
  42.      */
  43.     public static String expand(Host host, URL war)
  44.         throws IOException {
  45.         int debug = 0;
  46.         Logger logger = host.getLogger();
  47.         if (host instanceof StandardHost) {
  48.             debug = ((StandardHost) host).getDebug();
  49.         }
  50.         // Calculate the directory name of the expanded directory
  51.         if (debug >= 1) {
  52.             logger.log(“expand(“ + war.toString() + “)”);
  53.         }
  54.         String pathname = war.toString().replace(‘\\‘, ‘/‘);
  55.         if (pathname.endsWith(“!/”)) {
  56.             pathname = pathname.substring(0, pathname.length() – 2);
  57.         }
  58.         int period = pathname.lastIndexOf(‘.‘);
  59.         if (period >= pathname.length() – 4)
  60.             pathname = pathname.substring(0, period);
  61.         int slash = pathname.lastIndexOf(‘/‘);
  62.         if (slash >= 0) {
  63.             pathname = pathname.substring(slash + 1);
  64.         }
  65.         if (debug >= 1) {
  66.             logger.log(”  Proposed directory name: “ + pathname);
  67.         }
  68.         return expand(host, war, pathname);
  69.     }
  70.     /**
  71.      * Expand the WAR file found at the specified URL into an unpacked
  72.      * directory structure, and return the absolute pathname to the expanded
  73.      * directory.
  74.      *
  75.      * @param host Host war is being installed for
  76.      * @param war URL of the web application archive to be expanded
  77.      *  (must start with “jar:”)
  78.      * @param pathname Context path name for web application
  79.      *
  80.      * @exception IllegalArgumentException if this is not a “jar:” URL
  81.      * @exception IOException if an input/output error was encountered
  82.      *  during expansion
  83.      */
  84.     public static String expand(Host host, URL war, String pathname)
  85.         throws IOException {
  86.         int debug = 0;
  87.         Logger logger = host.getLogger();
  88.         if (host instanceof StandardHost) {
  89.             debug = ((StandardHost) host).getDebug();
  90.         }
  91.         // Make sure that there is no such directory already existing
  92.         File appBase = new File(host.getAppBase());
  93.         if (!appBase.isAbsolute()) {
  94.             appBase = new File(System.getProperty(“catalina.base”),
  95.                                host.getAppBase());
  96.         }
  97.         if (!appBase.exists() || !appBase.isDirectory()) {
  98.             throw new IOException
  99.                 (sm.getString(“hostConfig.appBase”,
  100.                               appBase.getAbsolutePath()));
  101.         }
  102.         File docBase = new File(appBase, pathname);
  103.         if (docBase.exists()) {
  104.             // War file is already installed
  105.             return (docBase.getAbsolutePath());
  106.         }
  107.         // Create the new document base directory
  108.         docBase.mkdir();
  109.         if (debug >= 2) {
  110.             logger.log(”  Have created expansion directory “ +
  111.                 docBase.getAbsolutePath());
  112.         }
  113.         // Expand the WAR into the new document base directory
  114.         JarURLConnection juc = (JarURLConnection) war.openConnection();
  115.         juc.setUseCaches(false);
  116.         JarFile jarFile = null;
  117.         InputStream input = null;
  118.         try {
  119.             jarFile = juc.getJarFile();
  120.             if (debug >= 2) {
  121.                 logger.log(”  Have opened JAR file successfully”);
  122.             }
  123.             Enumeration jarEntries = jarFile.entries();
  124.             if (debug >= 2) {
  125.                 logger.log(”  Have retrieved entries enumeration”);
  126.             }
  127.             while (jarEntries.hasMoreElements()) {
  128.                 JarEntry jarEntry = (JarEntry) jarEntries.nextElement();
  129.                 String name = jarEntry.getName();
  130.                 if (debug >= 2) {
  131.                     logger.log(”  Am processing entry “ + name);
  132.                 }
  133.                 int last = name.lastIndexOf(‘/‘);
  134.                 if (last >= 0) {
  135.                     File parent = new File(docBase,
  136.                                            name.substring(0, last));
  137.                     if (debug >= 2) {
  138.                         logger.log(”  Creating parent directory “ + parent);
  139.                     }
  140.                     parent.mkdirs();
  141.                 }
  142.                 if (name.endsWith(“/”)) {
  143.                     continue;
  144.                 }
  145.                 if (debug >= 2) {
  146.                     logger.log(”  Creating expanded file “ + name);
  147.                 }
  148.                 input = jarFile.getInputStream(jarEntry);
  149.                 expand(input, docBase, name);
  150.                 input.close();
  151.                 input = null;
  152.             }
  153.         } catch (IOException e) {
  154.             // If something went wrong, delete expanded dir to keep things 
  155.             // clean
  156.             deleteDir(docBase);
  157.             throw e;
  158.         } finally {
  159.             if (input != null) {
  160.                 try {
  161.                     input.close();
  162.                 } catch (Throwable t) {
  163.                     ;
  164.                 }
  165.                 input = null;
  166.             }
  167.             if (jarFile != null) {
  168.                 try {
  169.                     jarFile.close();
  170.                 } catch (Throwable t) {
  171.                     ;
  172.                 }
  173.                 jarFile = null;
  174.             }
  175.         }
  176.         // Return the absolute path to our new document base directory
  177.         return (docBase.getAbsolutePath());
  178.     }
  179.     /**
  180.      * Delete the specified directory, including all of its contents and
  181.      * subdirectories recursively.
  182.      *
  183.      * @param dir File object representing the directory to be deleted
  184.      */
  185.     public static void deleteDir(File dir) {
  186.         String files[] = dir.list();
  187.         if (files == null) {
  188.             files = new String[0];
  189.         }
  190.         for (int i = 0; i < files.length; i++) {
  191.             File file = new File(dir, files[i]);
  192.             if (file.isDirectory()) {
  193.                 deleteDir(file);
  194.             } else {
  195.                 file.delete();
  196.             }
  197.         }
  198.         dir.delete();
  199.     }
  200.     /**
  201.      * Expand the specified input stream into the specified directory, creating
  202.      * a file named from the specified relative path.
  203.      *
  204.      * @param input InputStream to be copied
  205.      * @param docBase Document base directory into which we are expanding
  206.      * @param name Relative pathname of the file to be created
  207.      *
  208.      * @exception IOException if an input/output error occurs
  209.      */
  210.     protected static void expand(InputStream input, File docBase, String name)
  211.         throws IOException {
  212.         File file = new File(docBase, name);
  213.         BufferedOutputStream output = null;
  214.         try {
  215.             output = 
  216.                 new BufferedOutputStream(new FileOutputStream(file));
  217.             byte buffer[] = new byte[2048];
  218.             while (true) {
  219.                 [b]int n = input.read(buffer);[/b]               
  220.                 if (n <= 0)
  221.                     break;
  222.                 output.write(buffer, 0, n);
  223.             }
  224.         } finally {
  225.             if (output != null) {
  226.                 try {
  227.                     output.close();
  228.                 } catch (IOException e) {
  229.                     // Ignore
  230.                 }
  231.             }
  232.         }
  233.     }
  234. }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/188775.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 软件工程实验报告:图书管理系统

    软件工程实验报告:图书管理系统一、课程设计的目的与要求课程设计目的软件工程课程设计是学习软件工程课程后所进行的实践环节,目的是培养学生用工程化的思想和标准文档化的思想进行软件开发。本次课程设计通过开发一个小型实用的软件系统,亲身体验软件生命周期中的各个环节,以加深对软件工程课程的深入理解、锻炼独立分析、解决问题的能力。课程设计要求2.1课程设计准备1)复习软件工程课程的主要内容,熟练掌握软件生命周期的理论以及各阶段的基本概念。2)明确可行性分析、需求分析、设计、测试等阶段的基本任务和基本方法。3)熟练运用规范化的描述

    2022年8月22日
    8
  • plsql直接连接远程数据库_plsql远程连接oracle

    plsql直接连接远程数据库_plsql远程连接oracle前言每次安装Oracle以后,都会出现使用plsql连接不上的问题!多次重启电脑、重装系统的磨人经历之后,终于做出这么一篇文章,希望能帮助广大技术人员减少一些时间,顺利进行连接。注:也可以用plsql连接远程数据库(只要有oracle的network\admin\tnsnames.ora就行)。首先下载64位oracle以及32位轻量级客户端(注意版本的对应,我用的是11g的oracl……

    2022年10月20日
    0
  • 基于H5的移动端APP开发框架

    基于H5的移动端APP开发框架快速增长的APP应用软件市场,以及智能手机的普及,手机应用:Native(原生)APP快速占领了APP市场,成为了APP开发的主流,但其平台的不通用性,开发成本高,多版本开发等问题,一直困扰着专业APP开发企业,和APP服务提供商。安卓和IOS的操作方式,开发模式,界面UI显示方面的差别,也使得原生APP的不同版本体验有很大的区别,光是做兼容性调测,都要花费开发企业不少的时间。近年来,…

    2022年6月16日
    26
  • 【超详细】计算机组成原理总结及思维导图[通俗易懂]

    计算机组成第一章计算机系统概论冯诺依曼型计算机特点1.计算机由运算器,控制器,存储器,输入和输出设备5部分组成2.采用存储程序的方式,程序和数据放在同一个存储器中,并以二进制表示。3.指令由操作码和地址码组成4.指令在存储器中按执行顺序存放,由指令计数器(即程序计数器PC)指明要执行的指令所在的储存单元地址,一般按顺序递增,但可按运算结果或外界条件而改变5.机器以运…

    2022年4月17日
    64
  • 一般人到底要不要学Python_Python值得学吗

    一般人到底要不要学Python_Python值得学吗前言本人纯屌丝一枚,在学python之前对电脑的认知也就只限于上个网,玩个办公软件。这里不能跑题,我为啥说自学python,一般人我还是劝你算了吧。因为我就是那个一般人。基础真的很简单,是个人稍微认点真都能懂,这就是好多人说的python简单、易懂、好学,然后就是一顿浮夸的言论,误导那些小白,再然后那些小白也就跟着浮夸。这里我就给那些轻浮的人泼一桶冷水,懂跟学会是一码事吗?先来说哈python这个就业哈,我现在生活在祖国的肚皮上–成都,(嗯,有想了解川西迷你小环线的在下面留言哦),下面亲身经历,我喃,

    2022年9月3日
    2
  • android 设备usb调试模式,如何打开Android设备的USB调试模式「建议收藏」

    android 设备usb调试模式,如何打开Android设备的USB调试模式「建议收藏」目前大部分刷机等工具都是采用Adb方式连接,所以需要您在设备上打开USB调试模式Android设备打开USB调试模式总共有三种不同方法,根据系统版本而不同,以下为Android所有系统版本对应的打开方法壹2.1~2.3.7系统打开方法:点击手机Menu键(菜单键),在弹出的菜单中选择设置(Setting),或在应用程序中找到设置程序点击进入进入设置界面的应用程序即可打开…

    2022年9月13日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号