批量导入功能《个人框架只作为参考》
颜色相同对应调用部分
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