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);
}
}
}
查看全文
相关阅读:
OC中extern,static,const的用法
pod install 报错
设置Image渲染模式使用TintColor
VLC 用到的那些 YUV 格式
base64编码原理
scp 拷贝文件时报错
linux 恢复误删文件
hadoop 集群安装(Cloudera CDH方式)
记录自己需要读的几本书
求解最长回文子串
原文地址:https://www.cnblogs.com/ghx88/p/441657.html
最新文章
java笔记之java内存结构
J2SE笔记之方法
J2SE笔记之方法
java笔记之byte的面试题案例分析
java笔记之byte的面试题案例分析
linux开机自启动
Mysql #1251错误解决方法
OutputDebugString()函数
Bmob Unity Android支付接入文档
AB包
热门文章
unity背包系统(摘抄)
tcp粘包和拆包的原因及处理方案
经常用到的网址(借鉴别人的),方便自己使用
swift4.0 正则表达式判断手机号
AFNetWorking 对汉字部分UTF-8编码
iOS pods-xxxx-frameworks.sh:permission denied问题
通过渲染改变tabBarItem的背景图片
ios跳转到系统设置
3D Touch开发
保持屏幕常亮的方法
Copyright © 2011-2022 走看看