Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一些kelobytes从套接字读取,但不写入文件_Java_Sockets_Stream - Fatal编程技术网

Java 一些kelobytes从套接字读取,但不写入文件

Java 一些kelobytes从套接字读取,但不写入文件,java,sockets,stream,Java,Sockets,Stream,我在服务器端,从客户端以二进制流的形式接收图像, 服务器检测到我们处理的图像和大小(以字节为单位)是通过来自客户端的前一条消息到达服务器的 服务器有一个应该读取的文件大小,并且流…到while循环。。。从客户端流读取,并立即写入文件,然后将缓冲区重置为读取 一切正常,直到文件结尾前的某些KB某些KB未写入文件。。。我在服务器库中获得较小的文件。。。(我可以看到底部有小灰色行的图像 以下是我的尝试: /*loop*/ while (bytesRemaining!=0

我在服务器端,从客户端以二进制流的形式接收图像, 服务器检测到我们处理的图像和大小(以字节为单位)是通过来自客户端的前一条消息到达服务器的

服务器有一个应该读取的文件大小,并且流…到while循环。。。从客户端流读取,并立即写入文件,然后将缓冲区重置为读取
一切正常,直到文件结尾前的某些KB某些KB未写入文件。。。我在服务器库中获得较小的文件。。。(我可以看到底部有小灰色行的图像

以下是我的尝试:

               /*loop*/  while (bytesRemaining!=0)  {
                 try {
                   // read from client  
                   baos = new ByteArrayOutputStream();
                   bytesRemaining = sizeToread - readtotal;     
                   bytesRead = OIS.read(aByte, 0, bytesRemaining);
                  readtotal+=bytesRead;
                   System.out.println(     ", Remaining: "    +bytesRemaining);
                   System.out.println("read at oce: "+bytesRead) ;
                    System.out.println(++i+") have Read: "+ readtotal+", aByte:"+aByte[i-1]);


                   try {
                     baos.write(aByte); // write first byte to buffer

                     try{
                     bos.write(baos.toByteArray(),0,bytesRead);
                     }catch (IOException s){  System.out.println("not write to file"+s);}

          baos.reset();

                //     bos.flush();

                 } catch (IOException ex) {
                     Logger.getLogger(ThrdConv.class.getName()).log(Level.SEVERE, null, ex);
                                   System.out.println("baos.write(aByte); error  = "+ex );
                 }
                //  if (!(OIS.available()> 0))break; // until uavailabil stream
                 }      catch (EOFException eoff) {
   Logger.getLogger(ThrdConv.class.getName()).log(Level.SEVERE, null, eoff);
   System.out.println("Line 601 error= "+eoff );
    break;
   } catch (IOException ex) {
    Logger.getLogger(ThrdConv.class.getName()).log(Level.SEVERE, null, ex);
    System.out.println("Line 612,613 error= "+ex );
                 }


            } //loop End ///////////////////////////////////////////
注:文件大小:474591 我的指纹显示:

...
...
...
458) have Read: 468992, aByte:52
, Remaining: 5599
read at oce: 1024
459) have Read: 470016, aByte:59
, Remaining: 4575
read at oce: 1024
460) have Read: 471040, aByte:-117
, Remaining: 3551
read at oce: 1024
461) have Read: 472064, aByte:-110
, Remaining: 2527
read at oce: 1024
462) have Read: 473088, aByte:-8
, Remaining: 1503
read at oce: 1024
463) have Read: 474112, aByte:-124
, Remaining: 479
read at oce: 479
464) have Read: 474591, aByte:125
, Remaining: 0
read at oce: 0
465) have Read: 474591, aByte:30
realfile: C:\wamp\www\RandomSendServer\images\1384252358431.jpg
have Read: 474591 !!!
URL sent Back
我得到了大小为466944的文件,文件名为:474591。。。。 有什么帮助吗

更新

 according to answer 1 I make this change... my loop now looks:
                while ((bytesRead = OIS.read(aByte))>1)
 { bos.write(aByte,0,bytesRead);
 i+=bytesRead;
    System.out.println(bytesRead+" "+i);                 
 }
印刷品:

总数:474591 realfile:C:\wamp\www\RandomSendServer\images\1384262866220.jpg 发送的URL

但同样的问题仍然存在 循环告诉它读取了所有的474591…但是当我检查文件时,它的大小是:466944

最终解决方案
这段可怕的代码充满了错误、误解和毫无意义的废话,最好的办法就是扔掉它。Java中复制流的标准方法如下:

while ((count = in.read(buffer)) > 0)
{
     out.write(buffer, 0, count);
}
while (i < sizeToRead && (count = in.read(buffer, 0, sizeToRead-i > buffer.length ? buffer.length : (int)(sizeToRead-i))) > 0)
请注意,您根本不需要任何
ByteArrayInput/OutputStreams
,但您确实需要处理
read()
返回的结果,并使其保持简单。这项任务并不像您所做的那样困难

编辑您没有指定您的对等方在发送文件后没有关闭连接,这会改变情况。您的“最终解决方案”将不起作用。它通常会超出长度。您需要按如下方式更改读取调用:

while ((count = in.read(buffer)) > 0)
{
     out.write(buffer, 0, count);
}
while (i < sizeToRead && (count = in.read(buffer, 0, sizeToRead-i > buffer.length ? buffer.length : (int)(sizeToRead-i))) > 0)
while(ibuffer.length?buffer.length:(int)(sizeToRead-i)))>0)

并删除内部测试。

我可以知道为什么-1吗?我的否决票的人可以解释为什么吗?这表明你没有关闭文件。我不知道为什么循环条件没有阻止它,所以解决方案在新的更新中,感谢EJP的帮助我的解决方案工作正常,但你的解决方案“保证”不要在尺寸上有任何问题,竖起大拇指,非常感谢。换句话说,除非在特殊情况下尺寸均匀,否则你的尺寸不起作用。