ant copy 复制文件用法

               

转载自:http://hi.baidu.com/laihua2006/blog/item/e54e113956f12932b9998f44.html

ant copy 复制文件


在用Ant部署时,出现:“警告: 编码 GBK 的不可映射字符”,网上找了一下,主要是编码兼容性的问题,需要个javac指定编码,于是在build.xml中加入以下说明即可:


<target name="compile" depends="prepare" description="编绎">
   <javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="**">
    <!--给编译器指定编码,防止出现:"警告: 编码 GBK 的不可映射字符"-->
  <compilerarg line="-encoding UTF-8 "/>  
    <classpath refid="build.classpath" />
   </javac>
</target>


 


1. 拷贝单个文件到指定目录下。
例:<copy todir="${basedir}/new" file="${basedir}/old/old1.txt1"> 
将${basedir}/old/old.txt文件拷贝到${basedir}/new下


2. 拷贝一批文件到指定目录下
例:<copy todir="${basedir}/new">
            <fileset dir="${basedir}/old">
               <include name="old1.txt" />
               <include name="old2.txt" />
              <exclude name="old8.txt" />
          </fileset>
       </copy>
      这里fileset定义的是原文件的组成形式,<include/>子属性表示包括,<exclude/>子属性表示排除,很简单,通过他们组合实现多文件的筛选,当然我这个例子用得很傻。比如
                <include name="appgen/**"/>
                <include name="ibatis/**"/>
                <exclude name="**/*.log"/>
      拷贝appget目录和ibatis目录下除了.log文件以外的其它所有文件和子目录。


       可以把<fileset/>简写成<fileset dir="${basedir}/old" includes="old1.txt,old2.txt" />,includes可以理解成include的复数形式,包含多个文件时用逗号隔开,excludes也一样。


