tomcat解压war包的一点例外
我在项目的开发过程中,发现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
- protected void deployWARs(File appBase, String[] files) {
 - for (int i = 0; i < files.length; i++) {
 - if (files[i].equalsIgnoreCase("META-INF"))
 - continue;
 - if (files[i].equalsIgnoreCase("WEB-INF"))
 - continue;
 - if (deployed.contains(files[i]))
 - continue;
 - File dir = new File(appBase, files[i]);
 - if (files[i].toLowerCase().endsWith(".war")) {
 - deployed.add(files[i]);
 - // Calculate the context path and make sure it is unique
 - String contextPath = "/" + files[i];
 - int period = contextPath.lastIndexOf(".");
 - if (period >= 0)
 - contextPath = contextPath.substring(0, period);
 - if (contextPath.equals("/ROOT"))
 - contextPath = "";
 - if (host.findChild(contextPath) != null)
 - continue;
 - // Checking for a nested /META-INF/context.xml
 - JarFile jar = null;
 - JarEntry entry = null;
 - InputStream istream = null;
 - BufferedOutputStream ostream = null;
 - File xml = new File
 - (configBase, files[i].substring
 - (0, files[i].lastIndexOf(".")) + ".xml");
 - if (!xml.exists()) {
 - try {
 - jar = new JarFile(dir);
 - entry = jar.getJarEntry("META-INF/context.xml");
 - if (entry != null) {
 - istream = jar.getInputStream(entry);
 - ostream =
 - new BufferedOutputStream
 - (new FileOutputStream(xml), 1024);
 - byte buffer[] = new byte[1024];
 - while (true) {
 - int n = istream.read(buffer);
 - if (n < 0) {
 - break;
 - }
 - ostream.write(buffer, 0, n);
 - }
 - ostream.flush();
 - ostream.close();
 - ostream = null;
 - istream.close();
 - istream = null;
 - entry = null;
 - jar.close();
 - jar = null;
 - deployDescriptors(configBase(), configBase.list());
 - return;
 - }
 - } catch (Exception e) {
 - // Ignore and continue
 - if (ostream != null) {
 - try {
 - ostream.close();
 - } catch (Throwable t) {
 - ;
 - }
 - ostream = null;
 - }
 - if (istream != null) {
 - try {
 - istream.close();
 - } catch (Throwable t) {
 - ;
 - }
 - istream = null;
 - }
 - entry = null;
 - if (jar != null) {
 - try {
 - jar.close();
 - } catch (Throwable t) {
 - ;
 - }
 - jar = null;
 - }
 - }
 - }
 - if (isUnpackWARs()) {
 - // Expand and deploy this application as a directory
 - log.debug(sm.getString("hostConfig.expand", files[i]));
 - URL url = null;
 - String path = null;
 - try {
 - url = new URL("jar:file:" +
 - dir.getCanonicalPath() + "!/");
 - path = ExpandWar.expand(host, url);
 - } catch (IOException e) {
 - // JAR decompression failure
 - log.warn(sm.getString
 - ("hostConfig.expand.error", files[i]));
 - continue;
 - } catch (Throwable t) {
 - log.error(sm.getString
 - ("hostConfig.expand.error", files[i]), t);
 - continue;
 - }
 - try {
 - if (path != null) {
 - url = new URL("file:" + path);
 - ((Deployer) host).install(contextPath, url);
 - }
 - } catch (Throwable t) {
 - log.error(sm.getString
 - ("hostConfig.expand.error", files[i]), t);
 - }
 - } else {
 - // Deploy the application in this WAR file
 - log.info(sm.getString("hostConfig.deployJar", files[i]));
 - try {
 - URL url = new URL("file", null,
 - dir.getCanonicalPath());
 - url = new URL("jar:" + url.toString() + "!/");
 - ((Deployer) host).install(contextPath, url);
 - } catch (Throwable t) {
 - log.error(sm.getString("hostConfig.deployJar.error",
 - files[i]), t);
 - }
 - }
 - }
 - }
 - }
 
