zoukankan      html  css  js  c++  java
  • XML中的二进制文件的编码与解码[原创]

    (一)把二进制文件放到XML中
    using System;
    using System.Data;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class OutPutXML : System.Web.UI.Page
    {
        
    protected void Page_Load(object sender, EventArgs e)
        
    {
            
    int readByte = 0;
            
    int filesize = 0;
            FileStream fs 
    = new FileStream("C:\\060407color01.jpg", FileMode.Open);
            filesize 
    = Convert.ToInt32(fs.Length);
            
    string filepath = Server.MapPath(Request.ApplicationPath) + "\\" + System.Configuration.ConfigurationManager.AppSettings["UploadDir"].ToString() + "\\" + "test.xml";
            
    int index = fs.Name.LastIndexOf("\\"+ 1;
            
    string filename = fs.Name.Substring(index, (fs.Name.Length - index));

            BinaryReader br 
    = new BinaryReader(fs);
            XmlTextWriter wt 
    = new XmlTextWriter(filepath, Encoding.UTF8);

            wt.WriteStartDocument();
            wt.WriteStartElement(
    "Upload");
            wt.WriteStartElement(
    "username""guest");
            wt.WriteEndElement();
            wt.WriteStartElement(
    "password""123456");
            wt.WriteEndElement();
            wt.WriteStartElement(
    "file");
            wt.WriteAttributeString(
    "size", filesize.ToString()); //文件的大小
            wt.WriteAttributeString("name", filename);//文件名

            
    //以base64编码文件,并添加到XML中的元素中
            byte[] base64buffer = new byte[filesize];
            
    do
            
    {
                readByte 
    = br.Read(base64buffer, 0, filesize);
                wt.WriteBase64(base64buffer, 
    0, readByte);
            }
     while (filesize <= readByte);

            wt.WriteEndElement();
            wt.WriteEndElement();
            wt.WriteEndDocument();
            wt.Close();
            fs.Close();
            br.Close();
        }

    }


    (二)从XML中读出数据、解码、保存
    using System;
    using System.IO;
    using System.Data;
    using System.Text;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Xml;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using PowerEasy.BLL;
    using Power.Model;
    using Power.Rss;

    public partial class Rss_Upload : System.Web.UI.Page
    {
        
    protected void Page_Load(object sender, EventArgs e)
        
    {
            
    int readByte = 0;
            
    int filesize = 0;
            
    string username = string.Empty;
            
    string password = string.Empty;
            
    string uploadPath = Server.MapPath(Request.ApplicationPath) + "\\"+ System.Configuration.ConfigurationManager.AppSettings["UploadDir"].ToString()+"\\";
            
    string filename = string.Empty;
            
    string newFileName = string.Empty;
            
    string exName = string.Empty; //扩展名
            ProblemXML xml = new ProblemXML();
            BinaryWriter bw 
    = null;
            FileStream fs 
    = null;
            XmlTextReader xmlrd 
    = null;

            xmlrd 
    = new XmlTextReader(uploadPath + "test.xml");//Request.InputStream
            
            
    try
            
    {
                
    while (xmlrd.Read())
                
    {
                    
    if (xmlrd.NodeType == XmlNodeType.Element && xmlrd.Name == "username"
                    
    {
                        username 
    = xmlrd.GetAttribute(0).Trim();
                    }

                    
    if (xmlrd.NodeType == XmlNodeType.Element && xmlrd.Name == "password")
                    
    {
                        password 
    = xmlrd.GetAttribute(0).Trim();
                        
    if (!xml.CheckUser(username, password))
                        
    {
                            xml.PutInfo(ProblemXML.UPLOAD_ERR, ProblemXML.INPUT_PASSWORD_ERROR);
                            
    return;
                        }

                    }

                   
                    
    if (xmlrd.NodeType == XmlNodeType.Element && xmlrd.Name.Trim() == "file")
                    
    {
                        
    try
                        
    {
                            filesize 
    = Convert.ToInt32(xmlrd.GetAttribute("size"));
                            filename 
    = xmlrd.GetAttribute("name");
                            exName 
    = CommonFunction.FileValidator(filename);
                            
    if (exName == string.Empty) 
                            
    {
                                xml.PutInfo(ProblemXML.UPLOAD_ERR,
    "上传的文件只可以是 JPG 、GIF 、BMP 、PNG 、ZIP 、RAR 类型! ");
                                
    return;
                            }

                            newFileName 
    = DateTime.Now.Ticks.ToString() + "."+exName;
                            fs 
    = new FileStream(uploadPath + newFileName , FileMode.Create);
                            bw 
    = new BinaryWriter(fs);
                            
    byte[] base64buffer = new byte[filesize];
                            
    do
                            
    {
                                readByte 
    = xmlrd.ReadBase64(base64buffer, 0, filesize);
                                bw.Write(base64buffer, 
    0, readByte);

                            }
     while (readByte >= filesize);
                            bw.Close();
                            fs.Close();
                        }

                        
    catch (Exception ex1)
                        
    {
                            xml.PutInfo(ProblemXML.UPLOAD_ERR,ex1.Message);
                            
    return;
                        }

                    }
    //end of if
                }
    //end of while
               
            }

            
    catch 
            
    {
                
    //hrow new Exception(ex2.Message);
                xml.PutInfo(ProblemXML.UPLOAD_ERR, ProblemXML.INPUT_ERROR);
                
    return;
            }

            
    finally
            
    {
                xmlrd.Close();
            }


            
    if (File.Exists(uploadPath + newFileName))
            
    {
                xml.PutInfo(ProblemXML.UPLOAD_OK, newFileName);
            }

            
    else 
            
    {
                xml.PutInfo(ProblemXML.UPLOAD_ERR, ProblemXML.UPLOAD_ERR_INFO);
            }

          
        }


    }


  • 相关阅读:
    正则表达式笔记(re.search/re.match/re.split/re.compile/用法)
    python爬虫笔记(五)网络爬虫之提取—信息组织与提取方法(3)基于bs4库的HTML内容查找方法
    python爬虫笔记(五)网络爬虫之提取—信息组织与提取方法(2)信息提取的一般方法
    Ubuntu安装anaconda3
    python爬虫笔记(五)网络爬虫之提取—信息组织与提取方法(1)信息标记的三种形式
    python爬虫笔记(四)网络爬虫之提取—Beautiful Soup库(3)基于bs4库的格式化和编码
    python爬虫笔记(四)网络爬虫之提取—Beautiful Soup库(2)基于bs4库的HTML内容遍历方法
    Python爬虫从入门到放弃(十五)之 Scrapy框架中Spiders用法
    Python爬虫从入门到放弃(十四)之 Scrapy框架中选择器的用法
    Python爬虫从入门到放弃(十三)之 Scrapy框架的命令行详解
  • 原文地址:https://www.cnblogs.com/ghx88/p/441657.html
Copyright © 2011-2022 走看看