3. 拷贝一个目录到指定目录下
例:<copy todir="${basedir}/new">
            <fileset dir="${basedir}/old">
              <include name="appgen" />
             <include name="appgen/" />
             <include name=appgen/**" />
             <include name="appgen/***" />
            </fileset>
        </copy>
       同样使用<fileset/>属性,name指定目录名,不过这里要分两种情况,用<include/>子属性和不用<include/>子属性.
      若使用<include/>, 又要分三种情况
          若是“appgen”,则只会拷贝名为appgen的空目录过去,它里面的文件和子目录则不会拷贝。
          若是“appgen/”,或“appgen/**”,则会把整个appgen目录拷贝过去,包括里面的文件和子目录。
          若是“appgen/*”,则只会把该目录和该目录下第一级子目录的所有东西拷贝过去,而不会拷贝第二级和第二级以下的。注:“appgen/*”这儿是一个*号,*号若大于两个,也跟一个*号是同样效果。比如“appgen/*”和“appgen/****”都只拷贝appgen目录下第一级子目录。


注:若appeng这个目录本身就是个空目录,无论怎么写,这个空目录都不会被拷贝。也就是说,copy操作不会产生创建空目录的作用,要想创建空目录,只有用mkdir。


       若不使用任何<include>属性,如
           <fileset dir="${basedir}/old">
           </fileset>
       则会拷贝${basedir}/old下的所有文件和子目录。


注:使用<exclude/>排除目录时,目录名必须写成“appgen/”或“appgen/**”形式,否则不会生效。


       以上是三种拷贝到目录的种类,注意如果计算机中没有todir指定的路径,ant将会自动创建这个路径。


4. 拷贝单个的文件: 
〈copy tofile="old.txt" file="new.txt" /〉就这么简单就行了。
当然也可以写成
<copy tofile="${basedir}/new/new.txt">
      <fileset dir="${basedir}/old" includes="old.txt" />
   </copy>
       这里includes就只能写一个文件,不能写上多个文件,因为不能将多个文件复制到一个文件中去,所以这样麻烦的写法是没有意义的。


Copy Task的属性及功能


Copy 任务具有以下属性:


(1)file:用于指定要复制的源文件。


(2)preservelastmodified:作用是使得复制后的文件与源文件的最后修改时间相同。默认为false。


(3)tofile:用于指定要复制到的文件。


(4)todir:用于指定要复制到的目标目录。todir和tofile只能使用其中一个属性。


(5)overwrite:用于指定是否需要覆盖目录和文件,不管文件是否比源文件新,都会覆盖。默认为false。


(6)filtering:用于指定在复制时是否使用构件文件的全局过滤器对文件进行过滤。默认为false。


(7)flatten:用于指定是否需要复制目录,如果为true代表把所有的文件复制到todir属性设定的目录下。默认为false,复制目录。


(8)includeEmptyDirs:用于指定是否复制空目录。默认为true。


(9)failonerror:用于指定当遇到错误时是否停止执行。默认为true。


(10)verbose:用于指定当复制文件时是否记录日志信息。


(11)encoding:用于设定复制文件时的编码或文件过滤器使用的编码方式。默认时使用Java虚拟机的编码方式。


(12)outputencoding:指定写文件时的编码方式。默认时使用Java虚拟机的编码方式。


(13)enablemultiplemappings:用于设定是否允许多个映射。默认为false。


(14)granularity:用于指定一个文件修改时间的毫秒数据的允许误差。因为不是所有的文件系统的修改时间都是精确到毫秒数。默认时为0,如果为DOS系统则为2。


 


在执行Copy Task时使用文件过滤的实例


下面是一个复制文件的同时替换文件中的特殊符号的例子:


<copy todir="../backup/dir">


    <fileset dir="src_dir"/>


    <filterset>


      <filter token="TITLE" value="Foo Bar"/>


    </filterset>


</copy>


这个例子的作用是把src_dir目录下的所有文件复制到../backup/dir目录,并在所有文件中查找并替换@TITLE@为Foo Bar。当要进行新产品发布时通过需要替换文件中的版本信息和时间信息。


说明:在Ant工具中还提供了copydir和copyfile这两个特定的任务,分别用于对目录和文件进行复制。但自从有了Copy任务后,这两个任务已过期,不建议再使用,应该统一使用Copy任务。


小例:从工作空间复制到Tomcat 里面去


<?xml version="1.0" encoding="UTF-8"?>
<project name="" default="lch" basedir=".">
    <property name="db_file_dir" value="C:/Users/Administrator/workspace/gdagri/WebRoot/WEB-INF/classes/gznxt/database/"/>
    <property name="row_file_dir" value="C:/Users/Administrator/workspace/gdagri/WebRoot/WEB-INF/classes/gznxt/entity/"/>
    <property name="to_db_dir"  value="D:/tool/apache-tomcat-5.5.26/webapps/gdnyw/WEB-INF/classes/gznxt/database/"/>
    <property name="to_row_dir" value="D:/tool/apache-tomcat-5.5.26/webapps/gdnyw/WEB-INF/classes/gznxt/entity/"/>
    <!--  每次本地编译到服务器里面时,不要忘记重启服务器Tomcat  下面是农运通的  laefage -->
    <target name="lch_1" >
            <copy todir="${to_row_dir}">
                <fileset dir="${row_file_dir}">
                    <include name="TransportGoodsRow.class"/>
                    <include name="TransportCartRow.class"/>
                </fileset>
            </copy>
        </target>
    
    <target name="lch" >
        <copy todir="${to_db_dir}" overwrite="true">
            <fileset dir="${db_file_dir}">
                <include name="TransportCartDB.class"/>
                <include name="TransportGoodsDB.class"/>
            </fileset>
        </copy>
    </target>
    
</project>

           
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Ant Design 的 Upload 组件可以上传文件,并且可以通过一些配置来获取上传的文件数据。如果上传的文件是 Excel 文件,我们可以使用 js-xlsx 库来读取 Excel 数据。 下面是一个示例代码: ```vue <template> <div> <a-upload accept=".xls,.xlsx" :before-upload="beforeUpload" :on-success="onSuccess" > <a-button> <a-icon type="upload" /> 点击上传 </a-button> </a-upload> <div v-if="excelData"> <h3>Excel 数据</h3> <table> <thead> <tr> <th v-for="(value, key) in excelData[0]">{{ key }}</th> </tr> </thead> <tbody> <tr v-for="row in excelData"> <td v-for="value in row">{{ value }}</td> </tr> </tbody> </table> </div> </div> </template> <script> import XLSX from 'xlsx'; export default { data() { return { excelData: null, }; }, methods: { beforeUpload(file) { // 只处理 Excel 文件 const isExcel = file.type === 'application/vnd.ms-excel' || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; if (!isExcel) { this.$message.error('只能上传 Excel 文件!'); return false; } }, onSuccess(response) { // 读取 Excel 数据 const workbook = XLSX.read(response.file.response, { type: 'array' }); const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; const data = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); // 第一行为表头,去掉 data.shift(); this.excelData = data; }, }, }; </script> ``` 在这个示例代码中,我们使用了 Ant Design 的 Upload 组件,并通过 `accept` 属性限制只能上传 Excel 文件。在上传之前,我们可以通过 `before-upload` 属性来判断是否是 Excel 文件,并在不是 Excel 文件的情况下给用户提示错误信息。 在上传成功后,我们可以通过 `on-success` 属性来获取上传的文件信息。`response.file.response` 是上传成功后服务器返回的文件内容,我们使用 js-xlsx 库来读取 Excel 数据,然后将数据显示在页面上。 需要注意的是,这里使用了 `XLSX.utils.sheet_to_json` 方法将 Excel 表格转换成 JSON 格式的数据,但是这个方法只能转换表格中有数据的部分,如果表格中有合并单元格等特殊情况,可能会出现转换错误的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值