Java 使用ObjectOutputStream.writeObject()进行的数据传输不起作用

Java 使用ObjectOutputStream.writeObject()进行的数据传输不起作用,java,android,sockets,tcp,Java,Android,Sockets,Tcp,我创建了一个android到pc客户端服务器的应用程序,在我的应用程序的其他功能中,我使用ObjectOutputStream.writeObject(AbstractPacket对象)与我的应用程序一起发送控件和数据,现在当我尝试发送一个文件(例如:一个小的jpg/png文件)时,它们都可以正常工作,将文件字节发送到服务器端并重新创建原始jpg文件后,该文件显示其已损坏且未显示,但其大小与从客户端android应用程序发送的大小完全相同。 这里有一些代码 客户端:(文件到bytearray)

我创建了一个android到pc客户端服务器的应用程序,在我的应用程序的其他功能中,我使用ObjectOutputStream.writeObject(AbstractPacket对象)与我的应用程序一起发送控件和数据,现在当我尝试发送一个文件(例如:一个小的jpg/png文件)时,它们都可以正常工作,将文件字节发送到服务器端并重新创建原始jpg文件后,该文件显示其已损坏且未显示,但其大小与从客户端android应用程序发送的大小完全相同。 这里有一些代码

客户端:(文件到bytearray)

FileTransferPacket扩展了AbstractPacket:

public class FileTransferPacket extends AbstractPacket implements Serializable {


byte[] bytes;
long size;
String filename;

public FileTransferPacket(byte[] bytes,long size,String filename) {
super(Protocol.Command.FILE_TRANSFER);
this.bytes = bytes;
this.size = size;

this.filename = filename;
}

public byte[] getBytes() {
return bytes;
}

public long getSize() {return size; }

public String getFilename() {return filename; }
}
以下是如何将数据包对象发送到服务器:

public void sendPacket(AbstractPacket packet) throws IOException {
    connectionOutput.writeObject(packet);
    connectionOutput.flush();
    connectionOutput.reset();

  }
在服务器端,我读取如下对象:

AbstractPacket=(AbstractPacket)connectionInput.readObject()

接收方。handlerReceiveData(数据包、连接)

有人能告诉我这里到底出了什么问题,也请建议如何修复它们

经过一些搜索,我确实发现,如果我使用普通OutputStreams只将文件的bytearray发送到服务器,也许我可以修复,它可以工作:

write(mybytearray,0,mybytearray.length)


但在我的整个应用程序中,我使用了ObjectoutputStream,因此如何在一个套接字上同时包含这两种类型的流,以及在服务器端如何区分作为对象/字节缓冲区的传入数据。

您可以添加控制字节来指示流的类型。 下面是它如何工作的示例代码

