web service的缓冲区溢出????谁能解决?

caomo 2001-11-20 10:42:40
在web service函数中想对二进制文件编码,
[webmethod]
public string GetFile(string filePath)
{FileStream myfile=File.OpenRead(filePath);
BinaryReader br=new BinaryReader(myfile);
byte[] btBuf=new byte[myfile.Length];
long i=0;
while (br.PeekChar()>-1)
{
btBuf[i]=br.ReadByte();
i++;
}
myfile.Close();
return System.Convert.ToBase64String(btBuf);
}
调用时当文件大于4k就出现:
未处理的“System.Web.Services.Protocols.SoapException”类型的异常出现在 syst
em.web.services.dll 中

附加信息:System.Web.Services.Protocols.SoapException: 服务器无法处理请求。
---> System.ArgumentException: 转换缓冲区溢出。
at System.Text.UTF8Encoding.GetChars(Byte[] bytes, Int32 byteIndex, Int32
byteCount, Char[] chars, Int32 charIndex, UTF8Decoder decoder)
at System.Text.UTF8Decoder.GetChars(Byte[] bytes, Int32 byteIndex, Int32
byteCount, Char[] chars, Int32 charIndex)
at System.IO.BinaryReader.InternalReadOneChar()
at System.IO.BinaryReader.Read()
at System.IO.BinaryReader.PeekChar()
at useResData.FileServer.GetFile(String filePath) in e:\ddisk\inetpub\www
root\sei\test\useresdata\fileserver.asmx.cs:line 125
...全文
188 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
caomo 2001-11-25
  • 打赏
  • 举报
回复
怪哉?
加上写缓冲区的动作,那么你就没对我的程序进行任何改动
难道真是
<httpRuntime executionTimeout="900" maxRequestLength="102400" />
起了作用?你在你的web.config里加了这个吗?你试试不加这个会不会出问题?
你试过对一个>10k的gif或者其它纯二进制文件进行转换吗?
如果真的能解决问题我可以再给你些分。
itroad 2001-11-24
  • 打赏
  • 举报
回复
我是故意的,实际上是否存在那句话与你的问题并无多大关系。
拿到new一个buffer就不分配缓冲区了吗?
关键的是我没有出现那种错误。

另外:加上那句话,我已经得到了正确的结果,不知你的状况如何?
caomo 2001-11-23
  • 打赏
  • 举报
回复
老大,你看了返回结果没,你的程序去掉了写缓冲区的部分。btBuf里面压根全都是空值啊。转换有何意义?
caomo 2001-11-22
  • 打赏
  • 举报
回复
不好意思,还是不能加分。我没有看到它起作用!
我又仔细测试了一下,这个错误不仅和大小有关而且还和文件类型有关,我处理一个4k的gif会出错,但一个30k的doc文件却没问题。好像对文本格式没问题,对二进制有问题:(
itroad 2001-11-22
  • 打赏
  • 举报
回复
public string GetFile(string filePath)
{FileStream myfile=File.OpenRead(filePath);
BinaryReader br=new BinaryReader(myfile);
byte[] btBuf=new byte[myfile.Length];
//试一下
br.Read(byte,0,myfile.Length);
myfile.Close();
return System.Convert.ToBase64String(btBuf);
}
itroad 2001-11-22
  • 打赏
  • 举报
回复
[WebMethod]
public string GetFile(string filePath)
{
System.IO.FileStream myfile=System.IO.File.OpenRead(filePath);
System.IO.BinaryReader br=new System.IO.BinaryReader(myfile);
byte[] btBuf=new byte[myfile.Length];

myfile.Close();
return System.Convert.ToBase64String(btBuf);
}
在我的机器上,运行正常。
注:我的文件是814kb,不过时间长了点。
caomo 2001-11-22
  • 打赏
  • 举报
回复
而且,这两种形式都是换汤不换药,只是一种简洁(但不可行:))
缓冲区损耗不会有丝毫改变。
caomo 2001-11-22
  • 打赏
  • 举报
回复
你试过了吗?
br.Read(byte,0,myfile.Length);
myfile.Length是long,br.Read中是int(强制转换会丢东西的),如果能这样我早就这样了,
何必绕着弯一个个字节读?
julyclyde 2001-11-22
  • 打赏
  • 举报
回复
这是不是个漏洞?能不能用来黑服务器?
itroad 2001-11-20
  • 打赏
  • 举报
回复
快加分!
请在你的web.config中加入:
<httpRuntime executionTimeout="900" maxRequestLength="102400" />
其中的maxRequestLength="102400"就是你的app所能接受的最大文件kb数。
ok?

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