求教C#中filestream的问题 自己动手写了一段传送文件的代码,经验证可以传送成功,当把它移植到某一个程序中使用时,发送正常,接收正常,就是打不开文件,文件大小是正常的,接收到的数据也足够,经过测试后发现是传过来的字符流是乱码(传送一个txt文件,打开后是乱码,图片文件打不开),高手支招,注:单独使用时可以正常接收文件并可以打开。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 另外,你读取文件是否是以二进制方式读取?写入文件是否用二进制方式?因为我注意到你用了“字符流”而不是“字节流”的提法,因为你传输的文件不一定是文本,所以应该用二进制方式传输和读写,把文件内容读取到一个byte[]中,发送接收都用byte[],也直接把byte[]写入文件,这样问题应该少一些, 又或者是2边读取编码的方式不同呢 比如一边gbk 1边utf8 感谢二楼指教,表述有误,文件流变量为filestream,我用filestream.read读取到一个byte[]里,然后用stream.write写入网络流中,stream是networkstream。接收端是用stream.read读取byte[],然后用filestream.write写入文件的。另外我用stream.writebyte((byte)filestream.readbyte())按字节依次写入,接收端用filestream.((byte)stream.readbyte)按字节依次读取,仍然不行啊。单独使用这段代码是正常的,用到程序中就不正常了。 感谢三楼回复,我的代码单独运行可以,放在程序中调用就不成功,如果是这样,我该怎么添加代码呢?我贴出一部分代码来吧,try { FileStream filestream = new FileStream(filepath, FileMode.Open, FileAccess.Read); StreamWriter streamwriter = new StreamWriter(stream, System.Text.Encoding.Unicode); //用来写入文件长度 streamwriter.WriteLine(filestream.Length.ToString()); streamwriter.Flush();//写入长度 byte[] readfile=new byte[filestream.Length]; /* for (Int32 ii = 0; ii < filestream.Length; ii++) { readfile[ii]=(byte)filestream.ReadByte(); //stream.WriteByte((byte)filestream.Readbyte()); }*/ //按字节读取到byte[]中 filestream.Read(readfile, 0, filestream.Length);//读取到readfile中 stream.Write(readfile, 0, readfile.Length);//写入 stream.Flush();//刷新网络流 filestream.Close(); } catch(FileLoadException e) { return ; } catch(FileNotFoundException e) { return ; }接收端:StreamReader streamreader = new StreamReader(stream, System.Text.Encoding.Unicode); String responseData = streamreader.ReadLine();//获取文件长度 string filename = message.Substring(0,message.Length-4);//获取文件路径和文件名 FileStream filestream = new FileStream(filename, FileMode.Create, FileAccess.Write); int length = Convert.ToInt32(responseData); // MessageBox.Show(responseData + " = " + length.ToString()); Int32 i = 0; byte[] writefile = new byte[length]; stream.Read(writefile, 0, length); /* while (i < length) { MessageBox.Show(i.ToString() + " " + length.ToString()); writefile[i]=(byte)stream.ReadByte(); i++; }*/ filestream.Write(writefile, 0, length); filestream.Flush(); filestream.Close(); MessageBox.Show(length.ToString()); // Close everything. stream.Close(); C# winform关闭窗体 程序中的注释 如何将一个大文件分段读取 提示这个变量未声明或未赋值是什么意思啊 Pdf文件操作 Convert.IsDBNull 图片不能显示问题,(很奇怪)帮帮我吧 请问刚才回复我的“小华”大侠? 请教高手!.NET Winform Control: RichTextBox 的快速设置字体属性! 求C #代码注释啊 64位机器下c#调用waveOutOpen时错误
接收端是用stream.read读取byte[],然后用filestream.write写入文件的。
另外我用stream.writebyte((byte)filestream.readbyte())按字节依次写入,接收端用filestream.((byte)stream.readbyte)按字节依次读取,仍然不行啊。单独使用这段代码是正常的,用到程序中就不正常了。
{ FileStream filestream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
StreamWriter streamwriter = new StreamWriter(stream, System.Text.Encoding.Unicode); //用来写入文件长度
streamwriter.WriteLine(filestream.Length.ToString());
streamwriter.Flush();//写入长度
byte[] readfile=new byte[filestream.Length];
/* for (Int32 ii = 0; ii < filestream.Length; ii++)
{
readfile[ii]=(byte)filestream.ReadByte();
//stream.WriteByte((byte)filestream.Readbyte()); }*/ //按字节读取到byte[]中
filestream.Read(readfile, 0, filestream.Length);//读取到readfile中
stream.Write(readfile, 0, readfile.Length);//写入
stream.Flush();//刷新网络流
filestream.Close();
}
catch(FileLoadException e)
{
return ;
}
catch(FileNotFoundException e)
{
return ;
}
接收端:StreamReader streamreader = new StreamReader(stream, System.Text.Encoding.Unicode); String responseData = streamreader.ReadLine();//获取文件长度 string filename = message.Substring(0,message.Length-4);//获取文件路径和文件名 FileStream filestream = new FileStream(filename, FileMode.Create, FileAccess.Write); int length = Convert.ToInt32(responseData);
// MessageBox.Show(responseData + " = " + length.ToString());
Int32 i = 0;
byte[] writefile = new byte[length]; stream.Read(writefile, 0, length); /* while (i < length)
{
MessageBox.Show(i.ToString() + " " + length.ToString());
writefile[i]=(byte)stream.ReadByte();
i++;
}*/ filestream.Write(writefile, 0, length);
filestream.Flush(); filestream.Close();
MessageBox.Show(length.ToString()); // Close everything.
stream.Close();