- publicstaticvoidmain(String[]args){
- BASE64Encoderencoder=newBASE64Encoder();
- try{
- Filef=newFile("1_mxz55.jpg");
- if(f.exists())
- {
- FileInputStreamfis=newFileInputStream(f);
- byte[]buffer=newbyte[(int)f.length()];
- fis.read(buffer);
- Strings_imageData=encoder.encode(buffer);
- Documentdoc=DocumentHelper.createDocument();
- Elementroot=doc.addElement("ImageList");
- ElementimageID=root.addElement("imageID");
- ElementimageInfo=root.addElement("imageInfo");
- ElementimageSize=root.addElement("imageSize");
- ElementimageData=root.addElement("imageData");
- imageID.addText("01");
- imageInfo.addText("图片1");
- imageSize.addText(String.valueOf(f.length()));
- imageData.addText(s_imageData);
- XMLWriterwriter=newXMLWriter(newFileOutputStream("image2XML.xml"));
- writer.write(doc);
- writer.flush();
- writer.close();
- }
- }catch(FileNotFoundExceptione){
- e.printStackTrace();
- catch(IOExceptione){
- }
取出XML中图片数据
- Filef=newFile("image2XML.xml");
- SAXReaderreader=newSAXReader();
- Documentdoc=reader.read(f);
- Elementroot=doc.getRootElement();
- Elementimage=(Element)root.selectSingleNode("imageData");
- Strings_data=image.getText();
- BASE64Decoderdecoder=newBASE64Decoder();
- byte[]data=decoder.decodeBuffer(s_data);
- FileOutputStreamfos=newFileOutputStream("xxx.jpg");
- fos.write(data);
- fos.flush();
- fos.close();
- }catch(DocumentExceptione){
- //TODOAuto-generatedcatchblock
- }