Sending and receiving files on socket(在套接字上发送和接收文件)
问题描述
我正在从 java 服务器向远程 Android 客户端发送文件.我使用输出流写入字节.在读取这些字节时,read() 方法会在流结束后继续尝试读取字节.如果我在服务器端关闭 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 the stream is ended. if I close the outputstream on server-side, read operation work fines. But I have to write file on the same socket again so can't close output stream any solution?
注意:我的代码适用于共享单个文件
编写文件的代码
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 the 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;
}
其实我想重置socket连接
提前致谢
推荐答案
你需要:
- 在文件之前发送文件的长度.您可以为此使用 
DataOutputStream.writeLong(),在接收方使用DataInputStream.readLong(). 在接收端从流中准确读取那么多字节:
- Send the length of the file ahead of the file. You can use 
DataOutputStream.writeLong()for that, andDataInputStream.readLong()at the receiver. 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
其实我是想重置socket连接
Actually I want to reset socket connection
其实你不想做任何这样的事情.
Actually you don't want to do any such thing.
这篇关于在套接字上发送和接收文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在套接字上发送和接收文件
				
        
 
            
        - Java包名称中单词分隔符的约定是什么? 2022-01-01
 - 将log4j 1.2配置转换为log4j 2配置 2022-01-01
 - C++ 和 Java 进程之间的共享内存 2022-01-01
 - 从 finally 块返回时 Java 的奇怪行为 2022-01-01
 - Eclipse 插件更新错误日志在哪里? 2022-01-01
 - Jersey REST 客户端:发布多部分数据 2022-01-01
 - 如何使用WebFilter实现授权头检查 2022-01-01
 - Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
 - Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
 - value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
 
						
						
						
						
						