类 ExpandWar.java
 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/ExpandWar.java
- package org.apache.catalina.startup;
 - import java.io.BufferedOutputStream;
 - import java.io.File;
 - import java.io.FileOutputStream;
 - import java.io.InputStream;
 - import java.io.IOException;
 - import java.net.JarURLConnection;
 - import java.net.URL;
 - import java.util.Enumeration;
 - import java.util.jar.JarEntry;
 - import java.util.jar.JarFile;
 - import org.apache.catalina.Host;
 - import org.apache.catalina.Logger;
 - import org.apache.catalina.core.StandardHost;
 - import org.apache.catalina.util.StringManager;
 - /**
 - * Expand out a WAR in a Host‘s appBase.
 - *
 - * @author Craig R. McClanahan
 - * @author Remy Maucherat
 - * @author Glenn L. Nielsen
 - * @version $Revision: 1.4 $
 - */
 - public class ExpandWar {
 - /**
 - * The string resources for this package.
 - */
 - protected static final StringManager sm =
 - StringManager.getManager(Constants.Package);
 - /**
 - * Expand the WAR file found at the specified URL into an unpacked
 - * directory structure, and return the absolute pathname to the expanded
 - * directory.
 - *
 - * @param host Host war is being installed for
 - * @param war URL of the web application archive to be expanded
 - * (must start with "jar:")
 - *
 - * @exception IllegalArgumentException if this is not a "jar:" URL
 - * @exception IOException if an input/output error was encountered
 - * during expansion
 - */
 - public static String expand(Host host, URL war)
 - throws IOException {
 - int debug = 0;
 - Logger logger = host.getLogger();
 - if (host instanceof StandardHost) {
 - debug = ((StandardHost) host).getDebug();
 - }
 - // Calculate the directory name of the expanded directory
 - if (debug >= 1) {
 - logger.log("expand(" + war.toString() + ")");
 - }
 - String pathname = war.toString().replace(‘\\‘, ‘/‘);
 - if (pathname.endsWith("!/")) {
 - pathname = pathname.substring(0, pathname.length() - 2);
 - }
 - int period = pathname.lastIndexOf(‘.‘);
 - if (period >= pathname.length() - 4)
 - pathname = pathname.substring(0, period);
 - int slash = pathname.lastIndexOf(‘/‘);
 - if (slash >= 0) {
 - pathname = pathname.substring(slash + 1);
 - }
 - if (debug >= 1) {
 - logger.log(" Proposed directory name: " + pathname);
 - }
 - return expand(host, war, pathname);
 - }
 - /**
 - * Expand the WAR file found at the specified URL into an unpacked
 - * directory structure, and return the absolute pathname to the expanded
 - * directory.
 - *
 - * @param host Host war is being installed for
 - * @param war URL of the web application archive to be expanded
 - * (must start with "jar:")
 - * @param pathname Context path name for web application
 - *
 - * @exception IllegalArgumentException if this is not a "jar:" URL
 - * @exception IOException if an input/output error was encountered
 - * during expansion
 - */
 - public static String expand(Host host, URL war, String pathname)
 - throws IOException {
 - int debug = 0;
 - Logger logger = host.getLogger();
 - if (host instanceof StandardHost) {
 - debug = ((StandardHost) host).getDebug();
 - }
 - // Make sure that there is no such directory already existing
 - File appBase = new File(host.getAppBase());
 - if (!appBase.isAbsolute()) {
 - appBase = new File(System.getProperty("catalina.base"),
 - host.getAppBase());
 - }
 - if (!appBase.exists() || !appBase.isDirectory()) {
 - throw new IOException
 - (sm.getString("hostConfig.appBase",
 - appBase.getAbsolutePath()));
 - }
 - File docBase = new File(appBase, pathname);
 - if (docBase.exists()) {
 - // War file is already installed
 - return (docBase.getAbsolutePath());
 - }
 - // Create the new document base directory
 - docBase.mkdir();
 - if (debug >= 2) {
 - logger.log(" Have created expansion directory " +
 - docBase.getAbsolutePath());
 - }
 - // Expand the WAR into the new document base directory
 - JarURLConnection juc = (JarURLConnection) war.openConnection();
 - juc.setUseCaches(false);
 - JarFile jarFile = null;
 - InputStream input = null;
 - try {
 - jarFile = juc.getJarFile();
 - if (debug >= 2) {
 - logger.log(" Have opened JAR file successfully");
 - }
 - Enumeration jarEntries = jarFile.entries();
 - if (debug >= 2) {
 - logger.log(" Have retrieved entries enumeration");
 - }
 - while (jarEntries.hasMoreElements()) {
 - JarEntry jarEntry = (JarEntry) jarEntries.nextElement();
 - String name = jarEntry.getName();
 - if (debug >= 2) {
 - logger.log(" Am processing entry " + name);
 - }
 - int last = name.lastIndexOf(‘/‘);
 - if (last >= 0) {
 - File parent = new File(docBase,
 - name.substring(0, last));
 - if (debug >= 2) {
 - logger.log(" Creating parent directory " + parent);
 - }
 - parent.mkdirs();
 - }
 - if (name.endsWith("/")) {
 - continue;
 - }
 - if (debug >= 2) {
 - logger.log(" Creating expanded file " + name);
 - }
 - input = jarFile.getInputStream(jarEntry);
 - expand(input, docBase, name);
 - input.close();
 - input = null;
 - }
 - } catch (IOException e) {
 - // If something went wrong, delete expanded dir to keep things
 - // clean
 - deleteDir(docBase);
 - throw e;
 - } finally {
 - if (input != null) {
 - try {
 - input.close();
 - } catch (Throwable t) {
 - ;
 - }
 - input = null;
 - }
 - if (jarFile != null) {
 - try {
 - jarFile.close();
 - } catch (Throwable t) {
 - ;
 - }
 - jarFile = null;
 - }
 - }
 - // Return the absolute path to our new document base directory
 - return (docBase.getAbsolutePath());
 - }
 - /**
 - * Delete the specified directory, including all of its contents and
 - * subdirectories recursively.
 - *
 - * @param dir File object representing the directory to be deleted
 - */
 - public static void deleteDir(File dir) {
 - String files[] = dir.list();
 - if (files == null) {
 - files = new String[0];
 - }
 - for (int i = 0; i < files.length; i++) {
 - File file = new File(dir, files[i]);
 - if (file.isDirectory()) {
 - deleteDir(file);
 - } else {
 - file.delete();
 - }
 - }
 - dir.delete();
 - }
 - /**
 - * Expand the specified input stream into the specified directory, creating
 - * a file named from the specified relative path.
 - *
 - * @param input InputStream to be copied
 - * @param docBase Document base directory into which we are expanding
 - * @param name Relative pathname of the file to be created
 - *
 - * @exception IOException if an input/output error occurs
 - */
 - protected static void expand(InputStream input, File docBase, String name)
 - throws IOException {
 - File file = new File(docBase, name);
 - BufferedOutputStream output = null;
 - try {
 - output =
 - new BufferedOutputStream(new FileOutputStream(file));
 - byte buffer[] = new byte[2048];
 - while (true) {
 - [b]int n = input.read(buffer);[/b]
 - if (n <= 0)
 - break;
 - output.write(buffer, 0, n);
 - }
 - } finally {
 - if (output != null) {
 - try {
 - output.close();
 - } catch (IOException e) {
 - // Ignore
 - }
 - }
 - }
 - }
 - }
 
