android开发-java服务器端出错java.io.StreamCorruptedException

问题描述

java服务器端出错java.io.StreamCorruptedException

报错信息:java.io.StreamCorruptedException: invalid stream header: 64000000
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.(Unknown Source)
at org.lxh.server.ServerThreadUtil.run(ServerThreadUtil.java:30)
at java.lang.Thread.run(Unknown Source)
出错代码如下:
public void run() { // 覆写run()方法
try {
PrintStream out = new PrintStream(
client.getOutputStream()); // 取得客户端输出流

        ObjectInputStream ois = new ObjectInputStream(client
                .getInputStream());                 // 这里出错了
        this.upload = (UploadFile) ois.readObject(); // 读取对象
        System.out.println("文件标题:" + this.upload.getTitle());
        System.out.println("文件类型:" + this.upload.getMimeType());
        System.out.println("文件大小:" + this.upload.getContentLength());
        out.print(saveFile());                      // 返回标记
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            this.client.close();                    // 关闭客户端连接
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

解决方案

client?是属于哪个类的对象?

解决方案二:

The provided test code serializes an object to a ByteArrayOutputStream,
converts the generated byte array into a string using the
ByteArrayOutputStream.toString() method, converts the string back into a byte
array using the String.getBytes() method, and then attempts to deserialize the
object from the byte array using a ByteArrayInputStream. This procedure will
in most cases fail because of the transformations that take place within
ByteArrayOutputStream.toString() and String.getBytes(): in order to convert the
contained sequence of bytes into a string, ByteArrayOutputStream.toString()
decodes the bytes according to the default charset in effect; similarly, in
order to convert the string back into a sequence of bytes, String.getBytes()
encodes the characters according to the default charset.

Converting bytes into characters and back again according to a given charset is
generally not an identity-preserving operation. As the javadoc for the
String(byte[], int, int) constructor (which is called by
ByteArrayOutputStream.toString()) states, "the behavior ... when the given
bytes are not valid in the default charset is unspecified". In the test case
provided, the first two bytes of the serialization stream, 0xac and 0xed (see
java.io.ObjectStreamConstants.STREAM_MAGIC), both get mapped to the character
'?' since they are not valid in the default charset (ISO646-US in the JDK I'm
running). The two '?' characters are then mapped back to the byte sequence
0x3f 0x3f in the reconstructed data stream, which do not constitute a valid
header.

The solution, from the perspective of the test case, is to use
ByteArrayOutputStream.toByteArray() instead of toString(), which will yield the
raw byte sequence; this can then be fed directly to the
ByteArrayInputStream(byte[]) constructor.

时间: 2024-09-20 00:30:19

android开发-java服务器端出错java.io.StreamCorruptedException的相关文章

《Java和Android开发实战详解》——1.2节Java基础知识

1.2 Java基础知识 Java和Android开发实战详解 Java语言类似于C++是一种编译型语言,不过两者并不完全相同,严格说来,Java是结合编译和解释优点的一种编程语言. 1.2.1 Java平台 "平台"(Platform)是一种结合硬件和软件的执行环境.Java既是一种高级的面向对象的编程语言,也是一个平台.Java平台是一种纯软件平台,它可以在各种基于硬件的平台上运行,与硬件无关,主要是由JVM和Java API两个部分组成. 1.JVM虚拟机 JVM(Java Vi

《Java和Android开发实战详解》——2.5节良好的Java程序代码编写风格

2.5 良好的Java程序代码编写风格Java和Android开发实战详解Java代码是由程序语句组成的,数个程序语句组合成一个程序块,每一个块拥有数行程序语句或注释,一行程序语句是一个表达式.变量和命令的程序代码. 2.5.1 程序语句Java程序由程序语句(Statement)组成,一行程序语句如同英文的一个句子,内含多个表达式.运算符或Java关键字(详见第3章的说明). 1.程序语句的范例一些Java程序语句的范例,如下所示: int total = 1234; rate = 0.05;

android学习开发提问-Android开发需要的知识

问题描述 Android开发需要的知识 要学习好Android开发,除了学好java,web,jdbc,还需要学习哪些东西? 解决方案 android开发需要会的知识 解决方案二: 主要是java,web吧 看好这些再说别的吧 解决方案三: 主要是java,web吧 看好这些再说别的吧

安卓应用开发-Android开发需要的知识

问题描述 Android开发需要的知识 要学习好Android开发,除了学好java,web,jdbc,还需要学习哪些东西? 解决方案 android开发需要会的知识 解决方案二: 四大组件 线程 sqlite 网络通信 一些第三方的api(有用再学) 自定义View

如何使用Kotlin进行Android开发

Kotlin是一门基于JVM的编程语言,它正成长为Android开发中用于替代Java语言的继承者.Java是世界上使用最多的编程语言之一,当其他编程语言为更加便于开发者使用而不断进化时,Java并没有像预期那样及时跟进. Kotlin是由JetBrains创建的基于JVM的编程语言,IntelliJ正是JetBrains的杰作,而Android Studio是基于IntelliJ修改而来的.Kotlin是一门包含很多函数式编程思想的面向对象编程语言. Kotlin生来就是为了弥补Java缺失的

socket-android写个注册程序时报java.io.StreamCorruptedException

问题描述 android写个注册程序时报java.io.StreamCorruptedException 求大神帮忙看一看,困了好久了,还是解决不了,先上点代码 public void onClick(View v) { if (!userpassword1.getText().toString() .equals(userpassword2.getText().toString())) { Toast.makeText(RegActivity.this, "密码不一致!", Toas

java.io.StreamCorruptedException: invalid type code: AC我真的无能为力了,能帮帮我吗?

问题描述 在一台电脑上实现,客户端的端口不一样,至少要两个客户端才能实现?异常在Client50中由衷感谢服务器importjava.io.*;importjava.net.*;importjava.util.ArrayList;importjava.util.Collection;importjava.util.Scanner;classUserimplementsSerializable{privateStringname;privateInetAddressid;privateintpor

android开发中的java内存泄露分析

做了较长时间的android开发了,发现其实android应用开发入门容易,但是进阶或者成为高级工程师,需要具备的基础能力还是非常高的:性能优化.内存泄露.apk瘦身.热修复等等,这些都非常的考验一个人的能力.android成长之路还很长,自己会持续的走下去.本文主要介绍android内存泄露方面的知识.其实要真的理解内存泄露,需要对JVM.java语言有一定的了解,在这个基础上就比较容易理解本文了. 一.内存泄露概念 在java中,如果一个对象没有可用价值了,但又被其他引用所指向,那么这个对象

《Java和Android开发实战详解》——1.1节编程语言基础知识

1.1编程语言基础知识 Java和Android开发实战详解 "编程语言"(Programming Language)是人类告诉计算机如何工作的一款语言,如同人与人之间沟通使用自然语言,编程语言被设计用于人类与计算机之间进行沟通.从技术角度来说,编程语言就是一款将执行指令传达给计算机的标准通信技术. 1.1.1 程序.软件与应用程序 在说明编程语言之前,我们需要了解什么是程序.软件与应用程序.简单地说来,编程语言提供了语法,可以让我们编写程序代码来建立程序,程序经编译建立成应用程序后,