批量导入功能实现

本文档详细介绍了如何实现批量导入功能,从配置文件设定xlsx保存路径,到编写PathUtil和UploadUtil工具类,再到Action层读取并保存数据到rowMap,以及页面和JS脚本的配合,最后涉及实体类Entity的定义。这是一个针对个人框架的参考实现。
摘要由CSDN通过智能技术生成

批量导入功能《个人框架只作为参考》
颜色相同对应调用部分
1》在XXX-config.xml中定义xlsx文件保存路径

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <properties>
   
    <property name="business.files.temp.path">temp</property>   <!--临时文件目录-->

  </properties>
  <lists/>

  <maps>
  
  </maps>
  <trees/>
  <quotes/>
</configuration>

2》在Util文件夹写一个PathUtil类来接收XXX-Config.xml文件中定义保存位置,
括号内容为XXX-Config.xml加粗斜体部分

  public static String getFilesRealPath() throws Exception
    {
        String realPath = ConfigContext.getProperty( "business.files.real.path" ) ;
        
        return getFilesPath( realPath ) ;
    }

3》在Util文件夹中定义一个UploadUtil类来调用PathUtil中的方法

 public static void uploadZip( HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
    	boolean isMultipart = ServletFileUpload.isMultipartContent( request ) ;
        if( isMultipart )
        {
            FileItemFactory factory = new DiskFileItemFactory() ;
            ServletFileUpload upload = new ServletFileUpload( factory ) ;
            // 得到所有的表单域,它们目前都被当作FileItem
            List<FileItem> fileItems = upload.parseRequest( request ) ;
            String id = "" ;
            String oldName = "" ;
            String extName = "" ;
            // 如果大于1说明是分片处理
            int chunks = 1 ;
            int chunk = 0 ;
            FileItem tempFileItem = null ;

            for( FileItem fileItem : fileItems )
            {
                if( fileItem.getFieldName().equals( "guid" ) )
                {
                    id = fileItem.getString() ;
                }
                else if( fileItem.getFieldName().equals( "name" ) )
                {
                    oldName = new String( fileItem.getString().getBytes( "ISO-8859-1" ), "UTF-8" ) ;
                    extName = oldName.substring( oldName.lastIndexOf( "." ) ) ;
                }
                else if( fileItem.getFieldName().equals( "chunks" ) )
                {
                    chunks = NumberUtils.toInt( fileItem.getString() ) ;
                }
                else if( fileItem.getFieldName().equals( "chunk" ) )
                {
                    chunk = NumberUtils.toInt( fileItem.getString() ) ;
                }
                else if( fileItem.getFieldName().equals( "file" ) )
                {
                    tempFileItem = fileItem ;
                }
            }
            String fileName = id + extName ;
            // 临时目录用来存放所有分片文件
            String tempDirPath = PathUtils.getFilesTempPath() + id ;
            File tempDirFile = new File( tempDirPath ) ;
            if( !tempDirFile.exists() )
            {
                tempDirFile.mkdirs() ;
            }
            // 分片处理时,前台会多次调用上传接口,每次都会上传文件的一部分到后台
            File tempPartFile = new File( tempDirFile, fileName + "_" + chunk + ".part" ) ;
            FileUtils.copyInputStreamToFile( tempFileItem.getInputStream(), tempPartFile ) ;
            tempFileItem.delete() ;
            // 所有分片都存在才说明整个文件上传完成
            boolean uploadDone = true ;
            for( int i = 0; i < chunks; i++ )
            {
                File partFile = new File( tempDirFile, fileName + "_" + i + ".part" ) ;
                if( !partFile.exists() )
                {
                    uploadDone = false ;
                } 
            }
            // 所有分片文件都上传完成,将所有分片文件合并到一个文件中
            if( uploadDone )
            {
                // 创建上传文件最终存储目录
                String destDirPath = PathUtils.getFilesTempPath() ;
                File destDirFile = new File( destDirPath ) ;
                if( !destDirFile.exists() )
                    destDirFile.mkdirs() ;
                
                File destFile = new File( destDirPath, fileName ) ;

                FileOutputStream destfos = new FileOutputStream( destFile, true ) ;
                for( int i = 0; i < chunks; i++ )
                {
                    File partFile = new File( tempDirFile, fileName + "_" + i + ".part" ) ;
                    FileUtils.copyFile( partFile, destfos ) ;
                }
                destfos.close() ;
                // 删除临时目录中的分片文件
                FileUtils.deleteQuietly( tempDirFile ) ;
                Map<String,Object> resultMap = new HashMap<String,Object>() ;
                resultMap.put( "fileId", id ) ;
                resultMap.put( "fileName", oldName ) ;
                resultMap.put( "fileType", extName.substring( 1 ).toUpperCase() ) ;
                resultMap.put( "file_real_path", destFile.getPath() ) ;
                response.getWriter().print( AjaxResult.withMap( resultMap ).getOutput() ) ;
            }
        }
    }

4》在Action

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值