spring mvc 导入zip并解析其中数据(内excel)

直接附上代码

    /**
     * 导入Zip
     * @param dc
     * @param excelFile
     * @return
     * @throws IOException 
     */
    private DataCenter importZip(DataCenter dc, MultipartFile zipFile) {
    	boolean successFlag = true;
    	//获得文件名  
        String fileName = zipFile.getOriginalFilename();
        //检查文件  
		if("".equals(fileName)){ 
            successFlag = false ; 
        	dc.setParameter("successFlag", successFlag);
        	dc.setParameter("message", "文件不存在");
        	return dc;
        }  
    	try{
    		//再本地创建一个文件,读取此文件 防止浏览器读取的文件被损坏
			File localFile= new File("fyJyqdYhqdxxZip.zip");
		    FileOutputStream ftpOutstream = new FileOutputStream(localFile);
		    byte[] appByte =  zipFile.getBytes() ;
		    ftpOutstream.write(appByte);
		    ftpOutstream.flush();
		    ftpOutstream.close();//创建完毕后删除
		    
		    File file = new File("fyJyqdYhqdxxZip.zip");
            //不解压直接读取,加上gbk解决乱码问题,file转ZipInputStream
		    ZipInputStream in = new ZipInputStream(new FileInputStream(file),Charset.forName("GBK"));
	        //不解压直接读取,加上gbk解决乱码问题,ZipInputStream转BufferedReader
	    	BufferedReader br = new BufferedReader(new InputStreamReader(in,"gbk"));
	    	//把InputStream转成ByteArrayOutputStream 多次使用
	    	ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            ZipEntry ze;
            while ((ze = in.getNextEntry())!=null) {
            	if (ze.isDirectory()){
            		//如果是目录,不处理
            		continue;	
            	}
                try {
                	String zipFileName = ze.getName();
                	//不是我们指定的文件不导入,XXXXX.市场化清单.xls
                	if (zipFileName != null && zipFileName.indexOf(".") != -1
                     		&&zipFileName.equals(zipFileName.substring(0,zipFileName.indexOf(".xls"))+"市场化清单.xls")) {
               		 	continue;
                	}
                	byte[] buffer = new byte[1024];  
        	    	int len;  
        	    	while ((len = in.read(buffer)) > -1 ) {  
        	    	    baos.write(buffer, 0, len);  
        	    	}  
        	    	baos.flush();                
        	    	  
        	    	InputStream stream1 = new ByteArrayInputStream(baos.toByteArray()); 
					if(!readSchjsdExcelBySteam(stream1, ze.getName()))successFlag = false;//具体上传代码
					baos.reset();
				} catch (Exception e) {
		        	successFlag = false;
					e.printStackTrace();
				}
            } 
            br.close();
   	     	in.close();
   	     	baos.close();
   	     	//处理完毕删除
            localFile.delete();
        }catch(IOException e){
        	successFlag = false;
            e.printStackTrace();
        } 
    	dc.setParameter("successFlag", successFlag);
		return dc;

    } 

 

你可能感兴趣的:(JavaEE)