发送和接收的套接字文件

发送和接收的套接字文件

本文介绍了发送和接收的套接字文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从Java服务器将文件发送到远程Android客户端。我使用的OutputStream写入的字节数。在阅读这些字节读取()方法不断地尝试读取的字节流结束后。如果我关闭的OutputStream在服务器端,读操作工作的罚款。但是,我必须写在同一个套接字文件,再这样不能关闭输出流的任何解决办法吗?

I am sending files to remote Android client from java server. I write the bytes using outputstream. On reading these bytes read() method keep trying to read bytes after stream is ended. if I close the outputstream on server side, read operation work fines. But I have to write file on same socket again so can't close output stream any solution?

注:我的code正常工作,共享单个文件

$ C $下写文件时

CODE FOR WRITING FILE

   public static void writefile(String IP, String filepath, int port, OutputStream out ) throws IOException {

        ByteFileConversion bfc = new ByteFileConversion();
        byte[] file = bfc.FileToByteConversion(filepath);

        out.write(file, 0, file.length);
        out.close(); // i donot want to close this and how can I tell reading side that stream is ended.


        System.out.println("WRITTEN");

    }

我在这里读在Android上的文件:

Here Am I reading file on Android :

     public Bitmap fileReceived(InputStream is)
 {

Bitmap bitmap = null;
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "a.png";
String imageInSD = baseDir + File.separator + fileName;
//  System.out.println(imageInSD);
if (is!= null) {
    FileOutputStream fos = null;
    OutputStream bos = null;
    try {

        bos = new FileOutputStream(imageInSD);

        byte[] aByte = new byte[1024];
        int bytesRead;
         int index = 0;
         DataInputStream dis = new DataInputStream(is);



        while ( (bytesRead = is.read(aByte)) >0   ) {
             index =  bytesRead +index;
            bos.write(aByte, 0, bytesRead);

          //  index = index+ bytesRead;

       System.out.println("Loop"+aByte+ "    byte read are "+bytesRead+ "whree  index ="+ index);

        }
        bos.flush();
       bos.close();

    Log.i("IMSERVICE", "out of loop");
        java.io.FileInputStream in = new FileInputStream(imageInSD);
   bitmap = BitmapFactory.decodeStream(in);
      bitmap = BitmapFactory.decodeFile(imageInSD);

Log.i("IMSERVICE", "saved");
   //   if (bitmap != null)
 //       System.out.println("bitmap is    "+ bitmap.toString());

    } catch (IOException ex) {
        // Do exception handling
 //     Log.i("IMSERVICE", "exception ");
        System.out.println("ex");
    }
}

return bitmap;
}

其实我想重置套接字连接

Actually I want to reset socket connection

在此先感谢

推荐答案

您需要:

  1. 发送文件的长度超前的文件。您可以使用 DataOutputStream.writeLong()是什么,以及 DataInputStream.readLong()在接收器。
  2. 在接收从流中读取正是很多字节:

  1. Send the length of the file ahead of the file. You can use DataOutputStream.writeLong() for that, and DataInputStream.readLong() at the receiver.
  2. Read exactly that many bytes from the stream at the receiver:

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

E&放大器; OE

E&OE

其实我想重置套接字连接

其实你不想做任何这样的事情。

Actually you don't want to do any such thing.

这篇关于发送和接收的套接字文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:37