Java发送字符串(文件名)和文件在同一个套接字上

时间:2016-04-27 16:30:26

标签: java sockets stream

我会通过套接字发送“filename”和“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();



   }


 }

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

1 个答案:

答案 0 :(得分:2)

当你用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 OR始终使用BufferedInputStream。同样的推理也适用于 OutputStream (但您通过调用flush()设法避免了问题。)