Android DataInputStream读取(字节[]缓冲区)返回无关数据

Android DataInputStream读取(字节[]缓冲区)返回无关数据,android,sockets,inputstream,outputstream,Android,Sockets,Inputstream,Outputstream,如何从服务器端代码接收原始内容和文件名。 客户端代码 public void send1(Socket socket) { try { dataOutputStream = new DataOutputStream(socket.getOutputStream()); File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + "temp.txt

如何从服务器端代码接收原始内容和文件名。 客户端代码

public void send1(Socket socket)
{
    try
    {
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + "temp.txt");
        FileInputStream fis = new FileInputStream(file);

        //write file length
        dataOutputStream.writeLong(file.length());
        Log.i("File Size", "" + file.length());

        //write file names
        dataOutputStream.writeUTF(file.getName());
        Log.i("File Name", "" + file.getName());

        //write file to dos
        byte[] buf = new byte[4092];
        int n = 0;
        while((n = fis.read(buf)) != -1)
        {
            Log.i("length bytes", "" + n);
            dataOutputStream.write(buf, 0, n);
        }

        dataOutputStream.flush();
        dataOutputStream.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
文件名为
temp.txt
,内容为
Hello

服务器代码

public void receive1(Socket socket)
{
    try
    {
        inputStream = socket.getInputStream();
        dataInputStream = new DataInputStream(inputStream);

        File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + "test.txt");
        FileOutputStream fos = new FileOutputStream(file);

        int n = 0;
        byte[] buf = new byte[4092];

        //read file name
        /*String fileName = "";
        try
        {
            fileName = dataInputStream.readUTF();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        Log.i("File Name", "" + fileName);*/

        //read file size
        long fileSize = 0;
        try
        {
            fileSize = dataInputStream.readLong();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        Log.i("File Size", "" + fileSize);

        //read file
        while((n = dataInputStream.read(buf)) != -1)
        {
            Log.i("length bytes", "" + n);
            fos.write(buf, 0, n);
        }

        fos.flush();
        fos.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
文件名为
test.txt
,内容为
temp.txthhello
。 在这个
test.txt
文件中,包含
temp.txt
。我没有从
dataInputStream.readUTF()
获取文件名

我把代码弄错了

如果调用了
readUTF()
方法,则会得到异常

例外情况

java.io.EOFException
at libcore.io.Streams.readFully(Streams.java:83)
at java.io.DataInputStream.readFully(DataInputStream.java:99)
at java.io.DataInputStream.decodeUTF(DataInputStream.java:178)
at java.io.DataInputStream.decodeUTF(DataInputStream.java:173)
at java.io.DataInputStream.readUTF(DataInputStream.java:169)

您在send方法的inputStream中的
字符串之前编写了
Long
。您在服务器代码中所做的是希望在长字符串之前接收字符串,该字符串保留在您在send方法中所做的操作上

解决方案:

     //read file size
        long fileSize = 0;
        try
        {
            fileSize = dataInputStream.readLong();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        Log.i("File Size", "" + fileSize);

//read file name
        String fileName = "";
        try
        {
            fileName = dataInputStream.readUTF();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        Log.i("File Name", "" + fileName);
先读取long,然后读取string

示例:

     //read file size
        long fileSize = 0;
        try
        {
            fileSize = dataInputStream.readLong();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        Log.i("File Size", "" + fileSize);

//read file name
        String fileName = "";
        try
        {
            fileName = dataInputStream.readUTF();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        Log.i("File Name", "" + fileName);

除了在注释掉的代码中,您没有调用readUTF()。你期望什么?@EJB如果我调用readUTF(),我得到的是
java.io.EOFException
假设你发布了真正的代码和真正的异常?嗨,如果我发布了这个,我得到的是
java.io.EOFException
,文件内容也是空的。@Sathisathish发布它