Java 套接字 ClassCastException
Java Socket ClassCast Exception
我一直在开发一种软件,使用 Java 套接字通过 TCP/IP 连接发送文件。程序编译,但是 'Server Application' 抛出 Class Cast Exception 并且文件没有发送到服务器。谁能帮助我,我将不胜感激。以上是以下代码:
服务器class:
package filesender;
import java.io.*;
import java.net.*;
public class ServidorTCP {
public static void main(String[] args)
throws IOException, UnknownHostException {
try (ServerSocket socketServer = new ServerSocket(6789)) {
Socket socket = socketServer.accept();
System.out.println("Conexão realizada com o cliente na porta 6789");
byte[] objectAsByte = new byte[socket.getReceiveBufferSize()];
BufferedInputStream bf =
new BufferedInputStream(socket.getInputStream());
bf.read(objectAsByte);
Arquivo arquivo = (Arquivo) getObjectFromByte(objectAsByte);
String dir = arquivo.getDiretorio() + "\" + arquivo.getNome();
System.out.println("Criando o arquivo: " + dir);
try (FileOutputStream fos = new FileOutputStream(dir)) {
fos.write(arquivo.getConteudo());
}
}
}
private static Object getObjectFromByte(byte[] objectAsByte) {
Object obj = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
bis = new ByteArrayInputStream(objectAsByte);
ois = new ObjectInputStream(bis);
obj = ois.read();
bis.close();
ois.close();
} catch (IOException e) {
}
return obj;
}
}
客户class
package filesender;
import java.io.*;
import java.net.*;
public class ClienteTCP {
public static Arquivo arquivo;
private static final long serialVersionUID = 1L;
public static void main(String[] args)
throws IOException, UnknownHostException {
selectFile();
try (Socket clientSocket = new Socket("127.0.0.1", 6789)) {
try (BufferedOutputStream bf =
new BufferedOutputStream(clientSocket.getOutputStream())) {
byte[] bytea = serializeFile();
bf.write(bytea);
bf.flush();
}
}
}
private static void selectFile() {
try {
File file = new File("C:\Users\DeveloperEng\Documents\"
+ "teste\arquivo.txt");
byte[] bFile = new byte[(int) file.length()];
try (FileInputStream fis = new FileInputStream(file)) {
fis.read(bFile);
}
arquivo = new Arquivo();
arquivo.setConteudo(bFile);
arquivo.setNome("teste.txt");
arquivo.setDiretorio("C:\Users\DeveloperEng\Documents");
} catch (IOException e) {
}
}
private static byte[] serializeFile() {
ByteArrayOutputStream bao = null;
ObjectOutputStream ous = null;
try {
bao = new ByteArrayOutputStream();
ous = new ObjectOutputStream(bao);
ous.writeObject(arquivo);
//return bao.toByteArray();
} catch (IOException e) {
}
return bao.toByteArray();
}
}
文件class
package filesender;
import java.io.*;
public class Arquivo implements Serializable {
private String nome;
private byte[] conteudo;
private String diretorio;
private static final long serialVersionUID = 1L;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public byte[] getConteudo() {
return conteudo;
}
public void setConteudo(byte[] conteudo) {
this.conteudo = conteudo;
}
public String getDiretorio() {
return diretorio;
}
public void setDiretorio(String diretorio) {
this.diretorio = diretorio;
}
}
IDE 引发的异常:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to filesender.Arquivo
at filesender.ServidorTCP.main(ServidorTCP.java:20)
C:\Users\DeveloperEng\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 5 segundos)
请参阅 ServidorTCP class 的第 20 行。
您不能将 "getObjectFromByte" 方法返回的值转换为 "Archivio",因为它不是兼容类型。特别是,它是一个整数。
发生这种情况是因为 ObjectInputStream 的读取方法 returns 一个 int (See Javadoc).
我一直在开发一种软件,使用 Java 套接字通过 TCP/IP 连接发送文件。程序编译,但是 'Server Application' 抛出 Class Cast Exception 并且文件没有发送到服务器。谁能帮助我,我将不胜感激。以上是以下代码:
服务器class:
package filesender;
import java.io.*;
import java.net.*;
public class ServidorTCP {
public static void main(String[] args)
throws IOException, UnknownHostException {
try (ServerSocket socketServer = new ServerSocket(6789)) {
Socket socket = socketServer.accept();
System.out.println("Conexão realizada com o cliente na porta 6789");
byte[] objectAsByte = new byte[socket.getReceiveBufferSize()];
BufferedInputStream bf =
new BufferedInputStream(socket.getInputStream());
bf.read(objectAsByte);
Arquivo arquivo = (Arquivo) getObjectFromByte(objectAsByte);
String dir = arquivo.getDiretorio() + "\" + arquivo.getNome();
System.out.println("Criando o arquivo: " + dir);
try (FileOutputStream fos = new FileOutputStream(dir)) {
fos.write(arquivo.getConteudo());
}
}
}
private static Object getObjectFromByte(byte[] objectAsByte) {
Object obj = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
bis = new ByteArrayInputStream(objectAsByte);
ois = new ObjectInputStream(bis);
obj = ois.read();
bis.close();
ois.close();
} catch (IOException e) {
}
return obj;
}
}
客户class
package filesender;
import java.io.*;
import java.net.*;
public class ClienteTCP {
public static Arquivo arquivo;
private static final long serialVersionUID = 1L;
public static void main(String[] args)
throws IOException, UnknownHostException {
selectFile();
try (Socket clientSocket = new Socket("127.0.0.1", 6789)) {
try (BufferedOutputStream bf =
new BufferedOutputStream(clientSocket.getOutputStream())) {
byte[] bytea = serializeFile();
bf.write(bytea);
bf.flush();
}
}
}
private static void selectFile() {
try {
File file = new File("C:\Users\DeveloperEng\Documents\"
+ "teste\arquivo.txt");
byte[] bFile = new byte[(int) file.length()];
try (FileInputStream fis = new FileInputStream(file)) {
fis.read(bFile);
}
arquivo = new Arquivo();
arquivo.setConteudo(bFile);
arquivo.setNome("teste.txt");
arquivo.setDiretorio("C:\Users\DeveloperEng\Documents");
} catch (IOException e) {
}
}
private static byte[] serializeFile() {
ByteArrayOutputStream bao = null;
ObjectOutputStream ous = null;
try {
bao = new ByteArrayOutputStream();
ous = new ObjectOutputStream(bao);
ous.writeObject(arquivo);
//return bao.toByteArray();
} catch (IOException e) {
}
return bao.toByteArray();
}
}
文件class
package filesender;
import java.io.*;
public class Arquivo implements Serializable {
private String nome;
private byte[] conteudo;
private String diretorio;
private static final long serialVersionUID = 1L;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public byte[] getConteudo() {
return conteudo;
}
public void setConteudo(byte[] conteudo) {
this.conteudo = conteudo;
}
public String getDiretorio() {
return diretorio;
}
public void setDiretorio(String diretorio) {
this.diretorio = diretorio;
}
}
IDE 引发的异常:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to filesender.Arquivo
at filesender.ServidorTCP.main(ServidorTCP.java:20)
C:\Users\DeveloperEng\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 5 segundos)
请参阅 ServidorTCP class 的第 20 行。 您不能将 "getObjectFromByte" 方法返回的值转换为 "Archivio",因为它不是兼容类型。特别是,它是一个整数。 发生这种情况是因为 ObjectInputStream 的读取方法 returns 一个 int (See Javadoc).