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);
}
}
}
查看全文
相关阅读:
局域网的组建方案
网络的分类
设置Windows的TCP/IP属性和内部网络号码
在Windows8工作站上安装可靠多播协议
在Windows Server 2012服务器上安装可靠多播协议
【转】HTTP协议详解
局域网的通信协议
局域网的拓扑结构
Shiro学习笔记(5)——web集成
IT增值服务,客户案例(一)--山东青岛在职人士,2年.Net经验,转Java开发半年
原文地址:https://www.cnblogs.com/ghx88/p/441657.html
最新文章
MVC的博客
消息服务框架
WebApi迁移ASP.NET Core2.0
NET Core 2.0 使用支付宝
tk.mybatis.mapper.provider.SpecialProvider.<init>()
如何开始使用 Java 机器学习
聊聊Spring Cloud版本的那些事儿
How nginx "location if" works
java中利用RandomAccessFile读取超大文件
android 多线程断点续传下载
热门文章
android 多线程断点续传下载 三
springMVC + hadoop + httpclient 文件上传请求直接写入hdfs
hadoop: hdfs API示例
spring mvc + mahout + hadoop + jersey + mongodb + logback框架
android 多线程断点续传下载 一
Android网络传输中必用的两个加密算法:MD5 和 RSA
android环境下两种md5加密方式
android Base64加密解密
局域网的传输介质、网线水晶头制作图解教程
组件局域网中的无集线器、Windows XP、Windows 7、Windows 8的对等网
Copyright © 2011-2022 走看看