取消/编组base64编码的二进制数据作为流

时间:2015-07-10 15:21:42

标签: java xml stream jaxb base64

我有一个应用程序使用JAXB从Oracle数据库导入数据到/从XML导出数据。现在DB中有一些包含上传文件的BLOB字段,我希望在XML中将其作为base64编码字符串。使用@XmlSchemaType(name = "base64Binary")的JAXB开箱即用,如下所示:

@XmlType
public class DocumentTemplateFile {

    // other fields ommited

    @XmlElement(required = true)
    @XmlSchemaType(name = "base64Binary")
    private byte[] data;

    // other code ommited
}

此解决方案的问题在于,由于字节数组,整个文件内容存储在内存中。这取决于文件的大小,这可能会导致一些问题。

因此我想知道是否有一种方法可以创建一个XmlAdapter或者类似的方法来从文件中获取Streams,这样我就可以直接将数据流传输到DB的BLOB或从数据库的BLOB直接传输,而不需要将所有内容都存储在内存中。我正在考虑类似的事情:

public class BlobXmlAdapter extends XmlAdapter<InputStream, OutputStream> {

  @Override
  public InputStream marshal(final OutputStream value) throws Exception {
    return null;
  }

  @Override
  public OutputStream unmarshal(final InputStream value) throws Exception {
    return null;
  }

}

这显然只是一个说明性的例子,因此您可以了解我在寻找什么。最终解决方案不一定要使用XmlAdater s。我只需要一种方法来挂钩un / marshalling进程并通过缓冲区/队列流式传输数据,而不是将所有内容存储在一个字节数组中。

2 个答案:

答案 0 :(得分:2)

此解决方案使用以下第三方库。你应该使用以下maven依赖:

public IQueryable<User> getuserid(string email)
{
    return from m in DBL.Users
           where m.Email == email
           select m.ID;
}

我们需要使用以下自定义<dependency> <groupId>jlibs</groupId> <artifactId>jlibs-xsd</artifactId> <version>2.0</version> </dependency> <repository> <id>jlibs-snapshots-repository</id> <name>JLibs Snapshots Repository</name> <url>https://raw.githubusercontent.com/santhosh-tekuri/maven-repository/master</url> <layout>default</layout> </repository>

XmlAdapter

现在更改你的pojo以使用上面的适配器:

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.io.File;

/**
 * @author Santhosh Kumar Tekuri
 */
public class Base64Adapter extends XmlAdapter<String, File>{
    @Override
    public File unmarshal(String v) throws Exception{
        return new File(v);
    }

    @Override
    public String marshal(File v) throws Exception{
        throw new UnsupportedOperationException();
    }
}

现在应该使用以下帮助程序类来读取xml文件:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.File;

@XmlRootElement
public class DocumentTemplateFile {
    @XmlElement(required = true)
    public String userName;

    @XmlElement(required = true)
    @XmlSchemaType(name = "base64Binary")
    @XmlJavaTypeAdapter(Base64Adapter.class)
    public File data;
}

现在读取xml文件,如下所示:

import jlibs.xml.Namespaces;
import jlibs.xml.xsd.DOMLSInputList;
import jlibs.xml.xsd.XSParser;
import jlibs.xml.xsd.XSUtil;
import org.apache.xerces.xs.XSElementDeclaration;
import org.apache.xerces.xs.XSModel;
import org.apache.xerces.xs.XSSimpleTypeDefinition;
import org.apache.xerces.xs.XSTypeDefinition;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.namespace.QName;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Santhosh Kumar Tekuri
 */
public class JAXBBlobUtil{
    public static XSModel generateSchemas(Class clazz) throws Exception{
        final Map<String, ByteArrayOutputStream> schemas = new HashMap<String, ByteArrayOutputStream>();
        JAXBContext.newInstance(clazz).generateSchema(new SchemaOutputResolver(){
            @Override
            public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException{
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                schemas.put(suggestedFileName, bout);
                StreamResult result = new StreamResult(bout);
                result.setSystemId(suggestedFileName);
                return result;
            }
        });

        DOMLSInputList lsInputList = new DOMLSInputList();
        for(Map.Entry<String, ByteArrayOutputStream> entry : schemas.entrySet()){
            ByteArrayInputStream bin = new ByteArrayInputStream(entry.getValue().toByteArray());
            lsInputList.addStream(bin, entry.getKey(), null);
        }
        return new XSParser().parse(lsInputList);
    }

    private static Object unmarshal(Class clazz, InputSource is) throws Exception{
        XSModel xsModel = generateSchemas(clazz);
        JAXBContext context = JAXBContext.newInstance(clazz);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader xmlReader = factory.newSAXParser().getXMLReader();
        xmlReader = new Base64Filter(xmlReader, xsModel);
        return context.createUnmarshaller().unmarshal(new SAXSource(xmlReader, is));
    }

    private static class Base64Filter extends XMLFilterImpl{
        private XSModel schema;
        private List<QName> xpath = new ArrayList();
        private FileWriter fileWriter;
        public Base64Filter(XMLReader parent, XSModel schema){
            super(parent);
            this.schema = schema;
        }

        @Override
        public void startDocument() throws SAXException{
            xpath.clear();
            super.startDocument();
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException{
            super.startElement(uri, localName, qName, atts);

            xpath.add(new QName(uri, localName));
            XSElementDeclaration elem = XSUtil.findElementDeclaration(schema, this.xpath);
            if(elem!=null){
                XSTypeDefinition type = elem.getTypeDefinition();
                if(type.getTypeCategory()==XSTypeDefinition.SIMPLE_TYPE){
                    XSSimpleTypeDefinition simpleType = (XSSimpleTypeDefinition)type;
                    while(!Namespaces.URI_XSD.equals(simpleType.getNamespace()))
                        simpleType = (XSSimpleTypeDefinition)simpleType.getBaseType();
                    if("base64Binary".equals(simpleType.getName())){
                        try{
                            File file = File.createTempFile("data", "binary");
                            file.deleteOnExit();
                            fileWriter = new FileWriter(file);
                            String absolutePath = file.getAbsolutePath();
                            super.characters(absolutePath.toCharArray(), 0, absolutePath.length());
                        }catch(IOException ex){
                            throw new SAXException(ex);
                        }
                    }
                }
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException{
            try{
                if(fileWriter==null)
                    super.characters(ch, start, length);
                else
                    fileWriter.write(ch, start, length);
            }catch(IOException ex){
                throw new SAXException(ex);
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException{
            xpath.remove(xpath.size() - 1);
            try{
                if(fileWriter!=null)
                    fileWriter.close();
                fileWriter = null;
            }catch(IOException ex){
                throw new SAXException(ex);
            }
            super.endElement(uri, localName, qName);
        }
    };
}

答案 1 :(得分:1)

创建自定义XmlAdapter,如下所示:

public class Base64FileAdapter extends XmlAdapter<String, File>{
    @Override
    public String marshal(File file) throws Exception {
        // todo: read file and convert to base64 and return
    }

    @Override
    public File unmarshal(String data) throws Exception {
        File file = File.createTempFile("dataFile", "binary");
        file.deleteOnExit();
        //todo: base64 decode string data and write bytes to file
        return file;
    }
}

现在在你的bean里面,使用它:

@XmlElement(required = true)
@XmlJavaTypeAdapter(Base64FileAdapter.class)
private File dataFile;

现在整个二进制内容存储在文件中。你可以从这个文件读/写。并在jvm退出时删除此文件。