tomcat解压war包的一点例外的更多相关文章
- linux解压war包的命令
		
网上很多人说用jar包解压,但jar命令解压时不能指定目录,推荐使用unzip解压war包. 一.命令名: unzip 功 能说明:解压缩zip文 件 语 法:unzip [-cflptuvz][-a ...
 - jar 压缩 解压 war包
		
Win+R 输入cmd进入命令行,进入到源码所在目录.所用工具,jdk自带的jar.exe 打包命令:jar -cvf xxx.war * 解包命令: jar -xvf xxx.war * 参数 说明 ...
 - shell 脚本解压war包+备份+tomcat自动关闭+启动
		
公司的开发环境每次替换war包时候,老是需要重新上传并且手动解压,然后再去重启tomcat.觉得这样子太麻烦了,于是写了一个shell脚本,自动解压+备份+tomcat自动关闭+启动.代码如下: #关 ...
 - Linux下打包压缩war、解压war包和jar命令
		
情景:把project_a文件夹下的文件打包成project.war 1. 打包 jar -cvf project.war /project_a 说明: -c 创建war包 -v 显示过程信息 -f ...
 - unzip解压war包并覆盖
		
unzip -o blog.war -d BLOG 参数: -o 不进行询问直接覆盖 -d 压缩文件解压到BLOG文件夹下 详细使用语法: unzip [-Z] [-opts[modifiers]] ...
 - jar 解压war包到指定目录
		
用 jar -xvf .jar 命令默认解压到当前目录,想要解压到指定目录 需要使用unzip .jar -d 目录 如: unzip pay.war -d /home/zookeeper1/tes ...
 - linux解压war包
		
可以用unzip命令 unzip project.war -d project 这样就在当前目录下解压project.war到project目录里面,参数-d的意思是创建project目录 附:unz ...
 - 使用jar打war包或解压war包
		
进入Dos命令行,并到目标文件夹,如C:\Temp,待打包的内容在C:\Temp\Blog里,目标,把Blog里的相应文件打成war报 1.打包 C:\Temp\jar -cvf Blog.war . ...
 - 解压war包
		
unzip cat-alpha-3.0.0.war -d /tmp/test 说明:-d指定解压的路径和文件,文件名不存在会自动创建
 
随机推荐
- selenium python (十)浏览器多窗口处理
			
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip'#在测试过程中有时候会遇到出现多个浏览器窗口的情况,这时候我们可以通过窗口 ...
 - ifstream 流 判断文件是否结尾的函数eof(.xml
			
pre{ line-height:1; color:#800080; font-size:16px;}.sysFunc{color:#627cf6;font-style:italic;font-wei ...
 - cJSON学习笔记
			
1.JSON格式简述 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.它基于JavaScript(Standa ...
 - wifi reaver
			
PIN码的格式很简单, 八位十进制数,最后一位(第8位)为校验位(可根据前7位算出),验证时先检测前4位,如果一致则反馈一个信息,所以只需1万次就可完全扫描一遍前4位,前4位确定下来的话,只需再试10 ...
 - Delphi 调用外部程序并等待其运行结束
			
转自:http://blog.csdn.net/xieyunc/article/details/4140620 如何让Delphi调用外部程序并等待其运行结束 1. uses Window ...
 - 单源最短路径的Bellman-Ford 算法
			
1.算法标签 BFS 2.算法概念 Bellman-Ford算法有这么一个先验知识在里面,那就是最短路径至多在N步之内,其中N为节点数,否则说明图中有负权值的回路,这样的图是找不到最短路径的.因此Be ...
 - Azure Cloud中的Log4Net设置
			
这里的Cloud包含Worker Role和Web Role,Role是运行在云主机中的,这里的主机和VM有所不同,Windows Azure Role Architecture.我们并没有和本地服务 ...
 - Spark RDD概念学习系列之RDD的创建(六)
			
RDD的创建 两种方式来创建RDD: 1)由一个已经存在的Scala集合创建 2)由外部存储系统的数据集创建,包括本地文件系统,还有所有Hadoop支持的数据集,比如HDFS.Cassandra.H ...
 - 1001Sum Problem
			
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): ...
 - STM32中的位带(bit-band)操作
			
转:http://blog.csdn.net/gaojinshan/article/details/11479929 //位带操作,实现51类似的GPIO控制功能 //具体实现思想,参考<< ...