Java 通过同一套接字发送字符串(文件名)和文件

标签 java sockets stream

我会通过套接字发送“文件名”和"file",我能够轻松发送文件,但是当我尝试发送字符串时,例如PrintWriter pw.println()或DataOutputStream或“out.writeUTF()”文件发送已损坏,我在 StackOverflow 上阅读了很多问题但没有找到答案,我正在寻找一些发送字符串和文件的示例,您能帮忙吗?

服务器

package serverprova;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;



public class ServerProva {

final static int porta=8888;//porta server dove si collegano i client

public static void main(String[] args) throws IOException {
    // TODO code application logic here
    ServerSocket serverSocket=null;

    boolean ascoltando=true;

    serverSocket = new ServerSocket(porta);//avvia il server con il  numero di porta
    Socket s;
    BufferedReader br1=null;
     // ******  interfaccia f=new interfaccia);

      boolean r=true;
      BufferedInputStream bis=null;
       Scanner sc;
    FileOutputStream fout;

    while(ascoltando)
        {

        s=serverSocket.accept();// this socket 

    String filename="";
    String nome_cartella="";
    InputStream in = null;
OutputStream out = null;

   DataInputStream inString;


        inString = new DataInputStream(new BufferedInputStream(s.getInputStream()));
       //  filename = inString.readUTF();
       //  nome_cartella = inString.readUTF();



    in = s.getInputStream();



    //out = new FileOutputStream(nome_cartella+"/"+filename);
out = new FileOutputStream("ciao"+"/"+"asd.jpg");

byte[] b = new byte[20*1024];

int i ;


        while((i = in.read(b)) >0){
            out.write(b, 0, i);
        }
        out.close();
in.close();
//inString.close();
s.close();

        }


 }

}

客户端

    package clientprova;


import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;


public class ClientProva {
final static int porta=8888;

public static void main(String[] args) throws FileNotFoundException, IOException {
    File file;
    file = new File("kirlian12.jpg");
   InetAddress host = InetAddress.getLocalHost();
Socket sock = new Socket(host.getHostName(), 8888);
   DataOutputStream outString = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));

     //   outString.writeUTF(file.getName());
      //  outString.writeUTF("rivelatore2");
     //   outString.flush();

     byte[] b = new byte[20*1024]; 



                InputStream in = new FileInputStream(file);
        OutputStream out = sock.getOutputStream();

        int i ;

        while ((i = in.read(b)) >0    ) {
            out.write( b , 0 , i);
        }


        out.close();
        in.close();

sock.close();



   }


 }

当我尝试取消注释行时,我的文件被损坏

最佳答案

当您使用 BufferedInputStream 包装 InputStream 时,后者“取得 InputStream 的所有权”。这意味着当您调用 readUtf() 时,BufferedInputStream 可能会从 InputStream 读取比读取 UTF 字符串所需的更多字节。因此,当您下次直接访问 InputStream 时,传输文件的某些部分会丢失(因为它之前已读入 BufferedInputStream 的缓冲区并当前驻留在其中)。

inString = new DataInputStream(new BufferedInputStream(s.getInputStream()));
filename = inString.readUTF();
nome_cartella = inString.readUTF();
...
in = s.getInputStream();
while((i = in.read(b)) >0){

您必须从两个选项中进行选择:要么始终使用原始InputStream,要么始终使用BufferedInputStream。同样的推理也适用于OutputStream(但是您通过调用flush()设法避免了问题)。

关于Java 通过同一套接字发送字符串(文件名)和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36895702/

相关文章:

c++ - 从 recv() 读取多个 "message"

java - 从 Java 服务器接收字符串到客户端

java - 如何通过 Akka Streams for java 创建和使用 Pair 流?

java - 嵌套类+扩展Main Activity

java - Struts2中如何让单选按钮默认取消选中

java - Java中的多线程服务器程序

c++ - 加载格式化的二进制文件并将信息分配给结构 c++

java - 哪种数据结构占用更多内存?

java - 没有惰性字段的 Hibernate 克隆实体

c++ - C/C++ 从字符缓冲区读取以填充结构