加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将非文本信息存入xml

发布时间:2020-12-16 09:32:42 所属栏目:百科 来源:网络整理
导读:把图片内容放入XML文件中. public static void main(String[]args){ BASE64Encoderencoder= new BASE64Encoder(); try { Filef= new File( "1_mxz55.jpg" ); if (f.exists()) { FileInputStreamfis= new FileInputStream(f); byte []buffer= new byte [( int

把图片内容放入XML文件中.

  1. publicstaticvoidmain(String[]args){
  2. BASE64Encoderencoder=newBASE64Encoder();
  3. try{
  4. Filef=newFile("1_mxz55.jpg");
  5. if(f.exists())
  6. {
  7. FileInputStreamfis=newFileInputStream(f);
  8. byte[]buffer=newbyte[(int)f.length()];
  9. fis.read(buffer);
  10. Strings_imageData=encoder.encode(buffer);
  11. Documentdoc=DocumentHelper.createDocument();
  12. Elementroot=doc.addElement("ImageList");
  13. ElementimageID=root.addElement("imageID");
  14. ElementimageInfo=root.addElement("imageInfo");
  15. ElementimageSize=root.addElement("imageSize");
  16. ElementimageData=root.addElement("imageData");
  17. imageID.addText("01");
  18. imageInfo.addText("图片1");
  19. imageSize.addText(String.valueOf(f.length()));
  20. imageData.addText(s_imageData);
  21. XMLWriterwriter=newXMLWriter(newFileOutputStream("image2XML.xml"));
  22. writer.write(doc);
  23. writer.flush();
  24. writer.close();
  25. }
  26. }catch(FileNotFoundExceptione){
  27. e.printStackTrace();
  28. catch(IOExceptione){
  29. }

取出XML中图片数据

    Filef=newFile("image2XML.xml");
  1. SAXReaderreader=newSAXReader();
  2. Documentdoc=reader.read(f);
  3. Elementroot=doc.getRootElement();
  4. Elementimage=(Element)root.selectSingleNode("imageData");
  5. Strings_data=image.getText();
  6. BASE64Decoderdecoder=newBASE64Decoder();
  7. byte[]data=decoder.decodeBuffer(s_data);
  8. FileOutputStreamfos=newFileOutputStream("xxx.jpg");
  9. fos.write(data);
  10. fos.flush();
  11. fos.close();
  12. }catch(DocumentExceptione){
  13. //TODOAuto-generatedcatchblock
  14. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读