这有点棘手。 如果可能,使用不同的端口号会更简单

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // File stream.
        FileOutputStream fos = new FileOutputStream("test1.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        // add controll bytes.
        bos.write(0);
        bos.write("file contents".getBytes());
        bos.close();

        // Object stream.
        FileOutputStream fos2 = new FileOutputStream("test2.txt");
        // add controll bytes.
        fos2.write(1);
        ObjectOutputStream oos = new ObjectOutputStream(fos2);
        Exception serializableObj = new RuntimeException("serialize test");

        oos.writeObject(serializableObj);
        oos.close();

        readStream(new FileInputStream("test1.txt"));
        readStream(new FileInputStream("test2.txt"));
    }

    private static void readStream(InputStream in) throws IOException, ClassNotFoundException {
        int controll = in.read();
        if (controll == 0) { // File stream
            BufferedInputStream bis = new BufferedInputStream(in);
            FileOutputStream fos = new FileOutputStream("test3.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            byte[] buff = new byte[1024];
            while (true) {
                int ret = bis.read(buff);
                if (ret < 0) {
                    break;
                }
                bos.write(buff, 0, ret);
            }
            in.close();
            bos.close();

            BufferedReader br = new BufferedReader(new FileReader("test3.txt"));
            String line = br.readLine();
            System.out.println("line: " + line);
            assert (line.equals("file contents"));
            br.close();
        } else if (controll == 1) { // Object stream
            ObjectInputStream ois = new ObjectInputStream(in);
            RuntimeException e = (RuntimeException)ois.readObject();
            assert (e instanceof RuntimeException);
            assert (e.getMessage().equals("serialize test"));
            System.out.println("message: " + e.getMessage());
            ois.close();
        }

    }
}
import java.io.*;
公共班机{
公共静态void main(字符串[]args)引发IOException,ClassNotFoundException{
//文件流。
FileOutputStream fos=新的FileOutputStream(“test1.txt”);
BufferedOutputStream bos=新的BufferedOutputStream(fos);
//添加控制字节。
bos.write(0);
write(“文件内容”.getBytes());
bos.close();
//对象流。
FileOutputStream fos2=新的FileOutputStream(“test2.txt”);
//添加控制字节。
fos2.写入(1);
ObjectOutputStream oos=新的ObjectOutputStream(fos2);
Exception serializableObj=新运行时异常(“序列化测试”);
oos.writeObject(serializableObj);
oos.close();
readStream(新文件输入流(“test1.txt”);
readStream(新文件输入流(“test2.txt”);
}
私有静态void readStream(InputStream in)抛出IOException、ClassNotFoundException{
int controll=in.read();
如果(controll==0){//文件流
BufferedInputStream bis=新的BufferedInputStream(in);
FileOutputStream fos=新的FileOutputStream(“test3.txt”);
BufferedOutputStream bos=新的BufferedOutputStream(fos);
字节[]buff=新字节[1024];
while(true){
int ret=双读数(浅黄色);
如果(ret<0){
打破
}
bos.write(buff,0,ret);
}
in.close();
bos.close();
BufferedReader br=新的BufferedReader(新文件读取器(“test3.txt”);
String line=br.readLine();
System.out.println(“行:”+行);
断言(line.equals(“文件内容”);
br.close();
}else如果(controll==1){//对象流
ObjectInputStream ois=新ObjectInputStream(in);
RuntimeException e=(RuntimeException)ois.readObject();
断言(运行时异常的实例);
断言(e.getMessage().equals(“序列化测试”);
System.out.println(“消息:+e.getMessage());
ois.close();
}
}
}
public synchronized void handleReceiveData(AbstractPacket packet, TcpConnection connection) {

String filename=packet.getFilename();
long size= packet.getSize();

byte [] mybytearray  = new byte [(int)size];

byte[] myByteArray=packet.getBytes();

String userHomeFolder = System.getProperty("user.home");

userHomeFolder+="\\Desktop\\"+filename;

try {
FileOutputStream fos = new FileOutputStream(userHomeFolder);

BufferedOutputStream bos = new BufferedOutputStream(fos);

bos.write(mybytearray, 0 , (int)size);

bos.flush();
fos.close();
bos.close();

} catch (IOException e) {
e.printStackTrace();
}
}
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // File stream.
        FileOutputStream fos = new FileOutputStream("test1.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        // add controll bytes.
        bos.write(0);
        bos.write("file contents".getBytes());
        bos.close();

        // Object stream.
        FileOutputStream fos2 = new FileOutputStream("test2.txt");
        // add controll bytes.
        fos2.write(1);
        ObjectOutputStream oos = new ObjectOutputStream(fos2);
        Exception serializableObj = new RuntimeException("serialize test");

        oos.writeObject(serializableObj);
        oos.close();

        readStream(new FileInputStream("test1.txt"));
        readStream(new FileInputStream("test2.txt"));
    }

    private static void readStream(InputStream in) throws IOException, ClassNotFoundException {
        int controll = in.read();
        if (controll == 0) { // File stream
            BufferedInputStream bis = new BufferedInputStream(in);
            FileOutputStream fos = new FileOutputStream("test3.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            byte[] buff = new byte[1024];
            while (true) {
                int ret = bis.read(buff);
                if (ret < 0) {
                    break;
                }
                bos.write(buff, 0, ret);
            }
            in.close();
            bos.close();

            BufferedReader br = new BufferedReader(new FileReader("test3.txt"));
            String line = br.readLine();
            System.out.println("line: " + line);
            assert (line.equals("file contents"));
            br.close();
        } else if (controll == 1) { // Object stream
            ObjectInputStream ois = new ObjectInputStream(in);
            RuntimeException e = (RuntimeException)ois.readObject();
            assert (e instanceof RuntimeException);
            assert (e.getMessage().equals("serialize test"));
            System.out.println("message: " + e.getMessage());
            ois.close();
        }

    }
}