zip文件与jackrabbit

时间:2017-03-27 19:08:24

标签: java inputstream jackrabbit zipinputstream

我正在尝试上传jackrabbit,但zip文件下载已损坏

maven依赖项:

<dependency>
        <groupId>org.apache.jackrabbit</groupId>
        <artifactId>jackrabbit-jcr2dav</artifactId>
        <version>${jackrabit.version}</version>
    </dependency>

    <dependency>
        <groupId>javax.jcr</groupId>
        <artifactId>jcr</artifactId>
        <version>${javax.jcr.version}</version>
    </dependency>

上传代码:

isRollback = compactaFile( IOUtils.toInputStream( dmls.get( 1 ) ), rollbackFileNamesql , rollbackFileName);
        getThreadParam().jackRabbitUtil.upload( path, 
            rollbackFileName,
            isRollback );

private InputStream compactaFile(InputStream dml, String fileName, String tmpFileName) throws Exception{
    byte[] buffer = new byte[1024];
    try{


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream zout = new ZipOutputStream(baos);
        ZipEntry ze= new ZipEntry(fileName );
        zout.putNextEntry(ze);
        int len;
        while ((len = dml.read(buffer)) > 0) {
            zout.write(buffer, 0, len);
        }
        dml.close();

        zout.closeEntry();
        zout.finish();
        zout.close();
        baos.flush();
        baos.close();
        ByteArrayInputStream ios = new ByteArrayInputStream(baos.toByteArray());
        return ios;             
    }catch(Exception e){
        throw e;
    }
}


public synchronized void upload( String filePath, String fileName,InputStream content ) throws Exception {
    if ( StringUtils.isBlank( filePath ) ) {
        throw new Exception( "O caminho para upload do arquivo nao foi informado." );
    }

    if ( StringUtils.isBlank( fileName ) ) {
        throw new Exception( "O nome do arquivo para upload nao foi informado." );
    }

    if ( content == null ) {
        throw new Exception( "O conteudo do arquivo para upload nao foi informado." );
    }
    Session session = getSession();
    Node root = session.getRootNode();
    String[] folders = filePath.split( File.separator );
    Node uploadNode = null;
    VersionManager versionManager = session.getWorkspace().getVersionManager();
    String path = "";

    for ( String folder : folders ) {
        if ( !StringUtils.isBlank( folder ) ) {
            if ( !root.hasNode( folder ) ) {
                root.addNode( folder, "nt:folder" );
                uploadNode = root.getNode( folder );
                uploadNode.addMixin( "mix:versionable" );
                session.save();
            }
            uploadNode = root.getNode( folder );
            path += File.separator + uploadNode.getName();
            versionManager.checkout( path );
            root = uploadNode;
        }
    }

    if ( uploadNode != null && !uploadNode.hasNode( fileName ) ) {
        Node nodefile = uploadNode.addNode( fileName, "nt:file" );
        nodefile.addMixin( "mix:versionable" );
        final Node resource = nodefile.addNode( "jcr:content", "nt:resource" );
        Binary value = session.getValueFactory().createBinary( content );
        resource.setProperty( "jcr:data", value );
        session.save();
        versionManager.checkin( path );
    }

}

它将zip文件上传到jackrabbit,但是当我下载文件时这已损坏

2 个答案:

答案 0 :(得分:1)

这是jackrabbit的版本,在2.15.1上,服务器在2.10.1上,我切换到版本2.13,它工作

答案 1 :(得分:0)

这可能是因为你没有关闭你的一条溪流。