解决思路:
- 将获取的网络流转化成内存流
- 内存流位置指向0
- 内存流转化成字节流
//获取fileNamePath路径的网络流 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileNamePath); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); //将网络流写入内存流 MemoryStream memoryStream = new MemoryStream(); const int bufferLength = 1024; int actual; byte[] buffer = new byte[bufferLength]; while ((actual = stream.Read(buffer, 0, bufferLength)) > 0) { memoryStream.Write(buffer, 0, actual); } memoryStream.Position = 0; //将内存流转成字节流 byte[] bytes = new byte[memoryStream.Length]; memoryStream.Read(bytes,0,bytes.Length); memoryStream.Seek(0,SeekOrigin.Begin);
参考链接:https://blog.csdn.net/fengziyun/java/article/details/7414891